-
Notifications
You must be signed in to change notification settings - Fork 2
/
worker.js
74 lines (45 loc) · 1.4 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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
process.name = 'agendaWorker';
require('dotenv').config();
require('./server/utils/i18n');
const fs = require('fs');
const appRoot = require('app-root-path').path;
const agenda = require('./server/services/agenda');
const dbConn = require('./server/utils/db');
const nearDb = require('./server/services/near-db');
const tagLabel = process.name;
nearDb.init();
global.api = {
config: require('./server/config')
};
global.nearDb = nearDb;
global.imTheWorker = true;
global.utilities = require('@growishpay/service-utilities');
dbConn
.then(async db => {
utilities.logger.info(`Successfully connected to MongoDB cluster.`, {tagLabel});
await loadAgenda();
return db;
})
.catch(error => {
utilities.logger.error('Error while attempting to connect to database.', {error, tagLabel});
process.exit();
});
process.on('SIGINT', async () => {
utilities.logger.info('Gracefully stopping queue', {tagLabel});
try {
await agenda.stop();
} catch (error) {
utilities.logger.error('Cannot kill agenda', {tagLabel, error});
}
process.exit(0);
});
async function loadAgenda() {
await agenda.start();
fs.readdirSync(`${appRoot}/server/services/agenda/jobs`).map(file => {
require(`${appRoot}/server/services/agenda/jobs/${file}`)(agenda);
});
try {
process.send('ready');
} catch (e) {
}
}