@sidequest/core
Version:
@sidequest/core is the core package of SideQuest, a distributed background job queue for Node.js and TypeScript applications.
41 lines (38 loc) • 1.48 kB
JavaScript
import crypto from 'crypto';
import { stringify } from 'safe-stable-stringify';
import { logger } from '../logger.js';
const aliveStates = ["waiting", "claimed", "running"];
/**
* Uniqueness strategy for jobs that are still alive (waiting, claimed, or running).
*/
class AliveJobUniqueness {
config;
/**
* Creates a new AliveJobUniqueness instance.
* @param config The alive job uniqueness configuration.
*/
constructor(config) {
this.config = config;
}
/**
* Computes a digest for the job data if the job is in an alive state.
* @param jobData The job data to compute the digest for.
* @returns The digest string or null if not alive.
*/
digest(jobData) {
logger("Core").debug(`Creating alive digest for job ${jobData.id} with state ${jobData.state}`);
if (aliveStates.includes(jobData.state)) {
let key = jobData.class;
if (this.config.withArgs) {
key += "::args=" + stringify(jobData.args ?? []);
key += "::ctor=" + stringify(jobData.constructor_args ?? []);
}
logger("Core").debug(`Job is alive. Key for digest: ${key}`);
return crypto.createHash("sha256").update(key).digest("hex");
}
logger("Core").debug(`Job ${jobData.id} is not alive, returning null for digest.`);
return null;
}
}
export { AliveJobUniqueness };
//# sourceMappingURL=alive-job-uniqueness.js.map