@nicnocquee/dataqueue 1.30.0 → 1.32.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/index.cjs +2531 -1283
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.cts +367 -17
- package/dist/index.d.ts +367 -17
- package/dist/index.js +2530 -1284
- package/dist/index.js.map +1 -1
- package/migrations/1781200000004_create_cron_schedules_table.sql +33 -0
- package/package.json +3 -2
- package/src/backend.ts +139 -4
- package/src/backends/postgres.ts +676 -30
- package/src/backends/redis-scripts.ts +197 -22
- package/src/backends/redis.test.ts +971 -0
- package/src/backends/redis.ts +789 -22
- package/src/cron.test.ts +126 -0
- package/src/cron.ts +40 -0
- package/src/index.test.ts +361 -0
- package/src/index.ts +165 -29
- package/src/processor.ts +36 -97
- package/src/queue.test.ts +29 -0
- package/src/queue.ts +19 -251
- package/src/types.ts +177 -10
package/dist/index.d.cts
CHANGED
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
import * as pg from 'pg';
|
|
2
2
|
import { Pool } from 'pg';
|
|
3
|
+
import { Cron } from 'croner';
|
|
3
4
|
|
|
4
5
|
type JobType<PayloadMap> = keyof PayloadMap & string;
|
|
5
6
|
interface JobOptions<PayloadMap, T extends JobType<PayloadMap>> {
|
|
@@ -448,6 +449,23 @@ interface PostgresJobQueueConfig {
|
|
|
448
449
|
user?: string;
|
|
449
450
|
password?: string;
|
|
450
451
|
ssl?: DatabaseSSLConfig;
|
|
452
|
+
/**
|
|
453
|
+
* Maximum number of clients in the pool (default: 10).
|
|
454
|
+
* Increase when running multiple processors in the same process.
|
|
455
|
+
*/
|
|
456
|
+
max?: number;
|
|
457
|
+
/**
|
|
458
|
+
* Minimum number of idle clients in the pool (default: 0).
|
|
459
|
+
*/
|
|
460
|
+
min?: number;
|
|
461
|
+
/**
|
|
462
|
+
* Milliseconds a client must sit idle before being closed (default: 10000).
|
|
463
|
+
*/
|
|
464
|
+
idleTimeoutMillis?: number;
|
|
465
|
+
/**
|
|
466
|
+
* Milliseconds to wait for a connection before throwing (default: 0, no timeout).
|
|
467
|
+
*/
|
|
468
|
+
connectionTimeoutMillis?: number;
|
|
451
469
|
};
|
|
452
470
|
verbose?: boolean;
|
|
453
471
|
}
|
|
@@ -490,6 +508,81 @@ type JobQueueConfig = PostgresJobQueueConfig | RedisJobQueueConfig;
|
|
|
490
508
|
/** @deprecated Use JobQueueConfig instead. Alias kept for backward compat. */
|
|
491
509
|
type JobQueueConfigLegacy = PostgresJobQueueConfig;
|
|
492
510
|
type TagQueryMode = 'exact' | 'all' | 'any' | 'none';
|
|
511
|
+
/**
|
|
512
|
+
* Status of a cron schedule.
|
|
513
|
+
*/
|
|
514
|
+
type CronScheduleStatus = 'active' | 'paused';
|
|
515
|
+
/**
|
|
516
|
+
* Options for creating a recurring cron schedule.
|
|
517
|
+
* Each schedule defines a recurring job that is automatically enqueued
|
|
518
|
+
* when its cron expression matches.
|
|
519
|
+
*/
|
|
520
|
+
interface CronScheduleOptions<PayloadMap, T extends JobType<PayloadMap>> {
|
|
521
|
+
/** Unique human-readable name for the schedule. */
|
|
522
|
+
scheduleName: string;
|
|
523
|
+
/** Standard cron expression (5 fields, e.g. "0 * * * *"). */
|
|
524
|
+
cronExpression: string;
|
|
525
|
+
/** Job type from the PayloadMap. */
|
|
526
|
+
jobType: T;
|
|
527
|
+
/** Payload for each job instance. */
|
|
528
|
+
payload: PayloadMap[T];
|
|
529
|
+
/** Maximum retry attempts for each job instance (default: 3). */
|
|
530
|
+
maxAttempts?: number;
|
|
531
|
+
/** Priority for each job instance (default: 0). */
|
|
532
|
+
priority?: number;
|
|
533
|
+
/** Timeout in milliseconds for each job instance. */
|
|
534
|
+
timeoutMs?: number;
|
|
535
|
+
/** Whether to force-kill the job on timeout (default: false). */
|
|
536
|
+
forceKillOnTimeout?: boolean;
|
|
537
|
+
/** Tags for each job instance. */
|
|
538
|
+
tags?: string[];
|
|
539
|
+
/** IANA timezone string for cron evaluation (default: "UTC"). */
|
|
540
|
+
timezone?: string;
|
|
541
|
+
/**
|
|
542
|
+
* Whether to allow overlapping job instances (default: false).
|
|
543
|
+
* When false, a new job will not be enqueued if the previous instance
|
|
544
|
+
* is still pending, processing, or waiting.
|
|
545
|
+
*/
|
|
546
|
+
allowOverlap?: boolean;
|
|
547
|
+
}
|
|
548
|
+
/**
|
|
549
|
+
* A persisted cron schedule record.
|
|
550
|
+
*/
|
|
551
|
+
interface CronScheduleRecord {
|
|
552
|
+
id: number;
|
|
553
|
+
scheduleName: string;
|
|
554
|
+
cronExpression: string;
|
|
555
|
+
jobType: string;
|
|
556
|
+
payload: any;
|
|
557
|
+
maxAttempts: number;
|
|
558
|
+
priority: number;
|
|
559
|
+
timeoutMs: number | null;
|
|
560
|
+
forceKillOnTimeout: boolean;
|
|
561
|
+
tags: string[] | undefined;
|
|
562
|
+
timezone: string;
|
|
563
|
+
allowOverlap: boolean;
|
|
564
|
+
status: CronScheduleStatus;
|
|
565
|
+
lastEnqueuedAt: Date | null;
|
|
566
|
+
lastJobId: number | null;
|
|
567
|
+
nextRunAt: Date | null;
|
|
568
|
+
createdAt: Date;
|
|
569
|
+
updatedAt: Date;
|
|
570
|
+
}
|
|
571
|
+
/**
|
|
572
|
+
* Options for editing an existing cron schedule.
|
|
573
|
+
* All fields are optional; only provided fields are updated.
|
|
574
|
+
*/
|
|
575
|
+
interface EditCronScheduleOptions {
|
|
576
|
+
cronExpression?: string;
|
|
577
|
+
payload?: any;
|
|
578
|
+
maxAttempts?: number;
|
|
579
|
+
priority?: number;
|
|
580
|
+
timeoutMs?: number | null;
|
|
581
|
+
forceKillOnTimeout?: boolean;
|
|
582
|
+
tags?: string[] | null;
|
|
583
|
+
timezone?: string;
|
|
584
|
+
allowOverlap?: boolean;
|
|
585
|
+
}
|
|
493
586
|
interface JobQueue<PayloadMap> {
|
|
494
587
|
/**
|
|
495
588
|
* Add a job to the job queue.
|
|
@@ -550,12 +643,18 @@ interface JobQueue<PayloadMap> {
|
|
|
550
643
|
retryJob: (jobId: number) => Promise<void>;
|
|
551
644
|
/**
|
|
552
645
|
* Cleanup jobs that are older than the specified number of days.
|
|
646
|
+
* Deletes in batches for scale safety.
|
|
647
|
+
* @param daysToKeep - Number of days to retain completed jobs (default 30).
|
|
648
|
+
* @param batchSize - Number of rows to delete per batch (default 1000 for PostgreSQL, 200 for Redis).
|
|
553
649
|
*/
|
|
554
|
-
cleanupOldJobs: (daysToKeep?: number) => Promise<number>;
|
|
650
|
+
cleanupOldJobs: (daysToKeep?: number, batchSize?: number) => Promise<number>;
|
|
555
651
|
/**
|
|
556
652
|
* Cleanup job events that are older than the specified number of days.
|
|
653
|
+
* Deletes in batches for scale safety.
|
|
654
|
+
* @param daysToKeep - Number of days to retain events (default 30).
|
|
655
|
+
* @param batchSize - Number of rows to delete per batch (default 1000).
|
|
557
656
|
*/
|
|
558
|
-
cleanupOldJobEvents: (daysToKeep?: number) => Promise<number>;
|
|
657
|
+
cleanupOldJobEvents: (daysToKeep?: number, batchSize?: number) => Promise<number>;
|
|
559
658
|
/**
|
|
560
659
|
* Cancel a job given its ID.
|
|
561
660
|
* - This will set the job status to 'cancelled' and clear the locked_at and locked_by.
|
|
@@ -642,8 +741,6 @@ interface JobQueue<PayloadMap> {
|
|
|
642
741
|
* Tokens can be completed externally to resume a waiting job.
|
|
643
742
|
* Can be called outside of handlers (e.g., from an API route).
|
|
644
743
|
*
|
|
645
|
-
* **PostgreSQL backend only.** Throws if the backend is Redis.
|
|
646
|
-
*
|
|
647
744
|
* @param options - Optional token configuration (timeout, tags).
|
|
648
745
|
* @returns A token object with `id`.
|
|
649
746
|
*/
|
|
@@ -652,8 +749,6 @@ interface JobQueue<PayloadMap> {
|
|
|
652
749
|
* Complete a waitpoint token, resuming the associated waiting job.
|
|
653
750
|
* Can be called from anywhere (API routes, external services, etc.).
|
|
654
751
|
*
|
|
655
|
-
* **PostgreSQL backend only.** Throws if the backend is Redis.
|
|
656
|
-
*
|
|
657
752
|
* @param tokenId - The ID of the token to complete.
|
|
658
753
|
* @param data - Optional data to pass to the waiting handler.
|
|
659
754
|
*/
|
|
@@ -661,8 +756,6 @@ interface JobQueue<PayloadMap> {
|
|
|
661
756
|
/**
|
|
662
757
|
* Retrieve a waitpoint token by its ID.
|
|
663
758
|
*
|
|
664
|
-
* **PostgreSQL backend only.** Throws if the backend is Redis.
|
|
665
|
-
*
|
|
666
759
|
* @param tokenId - The ID of the token to retrieve.
|
|
667
760
|
* @returns The token record, or null if not found.
|
|
668
761
|
*/
|
|
@@ -671,11 +764,59 @@ interface JobQueue<PayloadMap> {
|
|
|
671
764
|
* Expire timed-out waitpoint tokens and resume their associated jobs.
|
|
672
765
|
* Call this periodically (e.g., alongside `reclaimStuckJobs`).
|
|
673
766
|
*
|
|
674
|
-
* **PostgreSQL backend only.** Throws if the backend is Redis.
|
|
675
|
-
*
|
|
676
767
|
* @returns The number of tokens that were expired.
|
|
677
768
|
*/
|
|
678
769
|
expireTimedOutTokens: () => Promise<number>;
|
|
770
|
+
/**
|
|
771
|
+
* Add a recurring cron schedule. The processor automatically enqueues
|
|
772
|
+
* due cron jobs before each batch, so no manual triggering is needed.
|
|
773
|
+
*
|
|
774
|
+
* @returns The ID of the created schedule.
|
|
775
|
+
* @throws If the cron expression is invalid or the schedule name is already taken.
|
|
776
|
+
*/
|
|
777
|
+
addCronJob: <T extends JobType<PayloadMap>>(options: CronScheduleOptions<PayloadMap, T>) => Promise<number>;
|
|
778
|
+
/**
|
|
779
|
+
* Get a cron schedule by its ID.
|
|
780
|
+
*/
|
|
781
|
+
getCronJob: (id: number) => Promise<CronScheduleRecord | null>;
|
|
782
|
+
/**
|
|
783
|
+
* Get a cron schedule by its unique name.
|
|
784
|
+
*/
|
|
785
|
+
getCronJobByName: (name: string) => Promise<CronScheduleRecord | null>;
|
|
786
|
+
/**
|
|
787
|
+
* List all cron schedules, optionally filtered by status.
|
|
788
|
+
*/
|
|
789
|
+
listCronJobs: (status?: CronScheduleStatus) => Promise<CronScheduleRecord[]>;
|
|
790
|
+
/**
|
|
791
|
+
* Remove a cron schedule by its ID. Does not cancel any already-enqueued jobs.
|
|
792
|
+
*/
|
|
793
|
+
removeCronJob: (id: number) => Promise<void>;
|
|
794
|
+
/**
|
|
795
|
+
* Pause a cron schedule. Paused schedules are skipped by `enqueueDueCronJobs()`.
|
|
796
|
+
*/
|
|
797
|
+
pauseCronJob: (id: number) => Promise<void>;
|
|
798
|
+
/**
|
|
799
|
+
* Resume a paused cron schedule.
|
|
800
|
+
*/
|
|
801
|
+
resumeCronJob: (id: number) => Promise<void>;
|
|
802
|
+
/**
|
|
803
|
+
* Edit an existing cron schedule. Only provided fields are updated.
|
|
804
|
+
* If `cronExpression` or `timezone` changes, `nextRunAt` is recalculated.
|
|
805
|
+
*/
|
|
806
|
+
editCronJob: (id: number, updates: EditCronScheduleOptions) => Promise<void>;
|
|
807
|
+
/**
|
|
808
|
+
* Check all active cron schedules and enqueue jobs for any whose
|
|
809
|
+
* `nextRunAt` has passed. When `allowOverlap` is false (the default),
|
|
810
|
+
* a new job is not enqueued if the previous instance is still
|
|
811
|
+
* pending, processing, or waiting.
|
|
812
|
+
*
|
|
813
|
+
* **Note:** The processor calls this automatically before each batch,
|
|
814
|
+
* so you typically do not need to call it yourself. It is exposed for
|
|
815
|
+
* manual use in tests or one-off scripts.
|
|
816
|
+
*
|
|
817
|
+
* @returns The number of jobs that were enqueued.
|
|
818
|
+
*/
|
|
819
|
+
enqueueDueCronJobs: () => Promise<number>;
|
|
679
820
|
/**
|
|
680
821
|
* Get the PostgreSQL database pool.
|
|
681
822
|
* Throws if the backend is not PostgreSQL.
|
|
@@ -723,6 +864,24 @@ interface JobUpdates {
|
|
|
723
864
|
timeoutMs?: number | null;
|
|
724
865
|
tags?: string[] | null;
|
|
725
866
|
}
|
|
867
|
+
/**
|
|
868
|
+
* Input shape for creating a cron schedule in the backend.
|
|
869
|
+
* This is the backend-level version of CronScheduleOptions.
|
|
870
|
+
*/
|
|
871
|
+
interface CronScheduleInput {
|
|
872
|
+
scheduleName: string;
|
|
873
|
+
cronExpression: string;
|
|
874
|
+
jobType: string;
|
|
875
|
+
payload: any;
|
|
876
|
+
maxAttempts: number;
|
|
877
|
+
priority: number;
|
|
878
|
+
timeoutMs: number | null;
|
|
879
|
+
forceKillOnTimeout: boolean;
|
|
880
|
+
tags: string[] | undefined;
|
|
881
|
+
timezone: string;
|
|
882
|
+
allowOverlap: boolean;
|
|
883
|
+
nextRunAt: Date | null;
|
|
884
|
+
}
|
|
726
885
|
/**
|
|
727
886
|
* Abstract backend interface that both PostgreSQL and Redis implement.
|
|
728
887
|
* All storage operations go through this interface so the processor
|
|
@@ -762,10 +921,10 @@ interface QueueBackend {
|
|
|
762
921
|
editJob(jobId: number, updates: JobUpdates): Promise<void>;
|
|
763
922
|
/** Edit all pending jobs matching filters. Returns count. */
|
|
764
923
|
editAllPendingJobs(filters: JobFilters | undefined, updates: JobUpdates): Promise<number>;
|
|
765
|
-
/** Delete completed jobs older than N days. Returns count deleted. */
|
|
766
|
-
cleanupOldJobs(daysToKeep?: number): Promise<number>;
|
|
767
|
-
/** Delete job events older than N days. Returns count deleted. */
|
|
768
|
-
cleanupOldJobEvents(daysToKeep?: number): Promise<number>;
|
|
924
|
+
/** Delete completed jobs older than N days. Deletes in batches for scale safety. Returns count deleted. */
|
|
925
|
+
cleanupOldJobs(daysToKeep?: number, batchSize?: number): Promise<number>;
|
|
926
|
+
/** Delete job events older than N days. Deletes in batches for scale safety. Returns count deleted. */
|
|
927
|
+
cleanupOldJobEvents(daysToKeep?: number, batchSize?: number): Promise<number>;
|
|
769
928
|
/** Reclaim jobs stuck in 'processing' for too long. Returns count. */
|
|
770
929
|
reclaimStuckJobs(maxProcessingTimeMinutes?: number): Promise<number>;
|
|
771
930
|
/** Update the progress percentage (0-100) for a job. */
|
|
@@ -774,6 +933,84 @@ interface QueueBackend {
|
|
|
774
933
|
recordJobEvent(jobId: number, eventType: JobEventType, metadata?: any): Promise<void>;
|
|
775
934
|
/** Get all events for a job, ordered by createdAt ASC. */
|
|
776
935
|
getJobEvents(jobId: number): Promise<JobEvent[]>;
|
|
936
|
+
/** Create a cron schedule and return its ID. */
|
|
937
|
+
addCronSchedule(input: CronScheduleInput): Promise<number>;
|
|
938
|
+
/** Get a cron schedule by ID, or null if not found. */
|
|
939
|
+
getCronSchedule(id: number): Promise<CronScheduleRecord | null>;
|
|
940
|
+
/** Get a cron schedule by its unique name, or null if not found. */
|
|
941
|
+
getCronScheduleByName(name: string): Promise<CronScheduleRecord | null>;
|
|
942
|
+
/** List cron schedules, optionally filtered by status. */
|
|
943
|
+
listCronSchedules(status?: CronScheduleStatus): Promise<CronScheduleRecord[]>;
|
|
944
|
+
/** Delete a cron schedule by ID. */
|
|
945
|
+
removeCronSchedule(id: number): Promise<void>;
|
|
946
|
+
/** Pause a cron schedule. */
|
|
947
|
+
pauseCronSchedule(id: number): Promise<void>;
|
|
948
|
+
/** Resume a cron schedule. */
|
|
949
|
+
resumeCronSchedule(id: number): Promise<void>;
|
|
950
|
+
/** Edit a cron schedule. */
|
|
951
|
+
editCronSchedule(id: number, updates: EditCronScheduleOptions, nextRunAt?: Date | null): Promise<void>;
|
|
952
|
+
/**
|
|
953
|
+
* Atomically fetch all active cron schedules whose nextRunAt <= now.
|
|
954
|
+
* In PostgreSQL this uses FOR UPDATE SKIP LOCKED to prevent duplicate enqueuing.
|
|
955
|
+
*/
|
|
956
|
+
getDueCronSchedules(): Promise<CronScheduleRecord[]>;
|
|
957
|
+
/**
|
|
958
|
+
* Update a cron schedule after a job has been enqueued.
|
|
959
|
+
* Sets lastEnqueuedAt, lastJobId, and advances nextRunAt.
|
|
960
|
+
*/
|
|
961
|
+
updateCronScheduleAfterEnqueue(id: number, lastEnqueuedAt: Date, lastJobId: number, nextRunAt: Date | null): Promise<void>;
|
|
962
|
+
/**
|
|
963
|
+
* Transition a job from 'processing' to 'waiting' status.
|
|
964
|
+
* Persists step data so the handler can resume from where it left off.
|
|
965
|
+
*
|
|
966
|
+
* @param jobId - The job to pause.
|
|
967
|
+
* @param options - Wait configuration including optional waitUntil date, token ID, and step data.
|
|
968
|
+
*/
|
|
969
|
+
waitJob(jobId: number, options: {
|
|
970
|
+
waitUntil?: Date;
|
|
971
|
+
waitTokenId?: string;
|
|
972
|
+
stepData: Record<string, any>;
|
|
973
|
+
}): Promise<void>;
|
|
974
|
+
/**
|
|
975
|
+
* Persist step data for a job. Called after each `ctx.run()` step completes
|
|
976
|
+
* to save intermediate progress. Best-effort: should not throw.
|
|
977
|
+
*
|
|
978
|
+
* @param jobId - The job to update.
|
|
979
|
+
* @param stepData - The step data to persist.
|
|
980
|
+
*/
|
|
981
|
+
updateStepData(jobId: number, stepData: Record<string, any>): Promise<void>;
|
|
982
|
+
/**
|
|
983
|
+
* Create a waitpoint token that can pause a job until an external signal completes it.
|
|
984
|
+
*
|
|
985
|
+
* @param jobId - The job ID to associate with the token (null if created outside a handler).
|
|
986
|
+
* @param options - Optional timeout string (e.g. '10m', '1h') and tags.
|
|
987
|
+
* @returns The created waitpoint with its unique ID.
|
|
988
|
+
*/
|
|
989
|
+
createWaitpoint(jobId: number | null, options?: CreateTokenOptions): Promise<{
|
|
990
|
+
id: string;
|
|
991
|
+
}>;
|
|
992
|
+
/**
|
|
993
|
+
* Complete a waitpoint token, optionally providing output data.
|
|
994
|
+
* Moves the associated job from 'waiting' back to 'pending' so it gets picked up.
|
|
995
|
+
*
|
|
996
|
+
* @param tokenId - The waitpoint token ID to complete.
|
|
997
|
+
* @param data - Optional data to pass to the waiting handler.
|
|
998
|
+
*/
|
|
999
|
+
completeWaitpoint(tokenId: string, data?: any): Promise<void>;
|
|
1000
|
+
/**
|
|
1001
|
+
* Retrieve a waitpoint token by its ID.
|
|
1002
|
+
*
|
|
1003
|
+
* @param tokenId - The waitpoint token ID to look up.
|
|
1004
|
+
* @returns The waitpoint record, or null if not found.
|
|
1005
|
+
*/
|
|
1006
|
+
getWaitpoint(tokenId: string): Promise<WaitpointRecord | null>;
|
|
1007
|
+
/**
|
|
1008
|
+
* Expire timed-out waitpoint tokens and move their associated jobs back to 'pending'.
|
|
1009
|
+
* Should be called periodically (e.g., alongside reclaimStuckJobs).
|
|
1010
|
+
*
|
|
1011
|
+
* @returns The number of tokens that were expired.
|
|
1012
|
+
*/
|
|
1013
|
+
expireTimedOutWaitpoints(): Promise<number>;
|
|
777
1014
|
/** Set a pending reason for unpicked jobs of a given type. */
|
|
778
1015
|
setPendingReasonForUnpickedJobs(reason: string, jobType?: string | string[]): Promise<void>;
|
|
779
1016
|
}
|
|
@@ -801,14 +1038,108 @@ declare class PostgresBackend implements QueueBackend {
|
|
|
801
1038
|
cancelAllUpcomingJobs(filters?: JobFilters): Promise<number>;
|
|
802
1039
|
editJob(jobId: number, updates: JobUpdates): Promise<void>;
|
|
803
1040
|
editAllPendingJobs(filters: JobFilters | undefined, updates: JobUpdates): Promise<number>;
|
|
804
|
-
|
|
805
|
-
|
|
1041
|
+
/**
|
|
1042
|
+
* Delete completed jobs older than the given number of days.
|
|
1043
|
+
* Deletes in batches of 1000 to avoid long-running transactions
|
|
1044
|
+
* and excessive WAL bloat at scale.
|
|
1045
|
+
*
|
|
1046
|
+
* @param daysToKeep - Number of days to retain completed jobs (default 30).
|
|
1047
|
+
* @param batchSize - Number of rows to delete per batch (default 1000).
|
|
1048
|
+
* @returns Total number of deleted jobs.
|
|
1049
|
+
*/
|
|
1050
|
+
cleanupOldJobs(daysToKeep?: number, batchSize?: number): Promise<number>;
|
|
1051
|
+
/**
|
|
1052
|
+
* Delete job events older than the given number of days.
|
|
1053
|
+
* Deletes in batches of 1000 to avoid long-running transactions
|
|
1054
|
+
* and excessive WAL bloat at scale.
|
|
1055
|
+
*
|
|
1056
|
+
* @param daysToKeep - Number of days to retain events (default 30).
|
|
1057
|
+
* @param batchSize - Number of rows to delete per batch (default 1000).
|
|
1058
|
+
* @returns Total number of deleted events.
|
|
1059
|
+
*/
|
|
1060
|
+
cleanupOldJobEvents(daysToKeep?: number, batchSize?: number): Promise<number>;
|
|
806
1061
|
reclaimStuckJobs(maxProcessingTimeMinutes?: number): Promise<number>;
|
|
807
1062
|
/**
|
|
808
1063
|
* Batch-insert multiple job events in a single query.
|
|
809
1064
|
* More efficient than individual recordJobEvent calls.
|
|
810
1065
|
*/
|
|
811
1066
|
private recordJobEventsBatch;
|
|
1067
|
+
/** Create a cron schedule and return its ID. */
|
|
1068
|
+
addCronSchedule(input: CronScheduleInput): Promise<number>;
|
|
1069
|
+
/** Get a cron schedule by ID. */
|
|
1070
|
+
getCronSchedule(id: number): Promise<CronScheduleRecord | null>;
|
|
1071
|
+
/** Get a cron schedule by its unique name. */
|
|
1072
|
+
getCronScheduleByName(name: string): Promise<CronScheduleRecord | null>;
|
|
1073
|
+
/** List cron schedules, optionally filtered by status. */
|
|
1074
|
+
listCronSchedules(status?: CronScheduleStatus): Promise<CronScheduleRecord[]>;
|
|
1075
|
+
/** Delete a cron schedule by ID. */
|
|
1076
|
+
removeCronSchedule(id: number): Promise<void>;
|
|
1077
|
+
/** Pause a cron schedule. */
|
|
1078
|
+
pauseCronSchedule(id: number): Promise<void>;
|
|
1079
|
+
/** Resume a paused cron schedule. */
|
|
1080
|
+
resumeCronSchedule(id: number): Promise<void>;
|
|
1081
|
+
/** Edit a cron schedule. */
|
|
1082
|
+
editCronSchedule(id: number, updates: EditCronScheduleOptions, nextRunAt?: Date | null): Promise<void>;
|
|
1083
|
+
/**
|
|
1084
|
+
* Atomically fetch all active cron schedules whose nextRunAt <= NOW().
|
|
1085
|
+
* Uses FOR UPDATE SKIP LOCKED to prevent duplicate enqueuing across workers.
|
|
1086
|
+
*/
|
|
1087
|
+
getDueCronSchedules(): Promise<CronScheduleRecord[]>;
|
|
1088
|
+
/**
|
|
1089
|
+
* Update a cron schedule after a job has been enqueued.
|
|
1090
|
+
* Sets lastEnqueuedAt, lastJobId, and advances nextRunAt.
|
|
1091
|
+
*/
|
|
1092
|
+
updateCronScheduleAfterEnqueue(id: number, lastEnqueuedAt: Date, lastJobId: number, nextRunAt: Date | null): Promise<void>;
|
|
1093
|
+
/**
|
|
1094
|
+
* Transition a job from 'processing' to 'waiting' status.
|
|
1095
|
+
* Persists step data so the handler can resume from where it left off.
|
|
1096
|
+
*
|
|
1097
|
+
* @param jobId - The job to pause.
|
|
1098
|
+
* @param options - Wait configuration including optional waitUntil date, token ID, and step data.
|
|
1099
|
+
*/
|
|
1100
|
+
waitJob(jobId: number, options: {
|
|
1101
|
+
waitUntil?: Date;
|
|
1102
|
+
waitTokenId?: string;
|
|
1103
|
+
stepData: Record<string, any>;
|
|
1104
|
+
}): Promise<void>;
|
|
1105
|
+
/**
|
|
1106
|
+
* Persist step data for a job. Called after each ctx.run() step completes.
|
|
1107
|
+
* Best-effort: does not throw to avoid killing the running handler.
|
|
1108
|
+
*
|
|
1109
|
+
* @param jobId - The job to update.
|
|
1110
|
+
* @param stepData - The step data to persist.
|
|
1111
|
+
*/
|
|
1112
|
+
updateStepData(jobId: number, stepData: Record<string, any>): Promise<void>;
|
|
1113
|
+
/**
|
|
1114
|
+
* Create a waitpoint token in the database.
|
|
1115
|
+
*
|
|
1116
|
+
* @param jobId - The job ID to associate with the token (null if created outside a handler).
|
|
1117
|
+
* @param options - Optional timeout string (e.g. '10m', '1h') and tags.
|
|
1118
|
+
* @returns The created waitpoint with its unique ID.
|
|
1119
|
+
*/
|
|
1120
|
+
createWaitpoint(jobId: number | null, options?: CreateTokenOptions): Promise<{
|
|
1121
|
+
id: string;
|
|
1122
|
+
}>;
|
|
1123
|
+
/**
|
|
1124
|
+
* Complete a waitpoint token and move the associated job back to 'pending'.
|
|
1125
|
+
*
|
|
1126
|
+
* @param tokenId - The waitpoint token ID to complete.
|
|
1127
|
+
* @param data - Optional data to pass to the waiting handler.
|
|
1128
|
+
*/
|
|
1129
|
+
completeWaitpoint(tokenId: string, data?: any): Promise<void>;
|
|
1130
|
+
/**
|
|
1131
|
+
* Retrieve a waitpoint token by its ID.
|
|
1132
|
+
*
|
|
1133
|
+
* @param tokenId - The waitpoint token ID to look up.
|
|
1134
|
+
* @returns The waitpoint record, or null if not found.
|
|
1135
|
+
*/
|
|
1136
|
+
getWaitpoint(tokenId: string): Promise<WaitpointRecord | null>;
|
|
1137
|
+
/**
|
|
1138
|
+
* Expire timed-out waitpoint tokens and move their associated jobs back to 'pending'.
|
|
1139
|
+
*
|
|
1140
|
+
* @returns The number of tokens that were expired.
|
|
1141
|
+
*/
|
|
1142
|
+
expireTimedOutWaitpoints(): Promise<number>;
|
|
812
1143
|
setPendingReasonForUnpickedJobs(reason: string, jobType?: string | string[]): Promise<void>;
|
|
813
1144
|
}
|
|
814
1145
|
|
|
@@ -863,6 +1194,25 @@ declare function testHandlerSerialization<PayloadMap, T extends keyof PayloadMap
|
|
|
863
1194
|
error?: string;
|
|
864
1195
|
}>;
|
|
865
1196
|
|
|
1197
|
+
/**
|
|
1198
|
+
* Calculate the next occurrence of a cron expression after a given date.
|
|
1199
|
+
*
|
|
1200
|
+
* @param cronExpression - A standard cron expression (5 fields, e.g. "0 * * * *").
|
|
1201
|
+
* @param timezone - IANA timezone string (default: "UTC").
|
|
1202
|
+
* @param after - The reference date to compute the next run from (default: now).
|
|
1203
|
+
* @param CronImpl - Cron class for dependency injection (default: croner's Cron).
|
|
1204
|
+
* @returns The next occurrence as a Date, or null if the expression will never fire again.
|
|
1205
|
+
*/
|
|
1206
|
+
declare function getNextCronOccurrence(cronExpression: string, timezone?: string, after?: Date, CronImpl?: typeof Cron): Date | null;
|
|
1207
|
+
/**
|
|
1208
|
+
* Validate whether a string is a syntactically correct cron expression.
|
|
1209
|
+
*
|
|
1210
|
+
* @param cronExpression - The cron expression to validate.
|
|
1211
|
+
* @param CronImpl - Cron class for dependency injection (default: croner's Cron).
|
|
1212
|
+
* @returns True if the expression is valid, false otherwise.
|
|
1213
|
+
*/
|
|
1214
|
+
declare function validateCronExpression(cronExpression: string, CronImpl?: typeof Cron): boolean;
|
|
1215
|
+
|
|
866
1216
|
/**
|
|
867
1217
|
* Initialize the job queue system.
|
|
868
1218
|
*
|
|
@@ -870,4 +1220,4 @@ declare function testHandlerSerialization<PayloadMap, T extends keyof PayloadMap
|
|
|
870
1220
|
*/
|
|
871
1221
|
declare const initJobQueue: <PayloadMap = any>(config: JobQueueConfig) => JobQueue<PayloadMap>;
|
|
872
1222
|
|
|
873
|
-
export { type CreateTokenOptions, type DatabaseSSLConfig, type EditJobOptions, FailureReason, type JobContext, type JobEvent, JobEventType, type JobHandler, type JobHandlers, type JobOptions, type JobQueue, type JobQueueConfig, type JobQueueConfigLegacy, type JobRecord, type JobStatus, type JobType, type OnTimeoutCallback, PostgresBackend, type PostgresJobQueueConfig, type Processor, type ProcessorOptions, type QueueBackend, type RedisJobQueueConfig, type RedisTLSConfig, type TagQueryMode, type WaitDuration, WaitSignal, type WaitToken, type WaitTokenResult, type WaitpointRecord, type WaitpointStatus, initJobQueue, testHandlerSerialization, validateHandlerSerializable };
|
|
1223
|
+
export { type CreateTokenOptions, type CronScheduleInput, type CronScheduleOptions, type CronScheduleRecord, type CronScheduleStatus, type DatabaseSSLConfig, type EditCronScheduleOptions, type EditJobOptions, FailureReason, type JobContext, type JobEvent, JobEventType, type JobHandler, type JobHandlers, type JobOptions, type JobQueue, type JobQueueConfig, type JobQueueConfigLegacy, type JobRecord, type JobStatus, type JobType, type OnTimeoutCallback, PostgresBackend, type PostgresJobQueueConfig, type Processor, type ProcessorOptions, type QueueBackend, type RedisJobQueueConfig, type RedisTLSConfig, type TagQueryMode, type WaitDuration, WaitSignal, type WaitToken, type WaitTokenResult, type WaitpointRecord, type WaitpointStatus, getNextCronOccurrence, initJobQueue, testHandlerSerialization, validateCronExpression, validateHandlerSerializable };
|