// tslint:disable:no-console import * as moment from 'moment'; import * as mongoose from 'mongoose'; import { chevre } from '../../../lib/index'; const project = { id: String(process.env.PROJECT_ID) }; async function main() { await mongoose.connect(process.env.MONGOLAB_URI); const taskRepo = new chevre.repository.Task(mongoose.connection); const cursor = taskRepo.taskModel.find( { // _id: { $eq: 'xxxx' }, 'project.id': { $eq: project.id }, status: { $in: [chevre.factory.taskStatus.Aborted] }, name: { $eq: chevre.factory.taskName.VoidRegisterServiceTransaction }, runsAt: { $gt: moment() // tslint:disable-next-line:no-magic-numbers .add(-2, 'days') .toDate() } }, { // _id: 1, } ) // .limit(10) .sort({ runsAt: chevre.factory.sortType.Ascending }) .cursor(); console.log('tasks found'); let i = 0; let updateCount = 0; await cursor.eachAsync(async (doc) => { i += 1; const task = >doc.toObject(); console.log('retrying...', task.id, task.name, task.runsAt, task.project.id); await taskRepo.taskModel.findByIdAndUpdate( task.id, { status: chevre.factory.taskStatus.Ready, remainingNumberOfTries: task.remainingNumberOfTries + 1 } ) .exec(); updateCount += 1; }); console.log(i, 'tasks checked'); console.log(updateCount, 'tasks updated'); } main() .then() .catch(console.error); // setInterval( // () => { // main() // .then() // .catch(console.error); // }, // // tslint:disable-next-line:no-magic-numbers // 60000 // );