This repository was archived by the owner on Jan 16, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.js
57 lines (51 loc) · 1.43 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
var handler = require('./')
var http = require('http')
var pino = require('pino')
var runSeries = require('run-series')
var schedule = require('node-schedule')
var sweepOffers = require('./jobs/offers')
var sweepPrescriptions = require('./jobs/prescriptions')
var uuid = require('uuid')
var log = pino({server: uuid.v4()})
runSeries([
function launchServer (done) {
var server = http.createServer(function (request, response) {
handler(log, request, response)
})
function close () {
log.info('closing')
server.close(function () {
log.info('closed')
process.exit(0)
})
}
function trapSignal () {
close()
}
process.on('SIGINT', trapSignal)
process.on('SIGQUIT', trapSignal)
process.on('SIGTERM', trapSignal)
process.on('uncaughtException', function (exception) {
log.error(exception)
close()
})
server.listen(process.env.PORT || 0, function () {
// If the environment set PORT=0, we'll get a random high port.
var port = this.address().port
log.info({port: port}, 'listening')
done()
})
},
function filesweeper (done) {
var jobs = [sweepPrescriptions, sweepOffers]
jobs.forEach(function (job) {
job(log, function () { })
schedule.scheduleJob('0 * * * *', function () {
job(log, function () { /* pass */ })
})
})
done()
}
], function (error) {
if (error) log.error(error)
})