@sidequest/core
Version:
@sidequest/core is the core package of SideQuest, a distributed background job queue for Node.js and TypeScript applications.
45 lines (41 loc) • 1.46 kB
JavaScript
;
var logger = require('../logger.cjs');
var transition = require('./transition.cjs');
/**
* Transition for snoozing (delaying) a job.
*
* This transition sets the job state to "waiting" and updates the
* available_at timestamp to the current time plus the specified delay.
* If the job is currently running, it will decrement the attempt count.
* This allows the job to be retried after the delay.
*
* Only jobs in "waiting" or "running" state can be snoozed.
*/
class SnoozeTransition extends transition.JobTransition {
/** The delay in milliseconds. */
delay;
/**
* Creates a new SnoozeTransition.
* @param delay The delay in milliseconds.
*/
constructor(delay) {
super();
this.delay = delay;
}
apply(job) {
logger.logger("Core").info(`Job #${job.id} - ${job.class} snoozed by ${this.delay}ms`);
// Attempts are only decremented if the job is running and has already been attempted
// This means that the job will not consider the current run as an attempt
if (job.state === "running" && job.attempt > 0) {
job.attempt -= 1;
}
job.state = "waiting";
job.available_at = new Date(Date.now() + this.delay);
return job;
}
shouldRun(job) {
return ["waiting", "running"].includes(job.state);
}
}
exports.SnoozeTransition = SnoozeTransition;
//# sourceMappingURL=snooze-transition.cjs.map