@sidequest/core
Version:
@sidequest/core is the core package of SideQuest, a distributed background job queue for Node.js and TypeScript applications.
47 lines (44 loc) • 1.35 kB
JavaScript
import { logger } from '../logger.js';
import { toErrorData } from '../tools/parse-error-data.js';
import { JobTransition } from './transition.js';
/**
* 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 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("Core").error(this.reason);
const error = 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";
}
}
export { FailTransition };
//# sourceMappingURL=fail-transition.js.map