@sidequest/engine
Version:
@sidequest/engine is the core engine of SideQuest, a distributed background job processing system for Node.js and TypeScript.
48 lines (44 loc) • 1.97 kB
JavaScript
;
var core = require('@sidequest/core');
var util = require('util');
require('../job/constants.cjs');
require('node-cron');
require('node:util');
require('node:fs');
require('node:url');
require('node:path');
require('piscina');
require('../constants.cjs');
var jobTransitioner = require('../job/job-transitioner.cjs');
/**
* Finds and releases stale jobs, making them available for processing again.
* @param backend The backend instance to operate on.
* @param maxStaleMs Maximum age of a job to be considered stale.
* @param maxClaimedMs Maximum age of a claimed job to be considered stale.
* @returns A promise that resolves when the operation is complete.
*/
async function releaseStaleJobs(backend, maxStaleMs, maxClaimedMs) {
const staleJobs = await backend.staleJobs(maxStaleMs, maxClaimedMs);
if (staleJobs.length > 0) {
core.logger("Engine").info(`Stale jobs found, making them available to process`);
core.logger("Engine").debug(`Stale jobs: ${util.inspect(staleJobs)}`);
for (const jobData of staleJobs) {
if (jobData.state === "running") {
// We need to use the JobTransitioner to properly handle retries and state transitions
// This fixes the issue where the release of a stale job incremented the retry count and
// did not respect the maxRetries setting.
await jobTransitioner.JobTransitioner.apply(backend, jobData, new core.RetryTransition("Stale job released for retry"));
}
else {
// If it's "claimed", then the attempt count was not incremented, so we can just set it back to "waiting"
jobData.state = "waiting";
await backend.updateJob(jobData);
}
}
}
else {
core.logger("Engine").debug(`No stale jobs found`);
}
}
exports.releaseStaleJobs = releaseStaleJobs;
//# sourceMappingURL=release-stale-jobs.cjs.map