mirror of
https://github.com/HabitRPG/habitica.git
synced 2025-12-16 06:07:21 +01:00
Set _cronSignature to current time instead of uuid (#8565)
* Changes made to satisfy #8163. _cronSignature is set to current time when cron starts so that if cron fails to set _cronSignature to 'NOT_RUNNING' for any reason a new cron can be started after a set amount of time (1 hour for now) * fix lint errors * changed cronTimeout to CRON_TIMEOUT * Changed variable names and comments to be more clear * Fixed stub for failing test so that it matches new mongo db update call signature * First pass at unit tests, error messages and some other things need to be determined * Fixed a tab that snuck in :/ * Fixed lint issues (issues with spaces) * Fix infix operator spacing * Created constant. Make sure cron failure test verifies that it is failing for the right reason * Fixed lint errors * Removed no longer used uuid import
This commit is contained in:
@@ -4,15 +4,23 @@ import Bluebird from 'bluebird';
|
||||
import { model as Group } from '../models/group';
|
||||
import { model as User } from '../models/user';
|
||||
import { recoverCron, cron } from '../libs/cron';
|
||||
import { v4 as uuid } from 'uuid';
|
||||
|
||||
// Wait this length of time in ms before attempting another cron
|
||||
const CRON_TIMEOUT_WAIT = new Date(60 * 60 * 1000).getTime();
|
||||
|
||||
async function checkForActiveCron (user, now) {
|
||||
let _cronSignature = uuid();
|
||||
// set _cronSignature to current time in ms since epoch time so we can make sure to wait at least CRONT_TIMEOUT_WAIT before attempting another cron
|
||||
let _cronSignature = now.getTime();
|
||||
// Calculate how long ago cron must have been attempted to try again
|
||||
let cronRetryTime = _cronSignature - CRON_TIMEOUT_WAIT;
|
||||
|
||||
// To avoid double cron we first set _cronSignature and then check that it's not changed while processing
|
||||
let userUpdateResult = await User.update({
|
||||
_id: user._id,
|
||||
_cronSignature: 'NOT_RUNNING', // Check that in the meantime another cron has not started
|
||||
$or: [ // Make sure last cron was successful or failed before cronRetryTime
|
||||
{_cronSignature: 'NOT_RUNNING'},
|
||||
{_cronSignature: {$lt: cronRetryTime}},
|
||||
],
|
||||
}, {
|
||||
$set: {
|
||||
_cronSignature,
|
||||
|
||||
Reference in New Issue
Block a user