-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathworker.js
59 lines (49 loc) · 1.23 KB
/
worker.js
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
51
52
53
54
55
56
57
58
59
const {configureQueues, createJobQueue, disconnectQueues} = require('bull-manager')
const sentry = require('./lib/utils/sentry')
const mongo = require('./lib/utils/mongo')
const store = require('./lib/utils/store')
const createRedis = require('./lib/utils/redis')
const jobs = require('./jobs/definitions')
async function main() {
await mongo.connect()
await mongo.ensureIndexes()
await store.createBucket()
configureQueues({
isSubscriber: true,
createRedis: createRedis({
onError: shutdown
}),
prefix: 'link-proxy',
onError: (job, err) => sentry.captureException(err, {
extra: {
queue: job.queue.name,
...job.data
}
})
})
await Promise.all(
jobs.map(job => {
const {handler, onError} = require(`./jobs/${job.name}`)
return createJobQueue(job.name, handler, {
concurrency: job.concurrency,
onError
}, job.options)
})
)
mongo.client.on('close', () => {
shutdown(new Error('Mongo connection was closed'))
})
}
main().catch(error => {
shutdown(error)
})
async function shutdown(err) {
await Promise.all([
disconnectQueues(),
mongo.disconnect()
])
if (err) {
sentry.captureException(err)
process.exit(1)
}
}