-
Notifications
You must be signed in to change notification settings - Fork 39
/
clean-database.js
34 lines (29 loc) · 1002 Bytes
/
clean-database.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
/*
Deletes expired checkpoints. Should be scheduled to run once per day.
*/
require('dotenv').config()
var mongoose = require('mongoose')
const readline = require('readline')
const Checkpoint = require('./models/checkpoint')
const oneDay = 1000 * 60 * 60 * 24
const estimatedDiagnosisDelay = Number(process.env['QUARANTINE_DAYS']) * oneDay
const mongoDbUri = process.env.MONGODB_URI || 'mongodb://localhost/checkpoints'
const rl = readline.createInterface({
input: process.stdin,
output: process.stdout,
terminal: true
})
rl.write('Cleaning database...\n')
mongoose.connect(mongoDbUri, { useNewUrlParser: true })
const db = mongoose.connection
db.once('open', function () {
const startSearchTimestamp = Date.now() - estimatedDiagnosisDelay
Checkpoint.deleteMany({ timestamp: { $lt: startSearchTimestamp } }, function (err) {
if (err) {
rl.write(String(err))
} else {
rl.write('\nExpired checkpoints were successfully deleted.\n\n')
}
process.exit()
})
})