mirror of
https://github.com/HabitRPG/habitica.git
synced 2025-12-16 06:07:21 +01:00
Tasks cron ryamodal fixes (#8871)
* Changed assumption of timezone location * Added checks for RYA and moved cron check * Fixed modal scope issue
This commit is contained in:
committed by
Sabe Jones
parent
4cd272f99c
commit
bc477455bb
@@ -7,6 +7,36 @@ import { recoverCron, cron } from '../libs/cron';
|
||||
import { v4 as uuid } from 'uuid';
|
||||
import logger from '../libs/logger';
|
||||
|
||||
async function checkForActiveCron (user, now) {
|
||||
let _cronSignature = uuid();
|
||||
|
||||
// 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
|
||||
}, {
|
||||
$set: {
|
||||
_cronSignature,
|
||||
lastCron: now, // setting lastCron now so we don't risk re-running parts of cron if it fails
|
||||
'auth.timestamps.loggedin': now,
|
||||
},
|
||||
}).exec();
|
||||
|
||||
// If the cron signature is already set, cron is running in another request
|
||||
// throw an error and recover later,
|
||||
if (userUpdateResult.nMatched === 0 || userUpdateResult.nModified === 0) {
|
||||
throw new Error('CRON_ALREADY_RUNNING');
|
||||
}
|
||||
}
|
||||
|
||||
async function unlockUser (user) {
|
||||
await User.update({
|
||||
_id: user._id,
|
||||
}, {
|
||||
_cronSignature: 'NOT_RUNNING',
|
||||
}).exec();
|
||||
}
|
||||
|
||||
async function cronAsync (req, res) {
|
||||
let user = res.locals.user;
|
||||
if (!user) return null; // User might not be available when authentication is not mandatory
|
||||
@@ -17,31 +47,14 @@ async function cronAsync (req, res) {
|
||||
try {
|
||||
let {daysMissed, timezoneOffsetFromUserPrefs} = user.daysUserHasMissed(now, req);
|
||||
|
||||
await checkForActiveCron(user, now);
|
||||
|
||||
if (daysMissed <= 0) {
|
||||
if (user.isModified()) await user.save();
|
||||
await unlockUser(user);
|
||||
return null;
|
||||
}
|
||||
|
||||
let _cronSignature = uuid();
|
||||
|
||||
// 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
|
||||
}, {
|
||||
$set: {
|
||||
_cronSignature,
|
||||
lastCron: now, // setting lastCron now so we don't risk re-running parts of cron if it fails
|
||||
'auth.timestamps.loggedin': now,
|
||||
},
|
||||
}).exec();
|
||||
|
||||
// If the cron signature is already set, cron is running in another request
|
||||
// throw an error and recover later,
|
||||
if (userUpdateResult.nMatched === 0 || userUpdateResult.nModified === 0) {
|
||||
throw new Error('CRON_ALREADY_RUNNING');
|
||||
}
|
||||
|
||||
let tasks = await Tasks.Task.find({
|
||||
userId: user._id,
|
||||
$or: [ // Exclude completed todos
|
||||
|
||||
Reference in New Issue
Block a user