@sidequest/core
Version:
@sidequest/core is the core package of SideQuest, a distributed background job queue for Node.js and TypeScript applications.
49 lines (45 loc) • 1.41 kB
JavaScript
;
var logger = require('../logger.cjs');
var parseErrorData = require('../tools/parse-error-data.cjs');
var transition = require('./transition.cjs');
/**
* Transition for marking a job as failed.
*
* This transition sets the job state to "failed" and records the
* failure reason and timestamp. It also stores the error data in the job's errors array.
* Failed jobs will not be retried or processed again.
*
* This transition can only be applied to jobs that are currently running.
*/
class FailTransition extends transition.JobTransition {
/** The reason for failure. */
reason;
/**
* Creates a new FailTransition.
* @param reason The reason for the job failure.
*/
constructor(reason) {
super();
this.reason = reason;
}
apply(job) {
logger.logger("Core").error(this.reason);
const error = parseErrorData.toErrorData(this.reason);
job.errors ??= [];
const errData = {
...error,
attempt: job.attempt,
attempted_at: job.attempted_at,
attempt_by: job.claimed_by,
};
job.errors.push(errData);
job.state = "failed";
job.failed_at = new Date();
return job;
}
shouldRun(job) {
return job.state === "running";
}
}
exports.FailTransition = FailTransition;
//# sourceMappingURL=fail-transition.cjs.map