workmatic 1.0.5 → 1.1.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.d.ts CHANGED
@@ -1,11 +1,12 @@
1
- import * as http from 'http';
2
1
  import * as better_sqlite3 from 'better-sqlite3';
2
+ import better_sqlite3__default from 'better-sqlite3';
3
+ import * as http from 'http';
3
4
  import { Kysely, Generated } from 'kysely';
4
5
 
5
6
  /**
6
7
  * Job status types
7
8
  */
8
- type JobStatus = 'ready' | 'running' | 'done' | 'failed' | 'dead';
9
+ type JobStatus = 'ready' | 'running' | 'done' | 'dead';
9
10
  /**
10
11
  * Database table schema for workmatic_jobs
11
12
  */
@@ -63,7 +64,7 @@ interface Job<TPayload = unknown> {
63
64
  maxAttempts: number;
64
65
  /** When the job was created (unix ms) */
65
66
  createdAt: number;
66
- /** Last error message if failed */
67
+ /** Last error from a previous attempt (e.g. before retry) */
67
68
  lastError: string | null;
68
69
  }
69
70
  /**
@@ -84,6 +85,13 @@ interface AddJobResult {
84
85
  ok: true;
85
86
  id: string;
86
87
  }
88
+ /**
89
+ * Result of adding multiple jobs in one transaction
90
+ */
91
+ interface AddManyResult {
92
+ ok: true;
93
+ ids: string[];
94
+ }
87
95
  /**
88
96
  * Options for creating a database
89
97
  */
@@ -124,7 +132,7 @@ interface WorkerOptions {
124
132
  leaseMs?: number;
125
133
  /** Poll interval in ms when no jobs available. Default: 1000 */
126
134
  pollMs?: number;
127
- /** Job execution timeout in ms. Default: undefined (no timeout) */
135
+ /** Job execution timeout in ms. Default: 60000 (1 min). Set to 0 to disable. */
128
136
  timeoutMs?: number;
129
137
  /** Backoff function for retries. Default: exponential */
130
138
  backoff?: BackoffFunction;
@@ -132,6 +140,18 @@ interface WorkerOptions {
132
140
  persistState?: boolean;
133
141
  /** Auto-restore worker state on creation. Default: true (only applies if persistState is true) */
134
142
  autoRestore?: boolean;
143
+ /**
144
+ * Minimum interval between database pause checks (CLI `pause`). Reduces round-trips while pumping.
145
+ * Default: 300 ms
146
+ */
147
+ pauseCheckIntervalMs?: number;
148
+ /**
149
+ * Minimum interval between lease requeue scans. Default: every pump (0).
150
+ * Set to e.g. 1000 to run expired-lease recovery at most once per second.
151
+ */
152
+ requeueExpiredIntervalMs?: number;
153
+ /** Called when the pump loop catches an error (after optional default logging) */
154
+ onPumpError?: (error: unknown) => void;
135
155
  }
136
156
  /**
137
157
  * Job processor function type
@@ -144,7 +164,6 @@ interface JobStats {
144
164
  ready: number;
145
165
  running: number;
146
166
  done: number;
147
- failed: number;
148
167
  dead: number;
149
168
  total: number;
150
169
  }
@@ -154,6 +173,11 @@ interface JobStats {
154
173
  interface WorkmaticClient {
155
174
  /** Add a job to the queue */
156
175
  add<TPayload = unknown>(payload: TPayload, options?: AddJobOptions): Promise<AddJobResult>;
176
+ /**
177
+ * Add many jobs in a single transaction (shared priority, delay, maxAttempts).
178
+ * Faster than repeated `add()` when inserting large batches.
179
+ */
180
+ addMany<TPayload = unknown>(payloads: TPayload[], options?: AddJobOptions): Promise<AddManyResult>;
157
181
  /** Get job statistics */
158
182
  stats(): Promise<JobStats>;
159
183
  /** Clear all jobs from the queue */
@@ -225,6 +249,78 @@ interface WorkmaticDashboard {
225
249
  /** Server port */
226
250
  readonly port: number;
227
251
  }
252
+ /**
253
+ * Options for creating an orchestrator
254
+ */
255
+ interface OrchestratorOptions {
256
+ /** Kysely database instance */
257
+ db: WorkmaticDb;
258
+ }
259
+ /**
260
+ * Options when registering a queue with the orchestrator
261
+ */
262
+ interface RegisterQueueOptions {
263
+ /** Create a worker for this queue (omit for client-only) */
264
+ worker?: Omit<WorkerOptions, 'db' | 'queue'>;
265
+ }
266
+ /**
267
+ * Options for bulk transfer between queues
268
+ */
269
+ interface TransferOptions {
270
+ /** Source queue name */
271
+ from: string;
272
+ /** Target queue name */
273
+ to: string;
274
+ /**
275
+ * Job statuses to transfer. Default: `['ready', 'dead']`.
276
+ * `running` and `done` are excluded by default for safety.
277
+ */
278
+ status?: JobStatus | JobStatus[];
279
+ /** Max jobs to move (default: 10000) */
280
+ limit?: number;
281
+ /** When moving `dead` jobs, reset to `ready` with attempts cleared (retry queue) */
282
+ resetForRetry?: boolean;
283
+ }
284
+ /**
285
+ * Result of a bulk transfer
286
+ */
287
+ interface TransferResult {
288
+ moved: number;
289
+ }
290
+ /**
291
+ * Options for moving a single job by public id
292
+ */
293
+ interface MoveJobOptions {
294
+ /** Allowed source statuses (default: ready, dead) */
295
+ status?: JobStatus | JobStatus[];
296
+ /** Reset dead job for retry in target queue */
297
+ resetForRetry?: boolean;
298
+ }
299
+ /**
300
+ * Multi-queue orchestrator interface
301
+ */
302
+ interface WorkmaticOrchestrator {
303
+ /** Register a queue (client + optional worker) */
304
+ register(queue: string, options?: RegisterQueueOptions): WorkmaticClient;
305
+ /** Get client for a queue (lazy-creates if not registered) */
306
+ client(queue: string): WorkmaticClient;
307
+ /** Get worker for a queue (must be registered with worker option) */
308
+ worker(queue: string): WorkmaticWorker;
309
+ /** All registered workers (for dashboard) */
310
+ workers(): WorkmaticWorker[];
311
+ /** Queue names from DB and registry */
312
+ queues(): Promise<string[]>;
313
+ /** Set processor on a registered worker */
314
+ process<TPayload = unknown>(queue: string, fn: JobProcessor<TPayload>): void;
315
+ startAll(): void;
316
+ stopAll(): Promise<void>;
317
+ pause(queue: string): Promise<void>;
318
+ resume(queue: string): Promise<void>;
319
+ isPaused(queue: string): Promise<boolean>;
320
+ stats(queue?: string): Promise<Record<string, JobStats>>;
321
+ transfer(options: TransferOptions): Promise<TransferResult>;
322
+ moveJob(publicId: string, toQueue: string, options?: MoveJobOptions): Promise<void>;
323
+ }
228
324
  /**
229
325
  * Internal job representation from database
230
326
  */
@@ -235,6 +331,9 @@ interface ClaimedJob {
235
331
  payload: string;
236
332
  attempts: number;
237
333
  max_attempts: number;
334
+ priority: number;
335
+ created_at: number;
336
+ last_error: string | null;
238
337
  }
239
338
 
240
339
  /**
@@ -258,6 +357,12 @@ interface ClaimedJob {
258
357
  * ```
259
358
  */
260
359
  declare function createDatabase(options?: DatabaseOptions): WorkmaticDb;
360
+ /**
361
+ * Get the underlying better-sqlite3 database instance from a Kysely instance
362
+ * created with {@link createDatabase}. For manually constructed `Kysely` instances,
363
+ * falls back to reading the dialect adapter (may break across Kysely versions).
364
+ */
365
+ declare function getUnderlyingDb(db: WorkmaticDb): better_sqlite3__default.Database;
261
366
 
262
367
  /**
263
368
  * Create a job queue client for adding jobs
@@ -286,6 +391,8 @@ declare function createDatabase(options?: DatabaseOptions): WorkmaticDb;
286
391
  */
287
392
  declare function createClient(options: ClientOptions): WorkmaticClient;
288
393
 
394
+ /** Default job execution timeout when `timeoutMs` is omitted (1 minute). Use `timeoutMs: 0` for no limit. */
395
+ declare const DEFAULT_WORKER_TIMEOUT_MS = 60000;
289
396
  /**
290
397
  * Create a job queue worker for processing jobs
291
398
  *
@@ -311,6 +418,11 @@ declare function createClient(options: ClientOptions): WorkmaticClient;
311
418
  */
312
419
  declare function createWorker(options: WorkerOptions): WorkmaticWorker;
313
420
 
421
+ /**
422
+ * Create a multi-queue orchestrator for registration, lifecycle, and transfers.
423
+ */
424
+ declare function createOrchestrator(options: OrchestratorOptions): WorkmaticOrchestrator;
425
+
314
426
  /**
315
427
  * Create a dashboard HTTP server for monitoring and controlling the job queue
316
428
  *
@@ -383,4 +495,4 @@ declare const defaultBackoff: BackoffFunction;
383
495
  */
384
496
  declare function validatePayload(payload: unknown): string;
385
497
 
386
- export { type AddJobOptions, type AddJobResult, type BackoffFunction, type ClaimedJob, type ClientOptions, type DashboardMiddleware, type DashboardMiddlewareOptions, type DashboardOptions, type DatabaseOptions, type Job, type JobProcessor, type JobStats, type JobStatus, type WorkerOptions, type WorkerState, type WorkmaticClient, type WorkmaticDashboard, type WorkmaticDatabase, type WorkmaticDb, type WorkmaticJobsTable, type WorkmaticWorker, createClient, createDashboard, createDashboardMiddleware, createDatabase, createWorker, defaultBackoff, validatePayload };
498
+ export { type AddJobOptions, type AddJobResult, type AddManyResult, type BackoffFunction, type ClaimedJob, type ClientOptions, DEFAULT_WORKER_TIMEOUT_MS, type DashboardMiddleware, type DashboardMiddlewareOptions, type DashboardOptions, type DatabaseOptions, type Job, type JobProcessor, type JobStats, type JobStatus, type MoveJobOptions, type OrchestratorOptions, type RegisterQueueOptions, type TransferOptions, type TransferResult, type WorkerOptions, type WorkerState, type WorkmaticClient, type WorkmaticDashboard, type WorkmaticDatabase, type WorkmaticDb, type WorkmaticJobsTable, type WorkmaticOrchestrator, type WorkmaticWorker, createClient, createDashboard, createDashboardMiddleware, createDatabase, createOrchestrator, createWorker, defaultBackoff, getUnderlyingDb, validatePayload };