UNPKG

@sidequest/core

Version:

@sidequest/core is the core package of SideQuest, a distributed background job queue for Node.js and TypeScript applications.

66 lines (63 loc) 2.29 kB
import { Uniqueness } from './uniqueness.js'; import { JobData } from '../schema/job-data.js'; /** * Time period granularity for fixed window uniqueness. */ type TimePeriod = "second" | "minute" | "hour" | "day" | "week" | "month"; /** * Configuration for fixed window uniqueness strategy. */ interface FixedWindowConfig { /** The type of uniqueness strategy. */ type: "fixed-window"; /** The time period granularity. */ period: TimePeriod; /** Whether to include job arguments in the digest. */ withArgs?: boolean; } /** * Implements uniqueness checking using a fixed time window approach. * * This class generates unique digest keys for jobs based on their class name, * execution time (truncated to a configured period), and optionally their arguments. * Jobs with identical digests within the same time window are considered duplicates. * * The time window is determined by truncating the job's `available_at` timestamp * to the specified period granularity (second, minute, hour, day, week, or month). * * @example * ```typescript * const uniqueness = new FixedWindowUniqueness({ * period: 'hour', * withArgs: true * }); * * const digest = uniqueness.digest(jobData); * // Returns SHA256 hash of "JobClass::time=2023-01-01T15:00:00.000Z::args=[...]::ctor=[...]" * ``` * * In this example, jobs of the same class scheduled within the same hour with the same arguments * will have the same digest and thus won't be duplicated. */ declare class FixedWindowUniqueness implements Uniqueness { config: FixedWindowConfig; /** * Creates a new FixedWindowUniqueness instance. * @param config The fixed window configuration. */ constructor(config: FixedWindowConfig); /** * Computes a digest for the job data based on the configured time window and arguments. * @param jobData The job data to compute the digest for. * @returns The digest string. */ digest(jobData: JobData): string | null; /** * Truncates a date to the configured time period granularity. * @param date The date to truncate. * @returns The truncated date as an ISO string. */ private truncateDateString; } export { FixedWindowUniqueness }; export type { FixedWindowConfig, TimePeriod };