cron: set lastCron at the beginning and misc fixes

This commit is contained in:
Matteo Pagliazzi
2016-05-31 17:10:31 +02:00
committed by Blade Barringer
parent c0d9c54c44
commit e07ade5a4a
4 changed files with 47 additions and 51 deletions

View File

@@ -12,6 +12,29 @@ const shouldDo = common.shouldDo;
const scoreTask = common.ops.scoreTask;
// const maxPMs = 200;
export async function recoverCron (status, locals) {
let {user} = locals;
await Bluebird.delay(300);
let reloadedUser = await User.findOne({_id: user._id}).exec();
if (!reloadedUser) {
throw new Error(`User ${user._id} not found while recovering.`);
} else if (reloadedUser._cronSignature !== 'NOT_RUNNING') {
status.times++;
if (status.times < 5) {
await recoverCron(status, locals);
} else {
throw new Error(`Impossible to recover from cron for user ${user._id}.`);
}
} else {
locals.user = reloadedUser;
return null;
}
}
let CLEAR_BUFFS = {
str: 0,
int: 0,
@@ -83,29 +106,6 @@ function performSleepTasks (user, tasksByType, now) {
});
}
export async function recoverCron (status, locals) {
let {user} = locals;
await Bluebird.delay(300);
let reloadedUser = await User.findOne({_id: user._id}).exec();
if (!reloadedUser) {
throw new Error(`User ${user._id} not found while recovering.`);
} else if (reloadedUser._cronSignature !== 'NOT_RUNNING') {
status.times++;
if (status.times < 4) {
await recoverCron(status, locals);
} else {
throw new Error(`Impossible to recover from cron for user ${user._id}.`);
}
} else {
locals.user = reloadedUser;
return null;
}
}
// Perform various beginning-of-day reset actions.
export function cron (options = {}) {
let {user, tasksByType, analytics, now = new Date(), daysMissed, timezoneOffsetFromUserPrefs} = options;