-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.ts
50 lines (39 loc) · 1.34 KB
/
index.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
import fastifyCron from 'fastify-cron'
import config from './src/config/server.config'
import logger from './src/utils/logger'
import getServer from './src/server'
import WezardError from './src/utils/WezardError'
import db from './src/utils/db'
const main = async () => {
try {
await db.$connect()
logger.info(`Database connection setup! ${config.databaseUrl}`)
const server = await getServer()
server.addHook('onClose', async () => {
logger.info(`${config.serviceName} was closed`)
// close the db connection
await db.$disconnect()
})
await server.register(fastifyCron, {
jobs: [
{
cronTime: '0 * * * *', // Everyhour
onTick: () => console.log('Internal cronjob'),
startWhenReady: true
}
]
})
await server.listen({ port: config.port, host: '0.0.0.0' })
server.cron.startAllJobs()
logger.info(`Server running at port ${config.port}`)
} catch (err) {
await db.$disconnect()
logger.info(`Could not start ${config.serviceName}`, { error: err as WezardError })
process.exit(1)
}
process.on('SIGTERM', () => process.exit())
}
main().catch((e) => {
logger.error(e)
process.exit(1)
})