@sidequest/core
Version:
@sidequest/core is the core package of SideQuest, a distributed background job queue for Node.js and TypeScript applications.
43 lines (40 loc) • 1.41 kB
JavaScript
import { logger } from '../logger.js';
import { JobTransition } from './transition.js';
/**
* 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 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("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);
}
}
export { SnoozeTransition };
//# sourceMappingURL=snooze-transition.js.map