bunderstack 0.24.6 → 0.25.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/CHANGELOG.md +19 -0
- package/dist/index.d.ts +1 -1
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js.map +1 -1
- package/dist/jobs/define.d.ts +43 -4
- package/dist/jobs/define.d.ts.map +1 -1
- package/dist/jobs/define.js +13 -0
- package/dist/jobs/define.js.map +1 -1
- package/dist/jobs/index.d.ts +2 -2
- package/dist/jobs/index.d.ts.map +1 -1
- package/dist/jobs/index.js +1 -1
- package/dist/jobs/index.js.map +1 -1
- package/dist/jobs/queue.d.ts +8 -0
- package/dist/jobs/queue.d.ts.map +1 -1
- package/dist/jobs/queue.js +25 -1
- package/dist/jobs/queue.js.map +1 -1
- package/dist/jobs/worker.d.ts.map +1 -1
- package/dist/jobs/worker.js +10 -2
- package/dist/jobs/worker.js.map +1 -1
- package/dist/runtime.d.ts +2 -2
- package/dist/runtime.d.ts.map +1 -1
- package/dist/runtime.js +3 -2
- package/dist/runtime.js.map +1 -1
- package/llms-full.txt +101 -0
- package/llms.txt +21 -0
- package/package.json +1 -1
package/CHANGELOG.md
CHANGED
|
@@ -2,6 +2,25 @@
|
|
|
2
2
|
|
|
3
3
|
All notable changes to `bunderstack` will be documented in this file.
|
|
4
4
|
|
|
5
|
+
## [0.25.0] — 2026-09-25
|
|
6
|
+
|
|
7
|
+
### Added
|
|
8
|
+
|
|
9
|
+
- `jobs.enqueue(name, input, { tx })` inserts the job row through the caller's
|
|
10
|
+
Drizzle transaction on `app.jobs`, `context.jobs`, job handler `ctx.jobs`,
|
|
11
|
+
and test fixtures. The job becomes visible to workers only when the
|
|
12
|
+
transaction commits, and a rollback removes it, so applications no longer
|
|
13
|
+
need an outbox table to schedule work atomically with their writes. `app.jobs`
|
|
14
|
+
types `tx` as the app's `BunderstackTx`; a transaction of the other database
|
|
15
|
+
dialect throws. Delivery stays at-least-once.
|
|
16
|
+
- `dedupeUntil: 'start' | 'finish'` on `j.job()` declarations. The default
|
|
17
|
+
`'finish'` keeps today's behavior: a `dedupeKey` collapses enqueues until the
|
|
18
|
+
job reaches a terminal state. `'start'` releases the key when a worker claims
|
|
19
|
+
the job, so an enqueue during the run schedules one more run that reads the
|
|
20
|
+
newer state. Cron declarations reject `dedupeUntil`.
|
|
21
|
+
- Export the `DedupeUntil`, `EnqueueTransaction`, and `TypedEnqueueOptions`
|
|
22
|
+
types.
|
|
23
|
+
|
|
5
24
|
## [0.24.6] — 2026-09-21
|
|
6
25
|
|
|
7
26
|
### Fixed
|
package/dist/index.d.ts
CHANGED
|
@@ -18,7 +18,7 @@ export { buildReadinessReport, readinessReportSchema } from './readiness.js';
|
|
|
18
18
|
export type { ReadinessCheck, ReadinessCheckName, ReadinessCode, ReadinessReport, ReadinessStatus, } from './readiness.js';
|
|
19
19
|
export type { EmailMessage, EmailAdapter } from './email.js';
|
|
20
20
|
export { createJobsBuilder } from './jobs/index.js';
|
|
21
|
-
export type { BunderstackJobContext, BunderstackJobsBuilder, BackgroundDefinition, BackgroundDefs, CronDefinition, CronInvocation, EnqueueOptions, JobContext, JobDefinition, JobsDefs, JobsFacade, JobsRuntimeFacade, QueueJobDefinition, QueueJobKeys, RunWorkerOptions, StartWorkerOptions, WorkerHandle, } from './jobs/index.js';
|
|
21
|
+
export type { BunderstackJobContext, BunderstackJobsBuilder, BackgroundDefinition, BackgroundDefs, CronDefinition, CronInvocation, DedupeUntil, EnqueueOptions, EnqueueTransaction, JobContext, JobDefinition, JobsDefs, JobsFacade, JobsRuntimeFacade, QueueJobDefinition, QueueJobKeys, TypedEnqueueOptions, RunWorkerOptions, StartWorkerOptions, WorkerHandle, } from './jobs/index.js';
|
|
22
22
|
export { defineSessionUser, defineAccess, validateAndResolveAccess, checkAccess, AUTH_TABLE_NAMES, } from './access.js';
|
|
23
23
|
export type { TableAccessInput, OperationRule, AccessContext, AccessUser, AccessUserBase, SessionUserConfig, SessionUserExtraOf, SessionUserSource, } from './access.js';
|
|
24
24
|
export { typeid, generate as generateTypeId, parse as parseTypeId, asTypeId, } from './typeid.js';
|
package/dist/index.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,cAAc,WAAW,CAAA;AACzB,OAAO,EAAE,eAAe,EAAE,MAAM,EAAE,WAAW,EAAE,QAAQ,EAAE,MAAM,aAAa,CAAA;AAC5E,YAAY,EACV,eAAe,EACf,mBAAmB,EACnB,eAAe,EACf,gBAAgB,EAChB,eAAe,GAChB,MAAM,aAAa,CAAA;AACpB,YAAY,EACV,mBAAmB,EACnB,qBAAqB,EACrB,YAAY,EACZ,aAAa,EACb,cAAc,EACd,aAAa,GACd,MAAM,WAAW,CAAA;AAClB,OAAO,EAAE,QAAQ,EAAE,MAAM,iBAAiB,CAAA;AAC1C,YAAY,EAAE,eAAe,EAAE,MAAM,iBAAiB,CAAA;AACtD,YAAY,EAAE,aAAa,EAAE,aAAa,EAAE,MAAM,MAAM,CAAA;AACxD,OAAO,EAAE,cAAc,EAAE,MAAM,cAAc,CAAA;AAC7C,OAAO,EAAE,gBAAgB,EAAE,MAAM,UAAU,CAAA;AAC3C,YAAY,EAAE,oBAAoB,EAAE,MAAM,UAAU,CAAA;AACpD,OAAO,EAAE,aAAa,EAAE,iBAAiB,EAAE,UAAU,EAAE,MAAM,UAAU,CAAA;AACvE,YAAY,EACV,iBAAiB,EACjB,iBAAiB,EACjB,eAAe,EACf,gBAAgB,EAChB,iBAAiB,EACjB,cAAc,GACf,MAAM,UAAU,CAAA;AACjB,OAAO,EAAE,WAAW,EAAE,eAAe,EAAE,mBAAmB,EAAE,MAAM,OAAO,CAAA;AACzE,YAAY,EAAE,cAAc,EAAE,OAAO,EAAE,YAAY,EAAE,MAAM,OAAO,CAAA;AAClE,OAAO,EAAE,aAAa,EAAE,aAAa,EAAE,MAAM,YAAY,CAA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AAE,MAAM,eAAe,CAAA"}
|
package/dist/index.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,cAAc,WAAW,CAAA;AACzB,OAAO,EAAE,eAAe,EAAE,MAAM,EAAE,WAAW,EAAE,QAAQ,EAAE,MAAM,aAAa,CAAA;AAgB5E,OAAO,EAAE,QAAQ,EAAE,MAAM,iBAAiB,CAAA;AAG1C,OAAO,EAAE,cAAc,EAAE,MAAM,cAAc,CAAA;AAC7C,OAAO,EAAE,gBAAgB,EAAE,MAAM,UAAU,CAAA;AAE3C,OAAO,EAAE,aAAa,EAAE,iBAAiB,EAAE,UAAU,EAAE,MAAM,UAAU,CAAA;AASvE,OAAO,EAAE,WAAW,EAAE,eAAe,EAAE,mBAAmB,EAAE,MAAM,OAAO,CAAA;AAEzE,OAAO,EAAE,aAAa,EAAE,aAAa,EAAE,MAAM,YAAY,CAAA;AAOzD,OAAO,EAAE,oBAAoB,EAAE,qBAAqB,EAAE,MAAM,aAAa,CAAA;AASzE,OAAO,EAAE,iBAAiB,EAAE,MAAM,cAAc,CAAA;
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,cAAc,WAAW,CAAA;AACzB,OAAO,EAAE,eAAe,EAAE,MAAM,EAAE,WAAW,EAAE,QAAQ,EAAE,MAAM,aAAa,CAAA;AAgB5E,OAAO,EAAE,QAAQ,EAAE,MAAM,iBAAiB,CAAA;AAG1C,OAAO,EAAE,cAAc,EAAE,MAAM,cAAc,CAAA;AAC7C,OAAO,EAAE,gBAAgB,EAAE,MAAM,UAAU,CAAA;AAE3C,OAAO,EAAE,aAAa,EAAE,iBAAiB,EAAE,UAAU,EAAE,MAAM,UAAU,CAAA;AASvE,OAAO,EAAE,WAAW,EAAE,eAAe,EAAE,mBAAmB,EAAE,MAAM,OAAO,CAAA;AAEzE,OAAO,EAAE,aAAa,EAAE,aAAa,EAAE,MAAM,YAAY,CAAA;AAOzD,OAAO,EAAE,oBAAoB,EAAE,qBAAqB,EAAE,MAAM,aAAa,CAAA;AASzE,OAAO,EAAE,iBAAiB,EAAE,MAAM,cAAc,CAAA;AAuBhD,OAAO,EACL,iBAAiB,EACjB,YAAY,EACZ,wBAAwB,EACxB,WAAW,EACX,gBAAgB,GACjB,MAAM,UAAU,CAAA;AAWjB,OAAO,EACL,MAAM,EACN,QAAQ,IAAI,cAAc,EAC1B,KAAK,IAAI,WAAW,EACpB,QAAQ,GACT,MAAM,UAAU,CAAA;AAejB,OAAO,EAAE,oBAAoB,EAAE,MAAM,mBAAmB,CAAA;AAMxD,OAAO,EAAE,gBAAgB,EAAE,SAAS,EAAE,MAAM,eAAe,CAAA;AAU3D,OAAO,EACL,gBAAgB,EAChB,qBAAqB,EACrB,gBAAgB,EAChB,2BAA2B,GAC5B,MAAM,gBAAgB,CAAA;AACvB,OAAO,EAAE,iBAAiB,EAAE,MAAM,eAAe,CAAA","sourcesContent":["export * from './backend'\nexport { createMessaging, resend, customEmail, telegram } from './messaging'\nexport type {\n MessagingConfig,\n MessagingDescriptor,\n MessagingFacade,\n MessagingFacades,\n TelegramMessage,\n} from './messaging'\nexport type {\n AppRunWorkerOptions,\n AppStartWorkerOptions,\n AuthInstance,\n BucketNamesOf,\n BunderstackApp,\n StorageFacade,\n} from './runtime'\nexport { listSpec } from './api/list-spec'\nexport type { ListSpecOptions } from './api/list-spec'\nexport type { BunderstackDb, BunderstackTx } from './db'\nexport { MAX_LIST_LIMIT } from './list-query'\nexport { BunderstackError } from './errors'\nexport type { BunderstackErrorCode } from './errors'\nexport { resolveConfig, resolveAuthConfig, defineAuth } from './config'\nexport type {\n AuthConfigContext,\n AuthConfigFactory,\n AuthConfigInput,\n BetterAuthConfig,\n BunderstackConfig,\n ResolvedConfig,\n} from './config'\nexport { validateEnv, createClientEnv, BunderstackEnvError } from './env'\nexport type { EnvConfigInput, BaseEnv, ValidatedEnv } from './env'\nexport { buildManifest, parseManifest } from './manifest'\nexport type {\n ApiOperation,\n ApiOperationEffect,\n BunderstackManifest,\n ManifestEnvVar,\n} from './manifest'\nexport { buildReadinessReport, readinessReportSchema } from './readiness'\nexport type {\n ReadinessCheck,\n ReadinessCheckName,\n ReadinessCode,\n ReadinessReport,\n ReadinessStatus,\n} from './readiness'\nexport type { EmailMessage, EmailAdapter } from './email'\nexport { createJobsBuilder } from './jobs/index'\nexport type {\n BunderstackJobContext,\n BunderstackJobsBuilder,\n BackgroundDefinition,\n BackgroundDefs,\n CronDefinition,\n CronInvocation,\n DedupeUntil,\n EnqueueOptions,\n EnqueueTransaction,\n JobContext,\n JobDefinition,\n JobsDefs,\n JobsFacade,\n JobsRuntimeFacade,\n QueueJobDefinition,\n QueueJobKeys,\n TypedEnqueueOptions,\n RunWorkerOptions,\n StartWorkerOptions,\n WorkerHandle,\n} from './jobs/index'\nexport {\n defineSessionUser,\n defineAccess,\n validateAndResolveAccess,\n checkAccess,\n AUTH_TABLE_NAMES,\n} from './access'\nexport type {\n TableAccessInput,\n OperationRule,\n AccessContext,\n AccessUser,\n AccessUserBase,\n SessionUserConfig,\n SessionUserExtraOf,\n SessionUserSource,\n} from './access'\nexport {\n typeid,\n generate as generateTypeId,\n parse as parseTypeId,\n asTypeId,\n} from './typeid'\nexport type { TypeId } from './typeid'\nexport type {\n DatabaseAdapter,\n DatabaseConnection,\n DatabaseConnectionResult,\n} from './database/adapter'\nexport type { StorageAdapter } from './storage/index'\nexport type {\n StorageConfigInput,\n BucketConfigInput,\n ResolvedBucket,\n} from './storage/buckets'\nexport type { TransformSpec } from './storage/thumbnails'\nexport type { RealtimeAction } from './realtime/publisher'\nexport { createRealtimeFacade } from './realtime/facade'\nexport type {\n RealtimeFacade,\n RealtimeTransport,\n SchemaTable,\n} from './realtime/facade'\nexport { createApiBuilder, defineApi } from './api/builder'\nexport type { BunderstackApiBuilder, ApiFactory } from './api/builder'\nexport type { ApiContext } from './api/context'\nexport type {\n CrudApiRouterFor,\n ExposedApiTables,\n MergeApiRouterTypes,\n UnifiedApiRouter,\n} from './api/types'\nexport type { TableCrudProcedures } from './api/crud-router'\nexport {\n buildApiRegistry,\n mergeApiRoutersStrict,\n normalizeApiPath,\n normalizeForeignOpenAPISpec,\n} from './api/registry'\nexport { mergeOpenAPISpecs } from './api/openapi'\n"]}
|
package/dist/jobs/define.d.ts
CHANGED
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
import type { StandardSchemaV1 } from '@standard-schema/spec';
|
|
2
|
-
import type { DbFor } from '../db.js';
|
|
2
|
+
import type { BunderstackTx, DbFor } from '../db.js';
|
|
3
|
+
import type { AnyDb } from '../dialect.js';
|
|
3
4
|
import type { MessagingConfig, MessagingFacadesFor } from '../messaging/index.js';
|
|
4
5
|
import type { StorageFacade } from '../runtime.js';
|
|
5
6
|
import { type CatchUp } from './slots.js';
|
|
@@ -16,14 +17,45 @@ export type BackgroundTiming = {
|
|
|
16
17
|
timeout?: number;
|
|
17
18
|
};
|
|
18
19
|
export declare function leaseDurationFor<T extends BackgroundTiming>(def: T): number;
|
|
20
|
+
/**
|
|
21
|
+
* Structural view of a Drizzle database or transaction handle that `enqueue`
|
|
22
|
+
* can insert through. The loose runtime facade accepts any handle of the app's
|
|
23
|
+
* dialect; `app.jobs` narrows it to the app's `BunderstackTx`.
|
|
24
|
+
*/
|
|
25
|
+
export type EnqueueTransaction = AnyDb;
|
|
19
26
|
export type EnqueueOptions = {
|
|
20
|
-
/**
|
|
27
|
+
/**
|
|
28
|
+
* Collapse duplicate enqueues while the queue row holds its key: until a
|
|
29
|
+
* terminal state by default, or until it is claimed with
|
|
30
|
+
* `dedupeUntil: 'start'`.
|
|
31
|
+
*/
|
|
21
32
|
dedupeKey?: string;
|
|
22
33
|
/** Milliseconds from now until the job becomes claimable. */
|
|
23
34
|
delay?: number;
|
|
24
35
|
/** Absolute time the job becomes claimable; wins over `delay`. */
|
|
25
36
|
runAt?: Date | number;
|
|
37
|
+
/**
|
|
38
|
+
* Insert the job row through this transaction. The job becomes visible to
|
|
39
|
+
* workers only when the transaction commits; a rollback removes it. Must be
|
|
40
|
+
* a transaction on the app's own database.
|
|
41
|
+
*/
|
|
42
|
+
tx?: EnqueueTransaction;
|
|
43
|
+
};
|
|
44
|
+
/** `EnqueueOptions` with `tx` narrowed to the app schema's transaction type. */
|
|
45
|
+
export type TypedEnqueueOptions<TSchema extends Record<string, unknown>> = Omit<EnqueueOptions, 'tx'> & {
|
|
46
|
+
/**
|
|
47
|
+
* Insert the job row through this transaction. The job becomes visible to
|
|
48
|
+
* workers only when the transaction commits; a rollback removes it.
|
|
49
|
+
*/
|
|
50
|
+
tx?: BunderstackTx<TSchema>;
|
|
26
51
|
};
|
|
52
|
+
/**
|
|
53
|
+
* How long a queue job's `dedupeKey` collapses new enqueues.
|
|
54
|
+
* - `'finish'` (default): until the job reaches a terminal state.
|
|
55
|
+
* - `'start'`: until a worker claims the job; later enqueues create a new row
|
|
56
|
+
* that runs after it and reads the newer state.
|
|
57
|
+
*/
|
|
58
|
+
export type DedupeUntil = 'start' | 'finish';
|
|
27
59
|
export type TickResult = {
|
|
28
60
|
/** Rows moved from pending to running this tick. */
|
|
29
61
|
claimed: number;
|
|
@@ -68,6 +100,12 @@ export type QueueJobDefinition<TInput, TSchema extends Record<string, unknown> =
|
|
|
68
100
|
};
|
|
69
101
|
/** Max simultaneous `running` rows of this type, enforced per worker. */
|
|
70
102
|
concurrency?: number;
|
|
103
|
+
/**
|
|
104
|
+
* When a `dedupeKey` stops collapsing new enqueues. Default `'finish'`:
|
|
105
|
+
* held until a terminal state. `'start'`: released when a worker claims the
|
|
106
|
+
* job, so an enqueue during the run schedules one more run.
|
|
107
|
+
*/
|
|
108
|
+
dedupeUntil?: DedupeUntil;
|
|
71
109
|
handler: (input: TInput, ctx: JobContext<TSchema, TEnvResult, TMessaging>) => Promise<void> | void;
|
|
72
110
|
/** Fires once, after the final attempt fails. Errors here are logged, never retried. */
|
|
73
111
|
onFailed?: (input: TInput, error: Error, ctx: JobContext<TSchema, TEnvResult, TMessaging>) => Promise<void> | void;
|
|
@@ -133,10 +171,11 @@ type JobInputOf<TDef> = TDef extends QueueJobDefinition<infer TInput, any, any,
|
|
|
133
171
|
* methods instead would make TS treat them as overloaded, so the loose
|
|
134
172
|
* `(name: string, ...)` signature would still accept any name.
|
|
135
173
|
*/
|
|
136
|
-
export type JobsFacade<TDefs extends JobsDefs> = Omit<JobsRuntimeFacade, 'enqueue'> & {
|
|
137
|
-
enqueue<K extends QueueJobKeys<TDefs>>(name: K, ...rest: JobInputOf<TDefs[K]> extends undefined ? [input?: undefined, opts?:
|
|
174
|
+
export type JobsFacade<TDefs extends JobsDefs, TSchema extends Record<string, unknown> | undefined = undefined> = Omit<JobsRuntimeFacade, 'enqueue'> & {
|
|
175
|
+
enqueue<K extends QueueJobKeys<TDefs>>(name: K, ...rest: JobInputOf<TDefs[K]> extends undefined ? [input?: undefined, opts?: JobsFacadeEnqueueOptions<TSchema>] : [input: JobInputOf<TDefs[K]>, opts?: JobsFacadeEnqueueOptions<TSchema>]): Promise<{
|
|
138
176
|
id: string;
|
|
139
177
|
}>;
|
|
140
178
|
};
|
|
179
|
+
type JobsFacadeEnqueueOptions<TSchema extends Record<string, unknown> | undefined> = [TSchema] extends [Record<string, unknown>] ? TypedEnqueueOptions<TSchema> : EnqueueOptions;
|
|
141
180
|
export {};
|
|
142
181
|
//# sourceMappingURL=define.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"define.d.ts","sourceRoot":"","sources":["../../src/jobs/define.ts"],"names":[],"mappings":"AAGA,OAAO,KAAK,EAAE,gBAAgB,EAAE,MAAM,uBAAuB,CAAA;AAE7D,OAAO,KAAK,EAAE,KAAK,EAAE,MAAM,OAAO,CAAA;
|
|
1
|
+
{"version":3,"file":"define.d.ts","sourceRoot":"","sources":["../../src/jobs/define.ts"],"names":[],"mappings":"AAGA,OAAO,KAAK,EAAE,g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ACH,MAAM,MAAM,UAAU,CACpB,KAAK,SAAS,QAAQ,EACtB,OAAO,SAAS,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,GAAG,SAAS,GAAG,SAAS,IAC7D,IAAI,CAAC,iBAAiB,EAAE,SAAS,CAAC,GAAG;IACvC,OAAO,CAAC,CAAC,SAAS,YAAY,CAAC,KAAK,CAAC,EACnC,IAAI,EAAE,CAAC,EACP,GAAG,IAAI,EAAE,UAAU,CAAC,KAAK,CAAC,CAAC,CAAC,CAAC,SAAS,SAAS,GAC3C,CAAC,KAAK,CAAC,EAAE,SAAS,EAAE,IAAI,CAAC,EAAE,wBAAwB,CAAC,OAAO,CAAC,CAAC,GAC7D,CAAC,KAAK,EAAE,UAAU,CAAC,KAAK,CAAC,CAAC,CAAC,CAAC,EAAE,IAAI,CAAC,EAAE,wBAAwB,CAAC,OAAO,CAAC,CAAC,GAC1E,OAAO,CAAC;QAAE,EAAE,EAAE,MAAM,CAAA;KAAE,CAAC,CAAA;CAC3B,CAAA;AAED,KAAK,wBAAwB,CAC3B,OAAO,SAAS,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,GAAG,SAAS,IACjD,CAAC,OAAO,CAAC,SAAS,CAAC,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,CAAC,GAC3C,mBAAmB,CAAC,OAAO,CAAC,GAC5B,cAAc,CAAA"}
|
package/dist/jobs/define.js
CHANGED
|
@@ -32,6 +32,7 @@ export function validateBackgroundDefs(defs) {
|
|
|
32
32
|
}
|
|
33
33
|
if (def.kind === 'cron') {
|
|
34
34
|
parseCron(def.schedule);
|
|
35
|
+
assertCronHasNoDedupeUntil(def, `cron "${name}"`);
|
|
35
36
|
if (def.concurrency !== undefined) {
|
|
36
37
|
throw new Error(`[bunderstack] cron "${name}": concurrency is not supported for cron tasks — slots are already unique`);
|
|
37
38
|
}
|
|
@@ -44,6 +45,17 @@ export function validateBackgroundDefs(defs) {
|
|
|
44
45
|
(def.concurrency < 1 || !Number.isInteger(def.concurrency))) {
|
|
45
46
|
throw new Error(`[bunderstack] job "${name}": concurrency must be a positive integer`);
|
|
46
47
|
}
|
|
48
|
+
if (def.dedupeUntil !== undefined &&
|
|
49
|
+
def.dedupeUntil !== 'start' &&
|
|
50
|
+
def.dedupeUntil !== 'finish') {
|
|
51
|
+
throw new Error(`[bunderstack] job "${name}": dedupeUntil must be 'start' or 'finish'`);
|
|
52
|
+
}
|
|
53
|
+
}
|
|
54
|
+
}
|
|
55
|
+
/** Cron slot ownership depends on the retained dedupe key. */
|
|
56
|
+
function assertCronHasNoDedupeUntil(def, label) {
|
|
57
|
+
if (def.dedupeUntil !== undefined) {
|
|
58
|
+
throw new Error(`[bunderstack] ${label}: dedupeUntil is not supported for cron tasks — cron tasks retain their slot key`);
|
|
47
59
|
}
|
|
48
60
|
}
|
|
49
61
|
/** @deprecated Use validateBackgroundDefs. */
|
|
@@ -74,6 +86,7 @@ export function createJobsBuilder() {
|
|
|
74
86
|
},
|
|
75
87
|
cron(def) {
|
|
76
88
|
parseCron(def.schedule);
|
|
89
|
+
assertCronHasNoDedupeUntil(def, 'cron');
|
|
77
90
|
return { kind: 'cron', ...def };
|
|
78
91
|
},
|
|
79
92
|
/** Identity with validation: returns the defs map, typed. */
|
package/dist/jobs/define.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"define.js","sourceRoot":"","sources":["../../src/jobs/define.ts"],"names":[],"mappings":"AASA,OAAO,EAAE,SAAS,EAAE,MAAM,QAAQ,CAAA;AAClC,OAAO,EAAE,WAAW,EAAgB,MAAM,SAAS,CAAA;AAEnD,MAAM,CAAC,MAAM,eAAe,GAAG,CAAC,CAAA;AAChC,MAAM,CAAC,MAAM,yBAAyB,GAAG,MAAM,CAAA;AAC/C,iDAAiD;AACjD,MAAM,CAAC,MAAM,kBAAkB,GAAG,yBAAyB,CAAA;AAW3D,MAAM,UAAU,gBAAgB,CAA6B,GAAM;IACjE,OAAO,GAAG,CAAC,aAAa,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AO,GAAG,CAAC,CAAC,CAAA;IAC7C,OAAO,IAAI,CAAC,KAAK,CAAC,IAAI,GAAG,CAAC,GAAG,GAAG,IAAI,CAAC,MAAM,EAAE,GAAG,GAAG,CAAC,CAAC,CAAA;AACvD,CAAC;AAED;;;GAGG;AACH,MAAM,UAAU,iBAAiB;IAK/B,OAAO;QACL,mEAAmE;QACnE,GAAG,CACD,GAGC;YAED,OAAO,EAAE,IAAI,EAAE,KAAK,EAAE,GAAG,GAAG,EAAE,CAAA;QAChC,CAAC;QACD,IAAI,CACF,GAGC;YAED,SAAS,CAAC,GAAG,CAAC,QAAQ,CAAC,CAAA;YACvB,OAAO,EAAE,IAAI,EAAE,MAAM,EAAE,GAAG,GAAG,EAAE,CAAA;QACjC,CAAC;QACD,6DAA6D;QAC7D,MAAM,CAA+B,IAAW;YAC9C,sBAAsB,CAAC,IAAI,CAAC,CAAA;YAC5B,OAAO,IAAI,CAAA;QACb,CAAC;KACF,CAAA;AACH,CAAC","sourcesContent":["// src/jobs/define.ts — job definition types and the typed builder.\n// `createJobsBuilder` exists purely to carry\n// TSchema/TEnvResult typing into inline callbacks and extracted files.\nimport type { StandardSchemaV1 } from '@standard-schema/spec'\n\nimport type { DbFor } from '../db'\nimport type { MessagingConfig, MessagingFacadesFor } from '../messaging'\nimport type { StorageFacade } from '../runtime'\n\nimport { parseCron } from './cron'\nimport { CRON_PREFIX, type CatchUp } from './slots'\n\nexport const DEFAULT_RETRIES = 3\nexport const DEFAULT_LEASE_DURATION_MS = 60_000\n/** @deprecated Use DEFAULT_LEASE_DURATION_MS. */\nexport const DEFAULT_TIMEOUT_MS = DEFAULT_LEASE_DURATION_MS\n\nexport type BackgroundTiming = {\n /** Lease duration in ms; an expired lease sends the job back to pending. */\n leaseDuration?: number\n /** Optional cooperative execution deadline in ms. */\n maxRuntime?: number\n /** @deprecated Use leaseDuration. */\n timeout?: number\n}\n\nexport function leaseDurationFor<T extends BackgroundTiming>(def: T): number {\n return def.leaseDuration ?? def.timeout ?? DEFAULT_LEASE_DURATION_MS\n}\n\nexport type EnqueueOptions = {\n /** Collapse duplicate enqueues while the queue row is non-terminal. */\n dedupeKey?: string\n /** Milliseconds from now until the job becomes claimable. */\n delay?: number\n /** Absolute time the job becomes claimable; wins over `delay`. */\n runAt?: Date | number\n}\n\nexport type TickResult = {\n /** Rows moved from pending to running this tick. */\n claimed: number\n /** Handlers that completed successfully. */\n ran: number\n /** Handlers that threw, whether or not they will be retried. */\n failed: number\n}\n\n/**\n * The untyped runtime facade. Job handlers and API context expose this shape;\n * `app.jobs` narrows `enqueue` to the declared job names/payloads.\n */\nexport type JobsRuntimeFacade = {\n enqueue(\n name: string,\n input?: unknown,\n opts?: EnqueueOptions,\n ): Promise<{ id: string }>\n /** Run one poll cycle deterministically (tests). `now` defaults to Date.now(). */\n tick(now?: number): Promise<TickResult>\n}\n\nimport type { RealtimeFacade } from '../realtime/facade'\n\nexport type JobContext<\n TSchema extends Record<string, unknown> = Record<string, unknown>,\n TEnvResult = Record<string, unknown>,\n TMessaging extends MessagingConfig | undefined = MessagingConfig,\n> = {\n db: DbFor<TSchema>\n env: TEnvResult\n messaging: MessagingFacadesFor<TMessaging>\n storage: StorageFacade\n jobs: JobsRuntimeFacade\n realtime: RealtimeFacade<TSchema>\n /** Aborted when execution reaches its deadline or the job loses its lease. */\n signal: AbortSignal\n}\n\nexport type BunderstackJobContext<\n TSchema extends Record<string, unknown> = Record<string, unknown>,\n TEnvResult = Record<string, unknown>,\n TMessaging extends MessagingConfig | undefined = MessagingConfig,\n> = JobContext<TSchema, TEnvResult, TMessaging>\n\nexport type QueueJobDefinition<\n TInput,\n TSchema extends Record<string, unknown> = Record<string, unknown>,\n TEnvResult = Record<string, unknown>,\n TMessaging extends MessagingConfig | undefined = MessagingConfig,\n> = BackgroundTiming & {\n kind: 'job'\n /** Standard Schema payload; parsed at enqueue AND before the handler runs. */\n input?: StandardSchemaV1<unknown, TInput>\n /** Attempts after the first failure. Default 3 (so 4 total attempts). */\n retries?: number\n /** Delay before retry N (1-based). Default exponential: 1s, 2s, 4s, … */\n backoff?: ((attempt: number) => number) | { baseMs?: number; factor?: number }\n /** Max simultaneous `running` rows of this type, enforced per worker. */\n concurrency?: number\n handler: (\n input: TInput,\n ctx: JobContext<TSchema, TEnvResult, TMessaging>,\n ) => Promise<void> | void\n /** Fires once, after the final attempt fails. Errors here are logged, never retried. */\n onFailed?: (\n input: TInput,\n error: Error,\n ctx: JobContext<TSchema, TEnvResult, TMessaging>,\n ) => Promise<void> | void\n}\n\nexport type CronInvocation = { scheduledFor: Date }\n\nexport type CronDefinition<\n TSchema extends Record<string, unknown> = Record<string, unknown>,\n TEnvResult = Record<string, unknown>,\n TSchedule extends string = string,\n TMessaging extends MessagingConfig | undefined = MessagingConfig,\n> = BackgroundTiming & {\n kind: 'cron'\n schedule: TSchedule\n /** Attempts after the first failure. Default 3 (so 4 total attempts). */\n retries?: number\n /** Delay before retry N (1-based). Default exponential: 1s, 2s, 4s, … */\n backoff?: ((attempt: number) => number) | { baseMs?: number; factor?: number }\n /** How missed slots are handled on wake. Default 'latest'. */\n catchUp?: CatchUp\n /** How far back catch-up looks, in ms. Default 1 hour. */\n catchUpWindow?: number\n handler: (\n invocation: CronInvocation,\n ctx: JobContext<TSchema, TEnvResult, TMessaging>,\n ) => Promise<void> | void\n /** Fires once, after the final attempt fails. Errors here are logged, never retried. */\n onFailed?: (\n invocation: CronInvocation,\n error: Error,\n ctx: JobContext<TSchema, TEnvResult, TMessaging>,\n ) => Promise<void> | void\n}\n\nexport type BackgroundDefinition =\n | QueueJobDefinition<any, any, any, any>\n | CronDefinition<any, any, any, any>\nexport type BackgroundDefs = Record<string, BackgroundDefinition>\n\n/** @deprecated Use QueueJobDefinition. */\nexport type JobDefinition<\n TInput,\n TSchema extends Record<string, unknown> = Record<string, unknown>,\n TEnvResult = Record<string, unknown>,\n TMessaging extends MessagingConfig | undefined = MessagingConfig,\n> = QueueJobDefinition<TInput, TSchema, TEnvResult, TMessaging>\n\n// eslint-disable-next-line @typescript-eslint/no-explicit-any\nexport type AnyJobDefinition = QueueJobDefinition<any, any, any, any>\n// eslint-disable-next-line @typescript-eslint/no-explicit-any\nexport type AnyBackgroundDefinition =\n | QueueJobDefinition<any, any, any, any>\n | CronDefinition<any, any, any, any>\nexport type JobsDefs = BackgroundDefs\n\nexport type QueueJobKeys<TDefs extends BackgroundDefs> = {\n [K in keyof TDefs & string]: TDefs[K] extends QueueJobDefinition<\n any,\n any,\n any,\n any\n >\n ? K\n : never\n}[keyof TDefs & string]\n\n/** Throws when a definition is unusable. Safe to call more than once. */\nexport function validateBackgroundDefs(defs: BackgroundDefs): void {\n for (const [name, def] of Object.entries(defs)) {\n if (typeof def.handler !== 'function') {\n throw new Error(`[bunderstack] background task \"${name}\" has no handler`)\n }\n if (def.kind === 'job' && name.startsWith(CRON_PREFIX)) {\n throw new Error(\n `[bunderstack] job \"${name}\": the \"${CRON_PREFIX}\" prefix is reserved for cron tasks`,\n )\n }\n if (\n def.retries !== undefined &&\n (def.retries < 0 || !Number.isInteger(def.retries))\n ) {\n throw new Error(\n `[bunderstack] background task \"${name}\": retries must be a non-negative integer`,\n )\n }\n if (def.timeout !== undefined && def.leaseDuration !== undefined) {\n throw new Error(\n `[bunderstack] background task \"${name}\": timeout and leaseDuration cannot both be declared`,\n )\n }\n for (const field of ['timeout', 'leaseDuration', 'maxRuntime'] as const) {\n const value = def[field]\n if (\n value !== undefined &&\n (!Number.isFinite(value) || !Number.isInteger(value) || value <= 0)\n ) {\n throw new Error(\n `[bunderstack] background task \"${name}\": ${field} must be a positive finite integer`,\n )\n }\n }\n if (def.kind === 'cron') {\n parseCron(def.schedule)\n if ((def as { concurrency?: number }).concurrency !== undefined) {\n throw new Error(\n `[bunderstack] cron \"${name}\": concurrency is not supported for cron tasks — slots are already unique`,\n )\n }\n if (def.catchUpWindow !== undefined && def.catchUpWindow <= 0) {\n throw new Error(\n `[bunderstack] cron \"${name}\": catchUpWindow must be positive`,\n )\n }\n continue\n }\n if (\n def.concurrency !== undefined &&\n (def.concurrency < 1 || !Number.isInteger(def.concurrency))\n ) {\n throw new Error(\n `[bunderstack] job \"${name}\": concurrency must be a positive integer`,\n )\n }\n }\n}\n\n/** @deprecated Use validateBackgroundDefs. */\nexport const validateJobsDefs = validateBackgroundDefs\n\n/**\n * Delay in ms before retry `attempt` (1-based = the attempt that just failed).\n * Jittered by ±20% so a shared outage does not retry every job in lockstep.\n * A caller-supplied backoff function is returned verbatim — the caller owns it.\n */\nexport function backoffMs(\n def: AnyBackgroundDefinition,\n attempt: number,\n): number {\n const b = def.backoff\n if (typeof b === 'function') return b(attempt)\n const baseMs = b?.baseMs ?? 1000\n const factor = b?.factor ?? 2\n const flat = baseMs * factor ** (attempt - 1)\n return Math.round(flat * (0.8 + Math.random() * 0.4))\n}\n\n/**\n * Build the `j` instance bunderstack hands to the config's `jobs` builder\n * callback (and exports for multi-file job setups).\n */\nexport function createJobsBuilder<\n TSchema extends Record<string, unknown>,\n TEnvResult = Record<string, unknown>,\n TMessaging extends MessagingConfig | undefined = MessagingConfig,\n>() {\n return {\n /** Identity with inference: pins TInput from the schema output. */\n job<TInput = undefined>(\n def: Omit<\n QueueJobDefinition<TInput, TSchema, TEnvResult, TMessaging>,\n 'kind'\n >,\n ): QueueJobDefinition<TInput, TSchema, TEnvResult, TMessaging> {\n return { kind: 'job', ...def }\n },\n cron<const TSchedule extends string>(\n def: Omit<\n CronDefinition<TSchema, TEnvResult, TSchedule, TMessaging>,\n 'kind'\n >,\n ): CronDefinition<TSchema, TEnvResult, TSchedule, TMessaging> {\n parseCron(def.schedule)\n return { kind: 'cron', ...def }\n },\n /** Identity with validation: returns the defs map, typed. */\n define<TDefs extends BackgroundDefs>(defs: TDefs): TDefs {\n validateBackgroundDefs(defs)\n return defs\n },\n }\n}\n\n/** Type of the `j` instance — for builder callbacks declared in separate files. */\nexport type BunderstackJobsBuilder<\n TSchema extends Record<string, unknown>,\n TEnvResult = Record<string, unknown>,\n TMessaging extends MessagingConfig | undefined = MessagingConfig,\n> = ReturnType<typeof createJobsBuilder<TSchema, TEnvResult, TMessaging>>\n\n// Infers TInput from the JobDefinition's own type argument rather than\n// pattern-matching the (optional, so union-with-undefined) `input` property —\n// A required-property pattern fails structurally because `input` is optional,\n// so infer from the definition's own type argument instead.\ntype JobInputOf<TDef> =\n TDef extends QueueJobDefinition<infer TInput, any, any, any>\n ? TInput\n : undefined\n\n/**\n * `app.jobs`: `enqueue` narrowed to declared names + payloads. `Omit`s the\n * runtime facade's loose `enqueue` first — intersecting two same-named\n * methods instead would make TS treat them as overloaded, so the loose\n * `(name: string, ...)` signature would still accept any name.\n */\nexport type JobsFacade<TDefs extends JobsDefs> = Omit<\n JobsRuntimeFacade,\n 'enqueue'\n> & {\n enqueue<K extends QueueJobKeys<TDefs>>(\n name: K,\n ...rest: JobInputOf<TDefs[K]> extends undefined\n ? [input?: undefined, opts?: EnqueueOptions]\n : [input: JobInputOf<TDefs[K]>, opts?: EnqueueOptions]\n ): Promise<{ id: string }>\n}\n"]}
|
|
1
|
+
{"version":3,"file":"define.js","sourceRoot":"","sources":["../../src/jobs/define.ts"],"names":[],"mappings":"AAUA,OAAO,EAAE,SAAS,EAAE,MAAM,QAAQ,CAAA;AAClC,OAAO,EAAE,WAAW,EAAgB,MAAM,SAAS,CAAA;AAEnD,MAAM,CAAC,MAAM,eAAe,GAAG,CAAC,CAAA;AAChC,MAAM,CAAC,MAAM,yBAAyB,GAAG,MAAM,CAAA;AAC/C,iDAAiD;AACjD,MAAM,CAAC,MAAM,kBAAkB,GAAG,yBAAyB,CAAA;AAW3D,MAAM,UAAU,gBAAgB,CAA6B,GAAM;IACjE,OAAO,GAAG,CAAC,aAAa,IAAI,GAAG,CAAC,OAAO,IAAI,yBAAyB,CAAA;AACtE,CAAC;AA6LD,yEAAyE;AACzE,MAAM,UAAU,sBAAsB,CAAC,IAAoB;IACzD,KAAK,MAAM,CAAC,IAAI,EAAE,GAAG,CAAC,IAAI,M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AAM,CAAC,GAAG,GAAG,CAAC,OAAO,CAAA;IACrB,IAAI,OAAO,CAAC,KAAK,UAAU;QAAE,OAAO,CAAC,CAAC,OAAO,CAAC,CAAA;IAC9C,MAAM,MAAM,GAAG,CAAC,EAAE,MAAM,IAAI,IAAI,CAAA;IAChC,MAAM,MAAM,GAAG,CAAC,EAAE,MAAM,IAAI,CAAC,CAAA;IAC7B,MAAM,IAAI,GAAG,MAAM,GAAG,MAAM,IAAI,CAAC,OAAO,GAAG,CAAC,CAAC,CAAA;IAC7C,OAAO,IAAI,CAAC,KAAK,CAAC,IAAI,GAAG,CAAC,GAAG,GAAG,IAAI,CAAC,MAAM,EAAE,GAAG,GAAG,CAAC,CAAC,CAAA;AACvD,CAAC;AAED;;;GAGG;AACH,MAAM,UAAU,iBAAiB;IAK/B,OAAO;QACL,mEAAmE;QACnE,GAAG,CACD,GAGC;YAED,OAAO,EAAE,IAAI,EAAE,KAAK,EAAE,GAAG,GAAG,EAAE,CAAA;QAChC,CAAC;QACD,IAAI,CACF,GAGC;YAED,SAAS,CAAC,GAAG,CAAC,QAAQ,CAAC,CAAA;YACvB,0BAA0B,CAAC,GAAG,EAAE,MAAM,CAAC,CAAA;YACvC,OAAO,EAAE,IAAI,EAAE,MAAM,EAAE,GAAG,GAAG,EAAE,CAAA;QACjC,CAAC;QACD,6DAA6D;QAC7D,MAAM,CAA+B,IAAW;YAC9C,sBAAsB,CAAC,IAAI,CAAC,CAAA;YAC5B,OAAO,IAAI,CAAA;QACb,CAAC;KACF,CAAA;AACH,CAAC","sourcesContent":["// src/jobs/define.ts — job definition types and the typed builder.\n// `createJobsBuilder` exists purely to carry\n// TSchema/TEnvResult typing into inline callbacks and extracted files.\nimport type { StandardSchemaV1 } from '@standard-schema/spec'\n\nimport type { BunderstackTx, DbFor } from '../db'\nimport type { AnyDb } from '../dialect'\nimport type { MessagingConfig, MessagingFacadesFor } from '../messaging'\nimport type { StorageFacade } from '../runtime'\n\nimport { parseCron } from './cron'\nimport { CRON_PREFIX, type CatchUp } from './slots'\n\nexport const DEFAULT_RETRIES = 3\nexport const DEFAULT_LEASE_DURATION_MS = 60_000\n/** @deprecated Use DEFAULT_LEASE_DURATION_MS. */\nexport const DEFAULT_TIMEOUT_MS = DEFAULT_LEASE_DURATION_MS\n\nexport type BackgroundTiming = {\n /** Lease duration in ms; an expired lease sends the job back to pending. */\n leaseDuration?: number\n /** Optional cooperative execution deadline in ms. */\n maxRuntime?: number\n /** @deprecated Use leaseDuration. */\n timeout?: number\n}\n\nexport function leaseDurationFor<T extends BackgroundTiming>(def: T): number {\n return def.leaseDuration ?? def.timeout ?? DEFAULT_LEASE_DURATION_MS\n}\n\n/**\n * Structural view of a Drizzle database or transaction handle that `enqueue`\n * can insert through. The loose runtime facade accepts any handle of the app's\n * dialect; `app.jobs` narrows it to the app's `BunderstackTx`.\n */\nexport type EnqueueTransaction = AnyDb\n\nexport type EnqueueOptions = {\n /**\n * Collapse duplicate enqueues while the queue row holds its key: until a\n * terminal state by default, or until it is claimed with\n * `dedupeUntil: 'start'`.\n */\n dedupeKey?: string\n /** Milliseconds from now until the job becomes claimable. */\n delay?: number\n /** Absolute time the job becomes claimable; wins over `delay`. */\n runAt?: Date | number\n /**\n * Insert the job row through this transaction. The job becomes visible to\n * workers only when the transaction commits; a rollback removes it. Must be\n * a transaction on the app's own database.\n */\n tx?: EnqueueTransaction\n}\n\n/** `EnqueueOptions` with `tx` narrowed to the app schema's transaction type. */\nexport type TypedEnqueueOptions<TSchema extends Record<string, unknown>> = Omit<\n EnqueueOptions,\n 'tx'\n> & {\n /**\n * Insert the job row through this transaction. The job becomes visible to\n * workers only when the transaction commits; a rollback removes it.\n */\n tx?: BunderstackTx<TSchema>\n}\n\n/**\n * How long a queue job's `dedupeKey` collapses new enqueues.\n * - `'finish'` (default): until the job reaches a terminal state.\n * - `'start'`: until a worker claims the job; later enqueues create a new row\n * that runs after it and reads the newer state.\n */\nexport type DedupeUntil = 'start' | 'finish'\n\nexport type TickResult = {\n /** Rows moved from pending to running this tick. */\n claimed: number\n /** Handlers that completed successfully. */\n ran: number\n /** Handlers that threw, whether or not they will be retried. */\n failed: number\n}\n\n/**\n * The untyped runtime facade. Job handlers and API context expose this shape;\n * `app.jobs` narrows `enqueue` to the declared job names/payloads.\n */\nexport type JobsRuntimeFacade = {\n enqueue(\n name: string,\n input?: unknown,\n opts?: EnqueueOptions,\n ): Promise<{ id: string }>\n /** Run one poll cycle deterministically (tests). `now` defaults to Date.now(). */\n tick(now?: number): Promise<TickResult>\n}\n\nimport type { RealtimeFacade } from '../realtime/facade'\n\nexport type JobContext<\n TSchema extends Record<string, unknown> = Record<string, unknown>,\n TEnvResult = Record<string, unknown>,\n TMessaging extends MessagingConfig | undefined = MessagingConfig,\n> = {\n db: DbFor<TSchema>\n env: TEnvResult\n messaging: MessagingFacadesFor<TMessaging>\n storage: StorageFacade\n jobs: JobsRuntimeFacade\n realtime: RealtimeFacade<TSchema>\n /** Aborted when execution reaches its deadline or the job loses its lease. */\n signal: AbortSignal\n}\n\nexport type BunderstackJobContext<\n TSchema extends Record<string, unknown> = Record<string, unknown>,\n TEnvResult = Record<string, unknown>,\n TMessaging extends MessagingConfig | undefined = MessagingConfig,\n> = JobContext<TSchema, TEnvResult, TMessaging>\n\nexport type QueueJobDefinition<\n TInput,\n TSchema extends Record<string, unknown> = Record<string, unknown>,\n TEnvResult = Record<string, unknown>,\n TMessaging extends MessagingConfig | undefined = MessagingConfig,\n> = BackgroundTiming & {\n kind: 'job'\n /** Standard Schema payload; parsed at enqueue AND before the handler runs. */\n input?: StandardSchemaV1<unknown, TInput>\n /** Attempts after the first failure. Default 3 (so 4 total attempts). */\n retries?: number\n /** Delay before retry N (1-based). Default exponential: 1s, 2s, 4s, … */\n backoff?: ((attempt: number) => number) | { baseMs?: number; factor?: number }\n /** Max simultaneous `running` rows of this type, enforced per worker. */\n concurrency?: number\n /**\n * When a `dedupeKey` stops collapsing new enqueues. Default `'finish'`:\n * held until a terminal state. `'start'`: released when a worker claims the\n * job, so an enqueue during the run schedules one more run.\n */\n dedupeUntil?: DedupeUntil\n handler: (\n input: TInput,\n ctx: JobContext<TSchema, TEnvResult, TMessaging>,\n ) => Promise<void> | void\n /** Fires once, after the final attempt fails. Errors here are logged, never retried. */\n onFailed?: (\n input: TInput,\n error: Error,\n ctx: JobContext<TSchema, TEnvResult, TMessaging>,\n ) => Promise<void> | void\n}\n\nexport type CronInvocation = { scheduledFor: Date }\n\nexport type CronDefinition<\n TSchema extends Record<string, unknown> = Record<string, unknown>,\n TEnvResult = Record<string, unknown>,\n TSchedule extends string = string,\n TMessaging extends MessagingConfig | undefined = MessagingConfig,\n> = BackgroundTiming & {\n kind: 'cron'\n schedule: TSchedule\n /** Attempts after the first failure. Default 3 (so 4 total attempts). */\n retries?: number\n /** Delay before retry N (1-based). Default exponential: 1s, 2s, 4s, … */\n backoff?: ((attempt: number) => number) | { baseMs?: number; factor?: number }\n /** How missed slots are handled on wake. Default 'latest'. */\n catchUp?: CatchUp\n /** How far back catch-up looks, in ms. Default 1 hour. */\n catchUpWindow?: number\n handler: (\n invocation: CronInvocation,\n ctx: JobContext<TSchema, TEnvResult, TMessaging>,\n ) => Promise<void> | void\n /** Fires once, after the final attempt fails. Errors here are logged, never retried. */\n onFailed?: (\n invocation: CronInvocation,\n error: Error,\n ctx: JobContext<TSchema, TEnvResult, TMessaging>,\n ) => Promise<void> | void\n}\n\nexport type BackgroundDefinition =\n | QueueJobDefinition<any, any, any, any>\n | CronDefinition<any, any, any, any>\nexport type BackgroundDefs = Record<string, BackgroundDefinition>\n\n/** @deprecated Use QueueJobDefinition. */\nexport type JobDefinition<\n TInput,\n TSchema extends Record<string, unknown> = Record<string, unknown>,\n TEnvResult = Record<string, unknown>,\n TMessaging extends MessagingConfig | undefined = MessagingConfig,\n> = QueueJobDefinition<TInput, TSchema, TEnvResult, TMessaging>\n\n// eslint-disable-next-line @typescript-eslint/no-explicit-any\nexport type AnyJobDefinition = QueueJobDefinition<any, any, any, any>\n// eslint-disable-next-line @typescript-eslint/no-explicit-any\nexport type AnyBackgroundDefinition =\n | QueueJobDefinition<any, any, any, any>\n | CronDefinition<any, any, any, any>\nexport type JobsDefs = BackgroundDefs\n\nexport type QueueJobKeys<TDefs extends BackgroundDefs> = {\n [K in keyof TDefs & string]: TDefs[K] extends QueueJobDefinition<\n any,\n any,\n any,\n any\n >\n ? K\n : never\n}[keyof TDefs & string]\n\n/** Throws when a definition is unusable. Safe to call more than once. */\nexport function validateBackgroundDefs(defs: BackgroundDefs): void {\n for (const [name, def] of Object.entries(defs)) {\n if (typeof def.handler !== 'function') {\n throw new Error(`[bunderstack] background task \"${name}\" has no handler`)\n }\n if (def.kind === 'job' && name.startsWith(CRON_PREFIX)) {\n throw new Error(\n `[bunderstack] job \"${name}\": the \"${CRON_PREFIX}\" prefix is reserved for cron tasks`,\n )\n }\n if (\n def.retries !== undefined &&\n (def.retries < 0 || !Number.isInteger(def.retries))\n ) {\n throw new Error(\n `[bunderstack] background task \"${name}\": retries must be a non-negative integer`,\n )\n }\n if (def.timeout !== undefined && def.leaseDuration !== undefined) {\n throw new Error(\n `[bunderstack] background task \"${name}\": timeout and leaseDuration cannot both be declared`,\n )\n }\n for (const field of ['timeout', 'leaseDuration', 'maxRuntime'] as const) {\n const value = def[field]\n if (\n value !== undefined &&\n (!Number.isFinite(value) || !Number.isInteger(value) || value <= 0)\n ) {\n throw new Error(\n `[bunderstack] background task \"${name}\": ${field} must be a positive finite integer`,\n )\n }\n }\n if (def.kind === 'cron') {\n parseCron(def.schedule)\n assertCronHasNoDedupeUntil(def, `cron \"${name}\"`)\n if ((def as { concurrency?: number }).concurrency !== undefined) {\n throw new Error(\n `[bunderstack] cron \"${name}\": concurrency is not supported for cron tasks — slots are already unique`,\n )\n }\n if (def.catchUpWindow !== undefined && def.catchUpWindow <= 0) {\n throw new Error(\n `[bunderstack] cron \"${name}\": catchUpWindow must be positive`,\n )\n }\n continue\n }\n if (\n def.concurrency !== undefined &&\n (def.concurrency < 1 || !Number.isInteger(def.concurrency))\n ) {\n throw new Error(\n `[bunderstack] job \"${name}\": concurrency must be a positive integer`,\n )\n }\n if (\n def.dedupeUntil !== undefined &&\n def.dedupeUntil !== 'start' &&\n def.dedupeUntil !== 'finish'\n ) {\n throw new Error(\n `[bunderstack] job \"${name}\": dedupeUntil must be 'start' or 'finish'`,\n )\n }\n }\n}\n\n/** Cron slot ownership depends on the retained dedupe key. */\nfunction assertCronHasNoDedupeUntil(def: object, label: string): void {\n if ((def as { dedupeUntil?: unknown }).dedupeUntil !== undefined) {\n throw new Error(\n `[bunderstack] ${label}: dedupeUntil is not supported for cron tasks — cron tasks retain their slot key`,\n )\n }\n}\n\n/** @deprecated Use validateBackgroundDefs. */\nexport const validateJobsDefs = validateBackgroundDefs\n\n/**\n * Delay in ms before retry `attempt` (1-based = the attempt that just failed).\n * Jittered by ±20% so a shared outage does not retry every job in lockstep.\n * A caller-supplied backoff function is returned verbatim — the caller owns it.\n */\nexport function backoffMs(\n def: AnyBackgroundDefinition,\n attempt: number,\n): number {\n const b = def.backoff\n if (typeof b === 'function') return b(attempt)\n const baseMs = b?.baseMs ?? 1000\n const factor = b?.factor ?? 2\n const flat = baseMs * factor ** (attempt - 1)\n return Math.round(flat * (0.8 + Math.random() * 0.4))\n}\n\n/**\n * Build the `j` instance bunderstack hands to the config's `jobs` builder\n * callback (and exports for multi-file job setups).\n */\nexport function createJobsBuilder<\n TSchema extends Record<string, unknown>,\n TEnvResult = Record<string, unknown>,\n TMessaging extends MessagingConfig | undefined = MessagingConfig,\n>() {\n return {\n /** Identity with inference: pins TInput from the schema output. */\n job<TInput = undefined>(\n def: Omit<\n QueueJobDefinition<TInput, TSchema, TEnvResult, TMessaging>,\n 'kind'\n >,\n ): QueueJobDefinition<TInput, TSchema, TEnvResult, TMessaging> {\n return { kind: 'job', ...def }\n },\n cron<const TSchedule extends string>(\n def: Omit<\n CronDefinition<TSchema, TEnvResult, TSchedule, TMessaging>,\n 'kind'\n >,\n ): CronDefinition<TSchema, TEnvResult, TSchedule, TMessaging> {\n parseCron(def.schedule)\n assertCronHasNoDedupeUntil(def, 'cron')\n return { kind: 'cron', ...def }\n },\n /** Identity with validation: returns the defs map, typed. */\n define<TDefs extends BackgroundDefs>(defs: TDefs): TDefs {\n validateBackgroundDefs(defs)\n return defs\n },\n }\n}\n\n/** Type of the `j` instance — for builder callbacks declared in separate files. */\nexport type BunderstackJobsBuilder<\n TSchema extends Record<string, unknown>,\n TEnvResult = Record<string, unknown>,\n TMessaging extends MessagingConfig | undefined = MessagingConfig,\n> = ReturnType<typeof createJobsBuilder<TSchema, TEnvResult, TMessaging>>\n\n// Infers TInput from the JobDefinition's own type argument rather than\n// pattern-matching the (optional, so union-with-undefined) `input` property —\n// A required-property pattern fails structurally because `input` is optional,\n// so infer from the definition's own type argument instead.\ntype JobInputOf<TDef> =\n TDef extends QueueJobDefinition<infer TInput, any, any, any>\n ? TInput\n : undefined\n\n/**\n * `app.jobs`: `enqueue` narrowed to declared names + payloads. `Omit`s the\n * runtime facade's loose `enqueue` first — intersecting two same-named\n * methods instead would make TS treat them as overloaded, so the loose\n * `(name: string, ...)` signature would still accept any name.\n */\nexport type JobsFacade<\n TDefs extends JobsDefs,\n TSchema extends Record<string, unknown> | undefined = undefined,\n> = Omit<JobsRuntimeFacade, 'enqueue'> & {\n enqueue<K extends QueueJobKeys<TDefs>>(\n name: K,\n ...rest: JobInputOf<TDefs[K]> extends undefined\n ? [input?: undefined, opts?: JobsFacadeEnqueueOptions<TSchema>]\n : [input: JobInputOf<TDefs[K]>, opts?: JobsFacadeEnqueueOptions<TSchema>]\n ): Promise<{ id: string }>\n}\n\ntype JobsFacadeEnqueueOptions<\n TSchema extends Record<string, unknown> | undefined,\n> = [TSchema] extends [Record<string, unknown>]\n ? TypedEnqueueOptions<TSchema>\n : EnqueueOptions\n"]}
|
package/dist/jobs/index.d.ts
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
export { createJobsBuilder, validateBackgroundDefs, validateJobsDefs, DEFAULT_RETRIES, DEFAULT_LEASE_DURATION_MS, DEFAULT_TIMEOUT_MS, leaseDurationFor, } from './define.js';
|
|
2
|
-
export type { AnyJobDefinition, BackgroundTiming, BackgroundDefinition, BackgroundDefs, BunderstackJobContext, BunderstackJobsBuilder, EnqueueOptions, JobContext, JobDefinition, QueueJobDefinition, CronDefinition, CronInvocation, QueueJobKeys, JobsDefs, JobsFacade, JobsRuntimeFacade, } from './define.js';
|
|
3
|
-
export { enqueueJob } from './queue.js';
|
|
2
|
+
export type { AnyJobDefinition, BackgroundTiming, BackgroundDefinition, BackgroundDefs, BunderstackJobContext, BunderstackJobsBuilder, DedupeUntil, EnqueueOptions, EnqueueTransaction, JobContext, JobDefinition, QueueJobDefinition, CronDefinition, CronInvocation, QueueJobKeys, TypedEnqueueOptions, JobsDefs, JobsFacade, JobsRuntimeFacade, } from './define.js';
|
|
3
|
+
export { enqueueJob, enqueueTarget } from './queue.js';
|
|
4
4
|
export { createJobRunner } from './worker.js';
|
|
5
5
|
export { startJobWorker } from './runtime.js';
|
|
6
6
|
export type { StartWorkerOptions, RunWorkerOptions, WorkerCycleResult, WorkerHandle, } from './runtime.js';
|
package/dist/jobs/index.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/jobs/index.ts"],"names":[],"mappings":"AACA,OAAO,EACL,iBAAiB,EACjB,sBAAsB,EACtB,gBAAgB,EAChB,eAAe,EACf,yBAAyB,EACzB,kBAAkB,EAClB,gBAAgB,GACjB,MAAM,UAAU,CAAA;AACjB,YAAY,EACV,gBAAgB,EAChB,gBAAgB,EAChB,oBAAoB,EACpB,cAAc,EACd,qBAAqB,EACrB,sBAAsB,EACtB,cAAc,EACd,UAAU,EACV,aAAa,EACb,kBAAkB,EAClB,cAAc,EACd,cAAc,EACd,YAAY,EACZ,QAAQ,EACR,UAAU,EACV,iBAAiB,GAClB,MAAM,UAAU,CAAA;AACjB,OAAO,EAAE,UAAU,EAAE,MAAM,SAAS,CAAA;
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/jobs/index.ts"],"names":[],"mappings":"AACA,OAAO,EACL,iBAAiB,EACjB,sBAAsB,EACtB,gBAAgB,EAChB,eAAe,EACf,yBAAyB,EACzB,kBAAkB,EAClB,gBAAgB,GACjB,MAAM,UAAU,CAAA;AACjB,YAAY,EACV,gBAAgB,EAChB,gBAAgB,EAChB,oBAAoB,EACpB,cAAc,EACd,qBAAqB,EACrB,sBAAsB,EACtB,WAAW,EACX,cAAc,EACd,kBAAkB,EAClB,UAAU,EACV,aAAa,EACb,kBAAkB,EAClB,cAAc,EACd,cAAc,EACd,YAAY,EACZ,mBAAmB,EACnB,QAAQ,EACR,UAAU,EACV,iBAAiB,GAClB,MAAM,UAAU,CAAA;AACjB,OAAO,EAAE,UAAU,EAAE,aAAa,EAAE,MAAM,SAAS,CAAA;AACnD,OAAO,EAAE,eAAe,EAAE,MAAM,UAAU,CAAA;AAC1C,OAAO,EAAE,cAAc,EAAE,MAAM,WAAW,CAAA;AAC1C,YAAY,EACV,kBAAkB,EAClB,gBAAgB,EAChB,iBAAiB,EACjB,YAAY,GACb,MAAM,WAAW,CAAA;AAClB,OAAO,EAAE,SAAS,EAAE,WAAW,EAAE,MAAM,QAAQ,CAAA;AAC/C,OAAO,EAAE,QAAQ,EAAE,SAAS,EAAE,WAAW,EAAE,OAAO,EAAE,MAAM,SAAS,CAAA;AACnE,YAAY,EAAE,OAAO,EAAE,MAAM,SAAS,CAAA;AACtC,YAAY,EAAE,UAAU,EAAE,MAAM,UAAU,CAAA"}
|
package/dist/jobs/index.js
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
// src/jobs/index.ts — module surface consumed by bunderstack.
|
|
2
2
|
export { createJobsBuilder, validateBackgroundDefs, validateJobsDefs, DEFAULT_RETRIES, DEFAULT_LEASE_DURATION_MS, DEFAULT_TIMEOUT_MS, leaseDurationFor, } from './define.js';
|
|
3
|
-
export { enqueueJob } from './queue.js';
|
|
3
|
+
export { enqueueJob, enqueueTarget } from './queue.js';
|
|
4
4
|
export { createJobRunner } from './worker.js';
|
|
5
5
|
export { startJobWorker } from './runtime.js';
|
|
6
6
|
export { parseCron, cronMatches } from './cron.js';
|
package/dist/jobs/index.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/jobs/index.ts"],"names":[],"mappings":"AAAA,8DAA8D;AAC9D,OAAO,EACL,iBAAiB,EACjB,sBAAsB,EACtB,gBAAgB,EAChB,eAAe,EACf,yBAAyB,EACzB,kBAAkB,EAClB,gBAAgB,GACjB,MAAM,UAAU,CAAA;
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/jobs/index.ts"],"names":[],"mappings":"AAAA,8DAA8D;AAC9D,OAAO,EACL,iBAAiB,EACjB,sBAAsB,EACtB,gBAAgB,EAChB,eAAe,EACf,yBAAyB,EACzB,kBAAkB,EAClB,gBAAgB,GACjB,MAAM,UAAU,CAAA;AAsBjB,OAAO,EAAE,UAAU,EAAE,aAAa,EAAE,MAAM,SAAS,CAAA;AACnD,OAAO,EAAE,eAAe,EAAE,MAAM,UAAU,CAAA;AAC1C,OAAO,EAAE,cAAc,EAAE,MAAM,WAAW,CAAA;AAO1C,OAAO,EAAE,SAAS,EAAE,WAAW,EAAE,MAAM,QAAQ,CAAA;AAC/C,OAAO,EAAE,QAAQ,EAAE,SAAS,EAAE,WAAW,EAAE,OAAO,EAAE,MAAM,SAAS,CAAA","sourcesContent":["// src/jobs/index.ts — module surface consumed by bunderstack.\nexport {\n createJobsBuilder,\n validateBackgroundDefs,\n validateJobsDefs,\n DEFAULT_RETRIES,\n DEFAULT_LEASE_DURATION_MS,\n DEFAULT_TIMEOUT_MS,\n leaseDurationFor,\n} from './define'\nexport type {\n AnyJobDefinition,\n BackgroundTiming,\n BackgroundDefinition,\n BackgroundDefs,\n BunderstackJobContext,\n BunderstackJobsBuilder,\n DedupeUntil,\n EnqueueOptions,\n EnqueueTransaction,\n JobContext,\n JobDefinition,\n QueueJobDefinition,\n CronDefinition,\n CronInvocation,\n QueueJobKeys,\n TypedEnqueueOptions,\n JobsDefs,\n JobsFacade,\n JobsRuntimeFacade,\n} from './define'\nexport { enqueueJob, enqueueTarget } from './queue'\nexport { createJobRunner } from './worker'\nexport { startJobWorker } from './runtime'\nexport type {\n StartWorkerOptions,\n RunWorkerOptions,\n WorkerCycleResult,\n WorkerHandle,\n} from './runtime'\nexport { parseCron, cronMatches } from './cron'\nexport { slotsDue, floorSlot, CRON_PREFIX, SLOT_MS } from './slots'\nexport type { CatchUp } from './slots'\nexport type { TickResult } from './define'\n"]}
|
package/dist/jobs/queue.d.ts
CHANGED
|
@@ -1,5 +1,13 @@
|
|
|
1
1
|
import type { AnyDb } from '../dialect.js';
|
|
2
2
|
import type { EnqueueOptions, JobsDefs } from './define.js';
|
|
3
|
+
/**
|
|
4
|
+
* The handle an enqueue inserts through: the caller's transaction when given,
|
|
5
|
+
* otherwise the app database. Drizzle transactions extend their dialect's
|
|
6
|
+
* database class, so the internal jobs table resolves the same way for both.
|
|
7
|
+
* A transaction from an unrelated connection of the same dialect cannot be
|
|
8
|
+
* detected cheaply and is unsupported.
|
|
9
|
+
*/
|
|
10
|
+
export declare function enqueueTarget(db: AnyDb, tx: AnyDb | undefined): AnyDb;
|
|
3
11
|
export declare function enqueueJob(db: AnyDb, defs: JobsDefs, name: string, input: unknown, opts?: EnqueueOptions, now?: number): Promise<{
|
|
4
12
|
id: string;
|
|
5
13
|
}>;
|
package/dist/jobs/queue.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"queue.d.ts","sourceRoot":"","sources":["../../src/jobs/queue.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"queue.d.ts","sourceRoot":"","sources":["../../src/jobs/queue.ts"],"names":[],"mappings":"AAKA,OAAO,KAAK,EAAE,KAAK,EAAW,MAAM,YAAY,CAAA;AAChD,OAAO,KAAK,EAAE,cAAc,EAAE,QAAQ,EAAE,MAAM,UAAU,CAAA;AAaxD;;;;;;GAMG;AACH,wBAAgB,aAAa,CAAC,EAAE,EAAE,KAAK,EAAE,EAAE,EAAE,KAAK,GAAG,SAAS,GAAG,KAAK,CAQrE;AAED,wBAAsB,UAAU,CAC9B,EAAE,EAAE,KAAK,EACT,IAAI,EAAE,QAAQ,EACd,IAAI,EAAE,MAAM,EACZ,KAAK,EAAE,OAAO,EACd,IAAI,GAAE,cAAmB,EACzB,GAAG,GAAE,MAAmB,GACvB,OAAO,CAAC;IAAE,EAAE,EAAE,MAAM,CAAA;CAAE,CAAC,CAgDzB"}
|
package/dist/jobs/queue.js
CHANGED
|
@@ -1,9 +1,33 @@
|
|
|
1
1
|
// src/jobs/queue.ts — durable enqueue with constraint-backed dedupe.
|
|
2
|
-
import { and, eq } from 'drizzle-orm';
|
|
2
|
+
import { and, eq, is } from 'drizzle-orm';
|
|
3
|
+
import { PgDatabase } from 'drizzle-orm/pg-core';
|
|
4
|
+
import { BaseSQLiteDatabase } from 'drizzle-orm/sqlite-core';
|
|
3
5
|
import { jobsTableFor } from '../internal-tables.js';
|
|
4
6
|
import { validateStandardSchema } from '../standard-schema.js';
|
|
5
7
|
import { generate } from '../typeid.js';
|
|
6
8
|
import { CRON_PREFIX } from './slots.js';
|
|
9
|
+
function handleDialect(handle) {
|
|
10
|
+
if (is(handle, PgDatabase))
|
|
11
|
+
return 'pg';
|
|
12
|
+
if (is(handle, BaseSQLiteDatabase))
|
|
13
|
+
return 'sqlite';
|
|
14
|
+
return undefined;
|
|
15
|
+
}
|
|
16
|
+
/**
|
|
17
|
+
* The handle an enqueue inserts through: the caller's transaction when given,
|
|
18
|
+
* otherwise the app database. Drizzle transactions extend their dialect's
|
|
19
|
+
* database class, so the internal jobs table resolves the same way for both.
|
|
20
|
+
* A transaction from an unrelated connection of the same dialect cannot be
|
|
21
|
+
* detected cheaply and is unsupported.
|
|
22
|
+
*/
|
|
23
|
+
export function enqueueTarget(db, tx) {
|
|
24
|
+
if (tx === undefined)
|
|
25
|
+
return db;
|
|
26
|
+
if (handleDialect(tx) !== handleDialect(db)) {
|
|
27
|
+
throw new Error('[bunderstack] enqueue tx belongs to a different database dialect');
|
|
28
|
+
}
|
|
29
|
+
return tx;
|
|
30
|
+
}
|
|
7
31
|
export async function enqueueJob(db, defs, name, input, opts = {}, now = Date.now()) {
|
|
8
32
|
const def = defs[name];
|
|
9
33
|
if (!def) {
|
package/dist/jobs/queue.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"queue.js","sourceRoot":"","sources":["../../src/jobs/queue.ts"],"names":[],"mappings":"AAAA,qEAAqE;AACrE,OAAO,EAAE,GAAG,EAAE,EAAE,EAAE,MAAM,aAAa,CAAA;
|
|
1
|
+
{"version":3,"file":"queue.js","sourceRoot":"","sources":["../../src/jobs/queue.ts"],"names":[],"mappings":"AAAA,qEAAqE;AACrE,OAAO,EAAE,GAAG,EAAE,EAAE,EAAE,EAAE,EAAE,MAAM,aAAa,CAAA;AACzC,OAAO,EAAE,UAAU,EAAE,MAAM,qBAAqB,CAAA;AAChD,OAAO,EAAE,kBAAkB,EAAE,MAAM,yBAAyB,CAAA;AAK5D,OAAO,EAAE,YAAY,EAAE,MAAM,oBAAoB,CAAA;AACjD,OAAO,EAAE,sBAAsB,EAAE,MAAM,oBAAoB,CAAA;AAC3D,OAAO,EAAE,QAAQ,EAAE,MAAM,WAAW,CAAA;AACpC,OAAO,EAAE,WAAW,EAAE,MAAM,SAAS,CAAA;AAErC,SAAS,aAAa,CAAC,MAAe;IACpC,IAAI,EAAE,CAAC,MAAM,EAAE,UAAU,CAAC;QAAE,OAAO,IAAI,CAAA;IACvC,IAAI,EAAE,CAAC,MAAM,EAAE,kBAAkB,CAAC;QAAE,OAAO,QAAQ,CAAA;IACnD,OA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AAE,EAAE,MAAM,CAAC,QAAQ,CAAC,CAAC,CAAC,CAAC,EAAE,CAAC,EAAE,CAAA;IACxD,CAAC;IACD,MAAM,IAAI,KAAK,CACb,6BAA6B,IAAI,2CAA2C,CAC7E,CAAA;AACH,CAAC","sourcesContent":["// src/jobs/queue.ts — durable enqueue with constraint-backed dedupe.\nimport { and, eq, is } from 'drizzle-orm'\nimport { PgDatabase } from 'drizzle-orm/pg-core'\nimport { BaseSQLiteDatabase } from 'drizzle-orm/sqlite-core'\n\nimport type { AnyDb, Dialect } from '../dialect'\nimport type { EnqueueOptions, JobsDefs } from './define'\n\nimport { jobsTableFor } from '../internal-tables'\nimport { validateStandardSchema } from '../standard-schema'\nimport { generate } from '../typeid'\nimport { CRON_PREFIX } from './slots'\n\nfunction handleDialect(handle: unknown): Dialect | undefined {\n if (is(handle, PgDatabase)) return 'pg'\n if (is(handle, BaseSQLiteDatabase)) return 'sqlite'\n return undefined\n}\n\n/**\n * The handle an enqueue inserts through: the caller's transaction when given,\n * otherwise the app database. Drizzle transactions extend their dialect's\n * database class, so the internal jobs table resolves the same way for both.\n * A transaction from an unrelated connection of the same dialect cannot be\n * detected cheaply and is unsupported.\n */\nexport function enqueueTarget(db: AnyDb, tx: AnyDb | undefined): AnyDb {\n if (tx === undefined) return db\n if (handleDialect(tx) !== handleDialect(db)) {\n throw new Error(\n '[bunderstack] enqueue tx belongs to a different database dialect',\n )\n }\n return tx\n}\n\nexport async function enqueueJob(\n db: AnyDb,\n defs: JobsDefs,\n name: string,\n input: unknown,\n opts: EnqueueOptions = {},\n now: number = Date.now(),\n): Promise<{ id: string }> {\n const def = defs[name]\n if (!def) {\n throw new Error(`[bunderstack] unknown background task \"${name}\"`)\n }\n const isCron = def.kind === 'cron'\n const type = isCron ? `${CRON_PREFIX}${name}` : name\n // Cron slots carry no payload; queue jobs validate theirs at the call site.\n const parsed = isCron\n ? null\n : def.input\n ? validateStandardSchema(def.input, input, `job \"${name}\" input`)\n : null\n const t = jobsTableFor(db)\n const runAt =\n opts.runAt !== undefined\n ? new Date(opts.runAt).getTime()\n : now + (opts.delay ?? 0)\n\n // Two rounds cover the race where the deduping row reaches a terminal state\n // (clearing its key) between our failed insert and our read.\n for (let round = 0; round < 2; round++) {\n const id = generate('job')\n const insertedRows = await db\n .insert(t)\n .values({\n id,\n type,\n payloadJson: JSON.stringify(parsed),\n status: 'pending',\n attempts: 0,\n runAt,\n dedupeKey: opts.dedupeKey ?? null,\n createdAt: now,\n })\n .onConflictDoNothing({ target: [t.type, t.dedupeKey] })\n .returning({ id: t.id })\n if (insertedRows[0]) return { id: String(insertedRows[0].id) }\n const existing = await db\n .select({ id: t.id })\n .from(t)\n .where(and(eq(t.type, type), eq(t.dedupeKey, opts.dedupeKey ?? '')))\n .limit(1)\n if (existing[0]) return { id: String(existing[0].id) }\n }\n throw new Error(\n `[bunderstack] enqueue of \"${name}\" lost a dedupe race twice — please retry`,\n )\n}\n"]}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"worker.d.ts","sourceRoot":"","sources":["../../src/jobs/worker.ts"],"names":[],"mappings":"AAeA,OAAO,KAAK,EAAE,KAAK,EAAE,MAAM,YAAY,CAAA;AACvC,OAAO,KAAK,EAAE,iBAAiB,EAAE,MAAM,YAAY,CAAA;AACnD,OAAO,KAAK,EAEV,QAAQ,EACR,iBAAiB,EACjB,UAAU,EACX,MAAM,UAAU,CAAA;AAqDjB,KAAK,UAAU,GAAG;IAAE,IAAI,CAAC,EAAE,OAAO,CAAC,IAAI,CAAC,CAAA;CAAE,CAAA;
|
|
1
|
+
{"version":3,"file":"worker.d.ts","sourceRoot":"","sources":["../../src/jobs/worker.ts"],"names":[],"mappings":"AAeA,OAAO,KAAK,EAAE,KAAK,EAAE,MAAM,YAAY,CAAA;AACvC,OAAO,KAAK,EAAE,iBAAiB,EAAE,MAAM,YAAY,CAAA;AACnD,OAAO,KAAK,EAEV,QAAQ,EACR,iBAAiB,EACjB,UAAU,EACX,MAAM,UAAU,CAAA;AAqDjB,KAAK,UAAU,GAAG;IAAE,IAAI,CAAC,EAAE,OAAO,CAAC,IAAI,CAAC,CAAA;CAAE,CAAA;AA8D1C,wBAAgB,eAAe,CAAC,IAAI,EAAE;IACpC,EAAE,EAAE,KAAK,CAAA;IACT,IAAI,EAAE,QAAQ,CAAA;IACd,4EAA4E;IAC5E,GAAG,EAAE,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,CAAA;IAC5B,MAAM,CAAC,EAAE,iBAAiB,CAAA;CAC3B;eAugBmB,MAAM,GAAgB,OAAO,CAAC,UAAU,CAAC;iBAnBlC,MAAM,KAAgB,OAAO,CAAC,UAAU,CAAC;iBAa1C,OAAO,CAAC,IAAI,CAAC;iBAYhB,MAAM;;;;;;;;;;;;oBAiEb,SAAS,GACT,SAAS,GACT,WAAW,GACX,QAAQ;;;;;;;qBAQH,iBAAiB;EAIrC"}
|
package/dist/jobs/worker.js
CHANGED
|
@@ -59,6 +59,13 @@ function definitionFor(defs, type) {
|
|
|
59
59
|
function terminalPatch(def) {
|
|
60
60
|
return def.kind === 'cron' ? {} : { dedupeKey: null };
|
|
61
61
|
}
|
|
62
|
+
/** `dedupeUntil: 'start'` releases the key at claim; everything else keeps it
|
|
63
|
+
* until a terminal state, so the claim patch stays unchanged for them. */
|
|
64
|
+
function claimPatch(def) {
|
|
65
|
+
return def.kind === 'job' && def.dedupeUntil === 'start'
|
|
66
|
+
? { dedupeKey: null }
|
|
67
|
+
: {};
|
|
68
|
+
}
|
|
62
69
|
export function createJobRunner(deps) {
|
|
63
70
|
const { db, defs } = deps;
|
|
64
71
|
const logger = deps.logger ?? consoleLogger;
|
|
@@ -248,7 +255,7 @@ export function createJobRunner(deps) {
|
|
|
248
255
|
.where(and(eq(t.status, 'succeeded'), lt(t.finishedAt, now - SUCCEEDED_RETENTION_MS)));
|
|
249
256
|
}
|
|
250
257
|
/** Atomically claim up to `limit` runnable jobs of one type. */
|
|
251
|
-
async function claim(type, limit, now, leaseUntil) {
|
|
258
|
+
async function claim(type, def, limit, now, leaseUntil) {
|
|
252
259
|
const pendingIds = db
|
|
253
260
|
.select({ id: t.id })
|
|
254
261
|
.from(t)
|
|
@@ -266,6 +273,7 @@ export function createJobRunner(deps) {
|
|
|
266
273
|
status: 'running',
|
|
267
274
|
lockedUntil: leaseUntil,
|
|
268
275
|
attempts: sql `${t.attempts} + 1`,
|
|
276
|
+
...claimPatch(def),
|
|
269
277
|
})
|
|
270
278
|
.where(and(inArray(t.id, sub), eq(t.status, 'pending')))
|
|
271
279
|
.returning({
|
|
@@ -289,7 +297,7 @@ export function createJobRunner(deps) {
|
|
|
289
297
|
const work = [];
|
|
290
298
|
while (available > 0) {
|
|
291
299
|
const limit = Math.min(CLAIM_BATCH, available);
|
|
292
|
-
const rows = await claim(type, limit, now, leaseUntil);
|
|
300
|
+
const rows = await claim(type, def, limit, now, leaseUntil);
|
|
293
301
|
for (const row of rows) {
|
|
294
302
|
work.push({
|
|
295
303
|
row,
|