-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathqueue.js
51 lines (42 loc) · 1.4 KB
/
queue.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
require('dotenv').config();
const agenda = require('./services/agenda');
const jobs = require('./jobs/index');
const dayjs = require('dayjs');
const utc = require('dayjs/plugin/utc')
const timezone = require('dayjs/plugin/timezone') // dependent on utc plugin
const fs = require('fs');
const path = require('path');
// Set up timezone argentina for dayjs
dayjs.extend(utc)
dayjs.extend(timezone)
dayjs.tz.setDefault("America/Argentina/Buenos_Aires");
// job processor
agenda.define('test', async job => {
try {
const { message } = job.attrs.data;
console.log(message);
} catch (error) {
console.error(error);
}
});
agenda.define('send-mail', jobs.sendMail)
// start agenda
agenda.start();
// ---------------------------------------------------
async function graceful() {
await agenda.stop();
// write a file in the root of the directory that saves 1 if the app is not alive
fs.writeFileSync(path.join(__dirname, 'alive'), '1');
process.exit(0);
}
// every 5 secs, write a file in the root of the directory that saves 0 if the app is alive
fs.writeFileSync(path.join(__dirname, 'alive'), '0');
setInterval(() => {
fs.writeFileSync(path.join(__dirname, 'alive'), '0');
}, 5 * 1000);
// on process kill of some reason, stop agenda
process.on('SIGTERM', graceful);
process.on('SIGINT', graceful);
process.on('exit', graceful);
process.on('beforeExit', graceful);
process.on('uncaughtException', graceful);