@chevre/domain
Version:
Chevre Domain Library for Node.js
43 lines (35 loc) • 1.21 kB
text/typescript
// tslint:disable:no-console
import * as moment from 'moment';
import * as mongoose from 'mongoose';
import { chevre } from '../../../lib/index';
const TASK_STORAGE_PERIOD_IN_MINUTES = 43200; // 30*24*60
async function main() {
const now = new Date();
await mongoose.connect(<string>process.env.MONGOLAB_URI, { autoIndex: false });
const taskRepo = await chevre.repository.Task.createInstance(mongoose.connection);
let taskInMinutes = 57600; // 40*24*60
// let taskInMinutes = 172800; // 120*24*60
// tslint:disable-next-line:no-magic-numbers
while (taskInMinutes > TASK_STORAGE_PERIOD_IN_MINUTES) {
taskInMinutes -= 1;
console.log(
'cleaning...',
taskInMinutes,
moment.duration(taskInMinutes, 'minutes')
.asDays(),
'days'
);
const result = await taskRepo.deleteRunsAtPassedCertainPeriod({
runsAt: {
$lt: moment(now)
.add(-taskInMinutes, 'minutes')
.toDate()
}
});
console.log('deleted', result);
}
console.log('success!');
}
main()
.then()
.catch(console.error);