-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathinit.js
42 lines (33 loc) · 841 Bytes
/
init.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
const mongoose = require('mongoose'),
cron = require('cron').CronJob,
lib = require('./lib'),
Status = require('./model')
const collect = async () => {
let status
try {
status = await lib.collect()
} catch (error) {
throw new Error('Cannot collect!')
}
try {
let s = new Status(status)
await s.save()
} catch (error) {
throw new Error('Cannot save data!')
}
return
}
module.exports = async () => {
try {
mongoose.connect(process.env.MONGODB, { useNewUrlParser: true, useUnifiedTopology: true })
} catch (error) {
throw new Error('Cannot conenct to mongodb')
}
const cronString = process.env.CRON || '0 0 * * * *'
await collect()
const job = new cron(cronString, async () => {
console.log('running job')
await collect()
})
job.start()
}