-
Notifications
You must be signed in to change notification settings - Fork 1
/
server.js
85 lines (65 loc) · 2.23 KB
/
server.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
75
76
77
78
79
80
81
82
83
84
85
const next = require('next')
const routes = require('./routes')
const app = next({ dev: process.env.NODE_ENV !== 'production' })
const express = require('express')
const mongoose = require('mongoose')
const Agenda = require('agenda')
const addCheckStatsJob = require('./jobs/check-stats')
const addCheckNotifierStatsJob = require('./jobs/check-notifier-stats')
const addCheckNodesJob = require('./jobs/check-nodes')
const addCheckPeersJob = require('./jobs/check-peers')
const addCheckPeerIpsJob = require('./jobs/check-peer-ips')
const addCleanCompletedJobsJob = require('./jobs/clean-completed-jobs')
const handler = routes.getRequestHandler(app, ({ req, res, route, query }) => {
// console.log('handler', route, query, req.nextRoute.data)
app.render(req, res, route.page, query)
})
mongoose.connect(process.env.MONGODB_URI, {
useNewUrlParser: true,
useUnifiedTopology: true,
useFindAndModify: false,
});
const db = mongoose.connection;
db.on('error', console.error.bind(console, 'connection error:'));
db.once('open', function () {
// we're connected!
app.prepare().then(() => {
express().use(handler).listen(3000)
})
});
const agenda = new Agenda({
db: {
address: process.env.MONGODB_URI,
options: {
useNewUrlParser: true,
useUnifiedTopology: true,
},
},
});
addCheckNotifierStatsJob.job(agenda)
addCheckStatsJob.job(agenda)
addCheckNodesJob.job(agenda)
if (process.env.START_JOBS && process.env.START_JOBS === 'true') {
addCheckPeersJob.job(agenda)
addCheckPeerIpsJob.job(agenda)
}
addCleanCompletedJobsJob.job(agenda)
async function graceful() {
await agenda.stop();
process.exit(0);
}
process.on('SIGTERM', graceful);
process.on('SIGINT', graceful);
(async function () { // IIFE to give access to async/await
await agenda.start();
await agenda.every('5 minutes', ['check stats', 'check notifier stats']);
if (process.env.START_JOBS && process.env.START_JOBS === 'true') {
await agenda.every('10 minutes', ['check peers']);
}
await agenda.every('1 hour', ['check nodes']);
await agenda.every('1 day', ['clean completed jobs']);
await agenda.cancel({ nextRunAt: null }, (err, numRemoved) => {
debug(err);
debug('Number of finished jobs removed', numRemoved);
});
})();