@fluixi/jobs 0.0.0-stage → 1.0.0-alpha.108
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/LICENSE +21 -0
- package/README.md +60 -2
- package/dist/define.cjs +44 -0
- package/dist/define.d.ts +60 -0
- package/dist/define.d.ts.map +1 -0
- package/dist/define.js +18 -0
- package/dist/define.mjs +18 -0
- package/dist/engine.cjs +18 -0
- package/dist/engine.d.ts +58 -0
- package/dist/engine.d.ts.map +1 -0
- package/dist/engine.js +5 -0
- package/dist/engine.mjs +0 -0
- package/dist/engines/memory.cjs +91 -0
- package/dist/engines/memory.d.ts +7 -0
- package/dist/engines/memory.d.ts.map +1 -0
- package/dist/engines/memory.js +70 -0
- package/dist/engines/memory.mjs +67 -0
- package/dist/engines/pg-boss.cjs +121 -0
- package/dist/engines/pg-boss.d.ts +17 -0
- package/dist/engines/pg-boss.d.ts.map +1 -0
- package/dist/engines/pg-boss.js +79 -0
- package/dist/engines/pg-boss.mjs +96 -0
- package/dist/engines/pglite.cjs +43 -0
- package/dist/engines/pglite.d.ts +24 -0
- package/dist/engines/pglite.d.ts.map +1 -0
- package/dist/engines/pglite.js +20 -0
- package/dist/engines/pglite.mjs +18 -0
- package/dist/index.cjs +194 -0
- package/dist/index.d.ts +5 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +3 -0
- package/dist/index.mjs +164 -0
- package/dist/key.cjs +39 -0
- package/dist/key.d.ts +6 -0
- package/dist/key.d.ts.map +1 -0
- package/dist/key.js +15 -0
- package/dist/key.mjs +14 -0
- package/dist/queue.cjs +170 -0
- package/dist/queue.d.ts +84 -0
- package/dist/queue.d.ts.map +1 -0
- package/dist/queue.js +141 -0
- package/dist/queue.mjs +144 -0
- package/dist/tsconfig.lib.tsbuildinfo +1 -0
- package/package.json +73 -3
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 Ibrahima Touré
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
CHANGED
|
@@ -1,3 +1,61 @@
|
|
|
1
|
-
#
|
|
1
|
+
# @fluixi/jobs
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
Background jobs for Fluixi apps: a job is declared once, sent from anywhere on the server, and
|
|
4
|
+
run by a worker, with retries, idempotent sends and the sending request's context. The engine
|
|
5
|
+
underneath is yours to choose.
|
|
6
|
+
|
|
7
|
+
```ts
|
|
8
|
+
import { getLocals } from '@fluixi/dom';
|
|
9
|
+
import { createJobs, defineJob } from '@fluixi/jobs';
|
|
10
|
+
import { pgBossEngine } from '@fluixi/jobs/pg-boss';
|
|
11
|
+
import { z } from 'zod';
|
|
12
|
+
|
|
13
|
+
export const sendInvitation = defineJob({
|
|
14
|
+
name: 'invitations.send',
|
|
15
|
+
schema: z.object({ invitationId: z.uuid() }),
|
|
16
|
+
retry: { limit: 5, backoff: true },
|
|
17
|
+
handler: async ({ invitationId }, run) => {
|
|
18
|
+
// run.attempt, run.context; getLocals() holds what captureContext took
|
|
19
|
+
},
|
|
20
|
+
});
|
|
21
|
+
|
|
22
|
+
export const jobs = createJobs({
|
|
23
|
+
engine: pgBossEngine({ connectionString: process.env.DATABASE_URL_DIRECT }),
|
|
24
|
+
jobs: [sendInvitation],
|
|
25
|
+
captureContext: () => ({ organizationId: getLocals().organizationId }),
|
|
26
|
+
});
|
|
27
|
+
|
|
28
|
+
await jobs.start();
|
|
29
|
+
await jobs.send(sendInvitation, { invitationId }, { key: `invite:${invitationId}` });
|
|
30
|
+
```
|
|
31
|
+
|
|
32
|
+
- **Delivery**: at least once. A handler may run again after a crash or a timeout, so it is
|
|
33
|
+
written to be idempotent.
|
|
34
|
+
- **Data**: checked with the job's schema (any Standard Schema validator) when sent and again
|
|
35
|
+
before the handler runs; a mismatch at send throws `JobDataError`.
|
|
36
|
+
- **Idempotency**: `key` names the job. A second send with the same key, before or after the
|
|
37
|
+
first ran, creates nothing and answers `null`, for as long as the job is kept (14 days by
|
|
38
|
+
default, `retentionDays`).
|
|
39
|
+
- **Context**: `captureContext` runs when a job is sent from a request; the handler runs inside a
|
|
40
|
+
request context whose locals hold what it took.
|
|
41
|
+
- **Workers**: `jobs.work()` runs the handlers; `jobs.stop({ timeoutSeconds })` stops taking
|
|
42
|
+
jobs and waits for the running ones.
|
|
43
|
+
- **Schedules**: `createJobs({ schedules })` takes recurring sends declared with `@fluixi/cron`;
|
|
44
|
+
`jobs.work()` puts them in place, removing the ones no longer declared.
|
|
45
|
+
|
|
46
|
+
## Engines
|
|
47
|
+
|
|
48
|
+
| Engine | Import | For |
|
|
49
|
+
|---|---|---|
|
|
50
|
+
| `pgBossEngine({ connectionString })` | `@fluixi/jobs/pg-boss` | a Postgres server |
|
|
51
|
+
| `pgBossEngine({ pglite })` | `@fluixi/jobs/pg-boss` | embedded PGlite, one process |
|
|
52
|
+
| `memoryEngine()` | `@fluixi/jobs/memory` | tests and prototypes; schedules are kept, never fired |
|
|
53
|
+
|
|
54
|
+
The pg-boss engine wakes workers with Postgres listen/notify the moment a job is sent, and polls
|
|
55
|
+
every 30 seconds as a safety net (`pollSeconds`). Give it a direct address: a connection pooler
|
|
56
|
+
in transaction mode drops the listen. `pg-boss` and `@electric-sql/pglite` are optional peers,
|
|
57
|
+
installed by the apps that use them.
|
|
58
|
+
|
|
59
|
+
An engine is an object implementing `JobEngine`; another queue can be added the same way.
|
|
60
|
+
Hosted services (Inngest, Trigger.dev, Upstash QStash) work beside Fluixi through their own
|
|
61
|
+
SDKs: their endpoints are web request handlers an API route serves as is.
|
package/dist/define.cjs
ADDED
|
@@ -0,0 +1,44 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __defProp = Object.defineProperty;
|
|
3
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
4
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
5
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
6
|
+
var __export = (target, all) => {
|
|
7
|
+
for (var name in all)
|
|
8
|
+
__defProp(target, name, { get: all[name], enumerable: true });
|
|
9
|
+
};
|
|
10
|
+
var __copyProps = (to, from, except, desc) => {
|
|
11
|
+
if (from && typeof from === "object" || typeof from === "function") {
|
|
12
|
+
for (let key of __getOwnPropNames(from))
|
|
13
|
+
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
14
|
+
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
15
|
+
}
|
|
16
|
+
return to;
|
|
17
|
+
};
|
|
18
|
+
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
19
|
+
|
|
20
|
+
// src/define.ts
|
|
21
|
+
var define_exports = {};
|
|
22
|
+
__export(define_exports, {
|
|
23
|
+
JobDefinitionError: () => JobDefinitionError,
|
|
24
|
+
defineJob: () => defineJob
|
|
25
|
+
});
|
|
26
|
+
module.exports = __toCommonJS(define_exports);
|
|
27
|
+
var NAME = /^[a-z][a-z0-9]*(?:[._-][a-z0-9]+)*$/;
|
|
28
|
+
var JobDefinitionError = class extends Error {
|
|
29
|
+
};
|
|
30
|
+
function defineJob(definition) {
|
|
31
|
+
if (!NAME.test(definition.name)) throw new JobDefinitionError(`job name "${definition.name}" must be lowercase, dotted: "invitations.send"`);
|
|
32
|
+
return Object.freeze({
|
|
33
|
+
name: definition.name,
|
|
34
|
+
schema: definition.schema,
|
|
35
|
+
retry: { limit: definition.retry?.limit ?? 3, delaySeconds: definition.retry?.delaySeconds ?? 10, backoff: definition.retry?.backoff ?? true },
|
|
36
|
+
timeoutSeconds: definition.timeoutSeconds ?? 15 * 60,
|
|
37
|
+
handler: definition.handler
|
|
38
|
+
});
|
|
39
|
+
}
|
|
40
|
+
// Annotate the CommonJS export names for ESM import in node:
|
|
41
|
+
0 && (module.exports = {
|
|
42
|
+
JobDefinitionError,
|
|
43
|
+
defineJob
|
|
44
|
+
});
|
package/dist/define.d.ts
ADDED
|
@@ -0,0 +1,60 @@
|
|
|
1
|
+
/** A Standard Schema validator (zod, valibot, arktype...): checks a job's data at send and before it runs. */
|
|
2
|
+
export interface StandardSchema<Output = unknown> {
|
|
3
|
+
readonly '~standard': {
|
|
4
|
+
readonly version: 1;
|
|
5
|
+
readonly vendor: string;
|
|
6
|
+
readonly validate: (value: unknown) => {
|
|
7
|
+
value: Output;
|
|
8
|
+
issues?: undefined;
|
|
9
|
+
} | {
|
|
10
|
+
issues: readonly {
|
|
11
|
+
message: string;
|
|
12
|
+
}[];
|
|
13
|
+
} | Promise<{
|
|
14
|
+
value: Output;
|
|
15
|
+
issues?: undefined;
|
|
16
|
+
} | {
|
|
17
|
+
issues: readonly {
|
|
18
|
+
message: string;
|
|
19
|
+
}[];
|
|
20
|
+
}>;
|
|
21
|
+
};
|
|
22
|
+
}
|
|
23
|
+
export interface RetryPolicy {
|
|
24
|
+
/** Attempts after the first. */
|
|
25
|
+
limit: number;
|
|
26
|
+
/** Seconds before the first retry; doubled each time with `backoff`. */
|
|
27
|
+
delaySeconds?: number;
|
|
28
|
+
backoff?: boolean;
|
|
29
|
+
}
|
|
30
|
+
/** What a handler knows besides the data. */
|
|
31
|
+
export interface JobRun {
|
|
32
|
+
id: string;
|
|
33
|
+
/** 0 on the first attempt. */
|
|
34
|
+
attempt: number;
|
|
35
|
+
/** What `captureContext` took when the job was sent; also the locals of the request context around the handler. */
|
|
36
|
+
context: Record<string, unknown>;
|
|
37
|
+
}
|
|
38
|
+
export interface JobDefinition<Data = unknown> {
|
|
39
|
+
/** Unique, dotted: `invitations.send`. Also the queue's name. */
|
|
40
|
+
readonly name: string;
|
|
41
|
+
readonly schema?: StandardSchema<Data>;
|
|
42
|
+
readonly retry: RetryPolicy;
|
|
43
|
+
/** A run longer than this is failed and retried. */
|
|
44
|
+
readonly timeoutSeconds: number;
|
|
45
|
+
readonly handler: (data: Data, run: JobRun) => Promise<void> | void;
|
|
46
|
+
}
|
|
47
|
+
export declare class JobDefinitionError extends Error {
|
|
48
|
+
}
|
|
49
|
+
/**
|
|
50
|
+
* A background job. Delivery is at least once: a handler may run again after a crash or a
|
|
51
|
+
* timeout, so it must be idempotent.
|
|
52
|
+
*/
|
|
53
|
+
export declare function defineJob<Data>(definition: {
|
|
54
|
+
name: string;
|
|
55
|
+
schema?: StandardSchema<Data>;
|
|
56
|
+
retry?: Partial<RetryPolicy>;
|
|
57
|
+
timeoutSeconds?: number;
|
|
58
|
+
handler: (data: Data, run: JobRun) => Promise<void> | void;
|
|
59
|
+
}): JobDefinition<Data>;
|
|
60
|
+
//# sourceMappingURL=define.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"define.d.ts","sourceRo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QAAQ,CAAC,cAAc,EAAE,MAAM,CAAC;IAChC,QAAQ,CAAC,OAAO,EAAE,CAAC,IAAI,EAAE,IAAI,EAAE,GAAG,EAAE,MAAM,KAAK,OAAO,CAAC,IAAI,CAAC,GAAG,IAAI,CAAC;CACrE;AAID,qBAAa,kBAAmB,SAAQ,KAAK;CAAG;AAEhD;;;GAGG;AACH,wBAAgB,SAAS,CAAC,IAAI,EAAE,UAAU,EAAE;IAC1C,IAAI,EAAE,MAAM,CAAC;IACb,MAAM,CAAC,EAAE,cAAc,CAAC,IAAI,CAAC,CAAC;IAC9B,KAAK,CAAC,EAAE,OAAO,CAAC,WAAW,CAAC,CAAC;IAC7B,cAAc,CAAC,EAAE,MAAM,CAAC;IACxB,OAAO,EAAE,CAAC,IAAI,EAAE,IAAI,EAAE,GAAG,EAAE,MAAM,KAAK,OAAO,CAAC,IAAI,CAAC,GAAG,IAAI,CAAC;CAC5D,GAAG,aAAa,CAAC,IAAI,CAAC,CAStB"}
|
package/dist/define.js
ADDED
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
const NAME = /^[a-z][a-z0-9]*(?:[._-][a-z0-9]+)*$/;
|
|
2
|
+
export class JobDefinitionError extends Error {
|
|
3
|
+
}
|
|
4
|
+
/**
|
|
5
|
+
* A background job. Delivery is at least once: a handler may run again after a crash or a
|
|
6
|
+
* timeout, so it must be idempotent.
|
|
7
|
+
*/
|
|
8
|
+
export function defineJob(definition) {
|
|
9
|
+
if (!NAME.test(definition.name))
|
|
10
|
+
throw new JobDefinitionError(`job name "${definition.name}" must be lowercase, dotted: "invitations.send"`);
|
|
11
|
+
return Object.freeze({
|
|
12
|
+
name: definition.name,
|
|
13
|
+
schema: definition.schema,
|
|
14
|
+
retry: { limit: definition.retry?.limit ?? 3, delaySeconds: definition.retry?.delaySeconds ?? 10, backoff: definition.retry?.backoff ?? true },
|
|
15
|
+
timeoutSeconds: definition.timeoutSeconds ?? 15 * 60,
|
|
16
|
+
handler: definition.handler,
|
|
17
|
+
});
|
|
18
|
+
}
|
package/dist/define.mjs
ADDED
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
// src/define.ts
|
|
2
|
+
var NAME = /^[a-z][a-z0-9]*(?:[._-][a-z0-9]+)*$/;
|
|
3
|
+
var JobDefinitionError = class extends Error {
|
|
4
|
+
};
|
|
5
|
+
function defineJob(definition) {
|
|
6
|
+
if (!NAME.test(definition.name)) throw new JobDefinitionError(`job name "${definition.name}" must be lowercase, dotted: "invitations.send"`);
|
|
7
|
+
return Object.freeze({
|
|
8
|
+
name: definition.name,
|
|
9
|
+
schema: definition.schema,
|
|
10
|
+
retry: { limit: definition.retry?.limit ?? 3, delaySeconds: definition.retry?.delaySeconds ?? 10, backoff: definition.retry?.backoff ?? true },
|
|
11
|
+
timeoutSeconds: definition.timeoutSeconds ?? 15 * 60,
|
|
12
|
+
handler: definition.handler
|
|
13
|
+
});
|
|
14
|
+
}
|
|
15
|
+
export {
|
|
16
|
+
JobDefinitionError,
|
|
17
|
+
defineJob
|
|
18
|
+
};
|
package/dist/engine.cjs
ADDED
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __defProp = Object.defineProperty;
|
|
3
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
4
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
5
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
6
|
+
var __copyProps = (to, from, except, desc) => {
|
|
7
|
+
if (from && typeof from === "object" || typeof from === "function") {
|
|
8
|
+
for (let key of __getOwnPropNames(from))
|
|
9
|
+
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
10
|
+
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
11
|
+
}
|
|
12
|
+
return to;
|
|
13
|
+
};
|
|
14
|
+
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
15
|
+
|
|
16
|
+
// src/engine.ts
|
|
17
|
+
var engine_exports = {};
|
|
18
|
+
module.exports = __toCommonJS(engine_exports);
|
package/dist/engine.d.ts
ADDED
|
@@ -0,0 +1,58 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* What a job engine does for `@fluixi/jobs`. The core knows nothing of the engine behind: pg-boss
|
|
3
|
+
* on Postgres (`@fluixi/jobs/pg-boss`), memory for tests (`@fluixi/jobs/memory`), others later.
|
|
4
|
+
*/
|
|
5
|
+
/** A queue, one per job, with the job's delivery settings. */
|
|
6
|
+
export interface QueueSpec {
|
|
7
|
+
name: string;
|
|
8
|
+
retryLimit: number;
|
|
9
|
+
retryDelaySeconds: number;
|
|
10
|
+
retryBackoff: boolean;
|
|
11
|
+
timeoutSeconds: number;
|
|
12
|
+
/** How long a finished job is kept, which is how long its id stays taken. */
|
|
13
|
+
retentionSeconds: number;
|
|
14
|
+
}
|
|
15
|
+
/** A job as the engine stores it: the core's envelope, opaque to the engine. */
|
|
16
|
+
export interface StoredJob {
|
|
17
|
+
id: string;
|
|
18
|
+
/** 0 on the first attempt. */
|
|
19
|
+
attempt: number;
|
|
20
|
+
payload: unknown;
|
|
21
|
+
}
|
|
22
|
+
export interface EngineSendOptions {
|
|
23
|
+
/** An id the caller chose: a second send with it creates nothing and answers null. */
|
|
24
|
+
id?: string;
|
|
25
|
+
startAfter?: Date;
|
|
26
|
+
priority?: number;
|
|
27
|
+
}
|
|
28
|
+
/** A recurring enqueue of `queue`, identified by `key` within it. */
|
|
29
|
+
export interface EngineSchedule {
|
|
30
|
+
queue: string;
|
|
31
|
+
key: string;
|
|
32
|
+
cron: string;
|
|
33
|
+
timeZone: string;
|
|
34
|
+
payload: unknown;
|
|
35
|
+
}
|
|
36
|
+
export interface JobEngine {
|
|
37
|
+
/**
|
|
38
|
+
* Creates what it needs and the queues. A sender only sends; a worker also runs maintenance
|
|
39
|
+
* and the schedules. A sender started again as a worker becomes one.
|
|
40
|
+
*/
|
|
41
|
+
start(queues: readonly QueueSpec[], options: {
|
|
42
|
+
worker: boolean;
|
|
43
|
+
}): Promise<void>;
|
|
44
|
+
/** The job's id, or null when `id` names a job already there. */
|
|
45
|
+
send(queue: string, payload: unknown, options: EngineSendOptions): Promise<string | null>;
|
|
46
|
+
/** Runs `handle` for the queue's jobs; a rejection is a failed attempt, retried by the queue's policy. */
|
|
47
|
+
work(queue: string, handle: (job: StoredJob) => Promise<void>): Promise<void>;
|
|
48
|
+
schedules: {
|
|
49
|
+
list(): Promise<EngineSchedule[]>;
|
|
50
|
+
set(schedule: EngineSchedule): Promise<void>;
|
|
51
|
+
remove(queue: string, key: string): Promise<void>;
|
|
52
|
+
};
|
|
53
|
+
/** Stops taking jobs and waits up to `timeoutSeconds` for running ones. */
|
|
54
|
+
stop(timeoutSeconds: number): Promise<void>;
|
|
55
|
+
/** Errors the engine met in the background, for the host to log. */
|
|
56
|
+
onError?(listener: (error: Error) => void): void;
|
|
57
|
+
}
|
|
58
|
+
//# sourceMappingURL=engine.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"engine.d.ts","sourceRoot":"","sources":["../src/engine.ts"],"names":[],"mappings":"AAAA;;;GAGG;AAEH,8DAA8D;AAC9D,MAAM,WAAW,SAAS;IACxB,IAAI,EAAE,MAAM,CAAC;IACb,UAAU,EAAE,MAAM,CAAC;IACnB,iBAAiB,EAAE,MAAM,CAAC;IAC1B,YAAY,EAAE,OAAO,CAAC;IACtB,cAAc,EAAE,MAAM,CAAC;IACvB,6EAA6E;IAC7E,gBAAgB,EAAE,MAAM,CAAC;CAC1B;AAED,gFAAgF;AAChF,MAAM,WAAW,SAAS;IACxB,EAAE,EAAE,MAAM,CAAC;IACX,8BAA8B;IAC9B,OAAO,EAAE,MAAM,CAAC;IAChB,OAAO,EAAE,OAAO,CAAC;CAClB;AAED,MAAM,WAAW,iBAAiB;IAChC,sFAAsF;IACtF,EAAE,CAAC,EAAE,MAAM,CAAC;IACZ,UAAU,CAAC,EAAE,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|
File without changes
|
|
@@ -0,0 +1,91 @@
|
|
|
1
|
+
/*! @fluixi/jobs v1.0.0-alpha.108 | (c) 2026 Ibrahima Touré and Fluixi contributors | MIT */
|
|
2
|
+
"use strict";
|
|
3
|
+
var __defProp = Object.defineProperty;
|
|
4
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
5
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
6
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
7
|
+
var __export = (target, all) => {
|
|
8
|
+
for (var name in all)
|
|
9
|
+
__defProp(target, name, { get: all[name], enumerable: true });
|
|
10
|
+
};
|
|
11
|
+
var __copyProps = (to, from, except, desc) => {
|
|
12
|
+
if (from && typeof from === "object" || typeof from === "function") {
|
|
13
|
+
for (let key of __getOwnPropNames(from))
|
|
14
|
+
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
15
|
+
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
16
|
+
}
|
|
17
|
+
return to;
|
|
18
|
+
};
|
|
19
|
+
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
20
|
+
|
|
21
|
+
// src/engines/memory.ts
|
|
22
|
+
var memory_exports = {};
|
|
23
|
+
__export(memory_exports, {
|
|
24
|
+
memoryEngine: () => memoryEngine
|
|
25
|
+
});
|
|
26
|
+
module.exports = __toCommonJS(memory_exports);
|
|
27
|
+
function memoryEngine() {
|
|
28
|
+
const queues = /* @__PURE__ */ new Map();
|
|
29
|
+
const handlers = /* @__PURE__ */ new Map();
|
|
30
|
+
const pending = /* @__PURE__ */ new Map();
|
|
31
|
+
const ids = /* @__PURE__ */ new Set();
|
|
32
|
+
const schedules = /* @__PURE__ */ new Map();
|
|
33
|
+
const running = /* @__PURE__ */ new Set();
|
|
34
|
+
const timers = /* @__PURE__ */ new Set();
|
|
35
|
+
let stopped = false;
|
|
36
|
+
let counter = 0;
|
|
37
|
+
const later = (ms, fn) => {
|
|
38
|
+
const timer = setTimeout(() => {
|
|
39
|
+
timers.delete(timer);
|
|
40
|
+
fn();
|
|
41
|
+
}, ms);
|
|
42
|
+
timers.add(timer);
|
|
43
|
+
};
|
|
44
|
+
const run = (queue, job) => {
|
|
45
|
+
const handle = handlers.get(queue);
|
|
46
|
+
if (!handle || stopped) return void (pending.get(queue) ?? pending.set(queue, []).get(queue)).push({ queue, job });
|
|
47
|
+
const spec = queues.get(queue);
|
|
48
|
+
const attempt = handle(job).catch(() => {
|
|
49
|
+
if (job.attempt >= spec.retryLimit) return;
|
|
50
|
+
const delay = spec.retryDelaySeconds * (spec.retryBackoff ? 2 ** job.attempt : 1);
|
|
51
|
+
later(delay * 1e3, () => run(queue, { ...job, attempt: job.attempt + 1 }));
|
|
52
|
+
});
|
|
53
|
+
running.add(attempt);
|
|
54
|
+
void attempt.finally(() => running.delete(attempt));
|
|
55
|
+
};
|
|
56
|
+
return {
|
|
57
|
+
async start(specs) {
|
|
58
|
+
for (const spec of specs) queues.set(spec.name, spec);
|
|
59
|
+
},
|
|
60
|
+
async send(queue, payload, options) {
|
|
61
|
+
if (!queues.has(queue)) throw new Error(`no queue "${queue}"`);
|
|
62
|
+
if (options.id && ids.has(options.id)) return null;
|
|
63
|
+
const id = options.id ?? `memory-${++counter}`;
|
|
64
|
+
ids.add(id);
|
|
65
|
+
const job = { id, attempt: 0, payload };
|
|
66
|
+
const wait = options.startAfter ? options.startAfter.getTime() - Date.now() : 0;
|
|
67
|
+
if (wait > 0) later(wait, () => run(queue, job));
|
|
68
|
+
else queueMicrotask(() => run(queue, job));
|
|
69
|
+
return id;
|
|
70
|
+
},
|
|
71
|
+
async work(queue, handle) {
|
|
72
|
+
handlers.set(queue, handle);
|
|
73
|
+
for (const { job } of pending.get(queue) ?? []) queueMicrotask(() => run(queue, job));
|
|
74
|
+
pending.delete(queue);
|
|
75
|
+
},
|
|
76
|
+
schedules: {
|
|
77
|
+
list: async () => [...schedules.values()],
|
|
78
|
+
set: async (schedule) => void schedules.set(`${schedule.queue}:${schedule.key}`, schedule),
|
|
79
|
+
remove: async (queue, key) => void schedules.delete(`${queue}:${key}`)
|
|
80
|
+
},
|
|
81
|
+
async stop(timeoutSeconds) {
|
|
82
|
+
stopped = true;
|
|
83
|
+
for (const timer of timers) clearTimeout(timer);
|
|
84
|
+
await Promise.race([Promise.allSettled([...running]), new Promise((resolve) => setTimeout(resolve, timeoutSeconds * 1e3))]);
|
|
85
|
+
}
|
|
86
|
+
};
|
|
87
|
+
}
|
|
88
|
+
// Annotate the CommonJS export names for ESM import in node:
|
|
89
|
+
0 && (module.exports = {
|
|
90
|
+
memoryEngine
|
|
91
|
+
});
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* An engine in memory, for tests and prototypes: jobs run in this process, with ids, retries and
|
|
3
|
+
* delays honoured. Nothing outlives the process, and schedules are kept but never fire.
|
|
4
|
+
*/
|
|
5
|
+
import type { JobEngine } from '../engine.js';
|
|
6
|
+
export declare function memoryEngine(): JobEngine;
|
|
7
|
+
//# sourceMappingURL=memory.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"memory.d.ts","sourceRoot":"","sources":["../../src/engines/memory.ts"],"names":[],"mappings":"AAAA;;;GAGG;AACH,OAAO,KAAK,EAAkB,SAAS,EAAwB,MAAM,cAAc,CAAC;AAEpF,wBAAgB,YAAY,IAAI,SAAS,CA+DxC"}
|
|
@@ -0,0 +1,70 @@
|
|
|
1
|
+
export function memoryEngine() {
|
|
2
|
+
const queues = new Map();
|
|
3
|
+
const handlers = new Map();
|
|
4
|
+
const pending = new Map();
|
|
5
|
+
const ids = new Set();
|
|
6
|
+
const schedules = new Map();
|
|
7
|
+
const running = new Set();
|
|
8
|
+
const timers = new Set();
|
|
9
|
+
let stopped = false;
|
|
10
|
+
let counter = 0;
|
|
11
|
+
const later = (ms, fn) => {
|
|
12
|
+
const timer = setTimeout(() => {
|
|
13
|
+
timers.delete(timer);
|
|
14
|
+
fn();
|
|
15
|
+
}, ms);
|
|
16
|
+
timers.add(timer);
|
|
17
|
+
};
|
|
18
|
+
const run = (queue, job) => {
|
|
19
|
+
const handle = handlers.get(queue);
|
|
20
|
+
if (!handle || stopped)
|
|
21
|
+
return void (pending.get(queue) ?? pending.set(queue, []).get(queue)).push({ queue, job });
|
|
22
|
+
const spec = queues.get(queue);
|
|
23
|
+
const attempt = handle(job).catch(() => {
|
|
24
|
+
if (job.attempt >= spec.retryLimit)
|
|
25
|
+
return;
|
|
26
|
+
const delay = spec.retryDelaySeconds * (spec.retryBackoff ? 2 ** job.attempt : 1);
|
|
27
|
+
later(delay * 1000, () => run(queue, { ...job, attempt: job.attempt + 1 }));
|
|
28
|
+
});
|
|
29
|
+
running.add(attempt);
|
|
30
|
+
void attempt.finally(() => running.delete(attempt));
|
|
31
|
+
};
|
|
32
|
+
return {
|
|
33
|
+
async start(specs) {
|
|
34
|
+
for (const spec of specs)
|
|
35
|
+
queues.set(spec.name, spec);
|
|
36
|
+
},
|
|
37
|
+
async send(queue, payload, options) {
|
|
38
|
+
if (!queues.has(queue))
|
|
39
|
+
throw new Error(`no queue "${queue}"`);
|
|
40
|
+
if (options.id && ids.has(options.id))
|
|
41
|
+
return null;
|
|
42
|
+
const id = options.id ?? `memory-${++counter}`;
|
|
43
|
+
ids.add(id);
|
|
44
|
+
const job = { id, attempt: 0, payload };
|
|
45
|
+
const wait = options.startAfter ? options.startAfter.getTime() - Date.now() : 0;
|
|
46
|
+
if (wait > 0)
|
|
47
|
+
later(wait, () => run(queue, job));
|
|
48
|
+
else
|
|
49
|
+
queueMicrotask(() => run(queue, job));
|
|
50
|
+
return id;
|
|
51
|
+
},
|
|
52
|
+
async work(queue, handle) {
|
|
53
|
+
handlers.set(queue, handle);
|
|
54
|
+
for (const { job } of pending.get(queue) ?? [])
|
|
55
|
+
queueMicrotask(() => run(queue, job));
|
|
56
|
+
pending.delete(queue);
|
|
57
|
+
},
|
|
58
|
+
schedules: {
|
|
59
|
+
list: async () => [...schedules.values()],
|
|
60
|
+
set: async (schedule) => void schedules.set(`${schedule.queue}:${schedule.key}`, schedule),
|
|
61
|
+
remove: async (queue, key) => void schedules.delete(`${queue}:${key}`),
|
|
62
|
+
},
|
|
63
|
+
async stop(timeoutSeconds) {
|
|
64
|
+
stopped = true;
|
|
65
|
+
for (const timer of timers)
|
|
66
|
+
clearTimeout(timer);
|
|
67
|
+
await Promise.race([Promise.allSettled([...running]), new Promise((resolve) => setTimeout(resolve, timeoutSeconds * 1000))]);
|
|
68
|
+
},
|
|
69
|
+
};
|
|
70
|
+
}
|
|
@@ -0,0 +1,67 @@
|
|
|
1
|
+
/*! @fluixi/jobs v1.0.0-alpha.108 | (c) 2026 Ibrahima Touré and Fluixi contributors | MIT */
|
|
2
|
+
|
|
3
|
+
// src/engines/memory.ts
|
|
4
|
+
function memoryEngine() {
|
|
5
|
+
const queues = /* @__PURE__ */ new Map();
|
|
6
|
+
const handlers = /* @__PURE__ */ new Map();
|
|
7
|
+
const pending = /* @__PURE__ */ new Map();
|
|
8
|
+
const ids = /* @__PURE__ */ new Set();
|
|
9
|
+
const schedules = /* @__PURE__ */ new Map();
|
|
10
|
+
const running = /* @__PURE__ */ new Set();
|
|
11
|
+
const timers = /* @__PURE__ */ new Set();
|
|
12
|
+
let stopped = false;
|
|
13
|
+
let counter = 0;
|
|
14
|
+
const later = (ms, fn) => {
|
|
15
|
+
const timer = setTimeout(() => {
|
|
16
|
+
timers.delete(timer);
|
|
17
|
+
fn();
|
|
18
|
+
}, ms);
|
|
19
|
+
timers.add(timer);
|
|
20
|
+
};
|
|
21
|
+
const run = (queue, job) => {
|
|
22
|
+
const handle = handlers.get(queue);
|
|
23
|
+
if (!handle || stopped) return void (pending.get(queue) ?? pending.set(queue, []).get(queue)).push({ queue, job });
|
|
24
|
+
const spec = queues.get(queue);
|
|
25
|
+
const attempt = handle(job).catch(() => {
|
|
26
|
+
if (job.attempt >= spec.retryLimit) return;
|
|
27
|
+
const delay = spec.retryDelaySeconds * (spec.retryBackoff ? 2 ** job.attempt : 1);
|
|
28
|
+
later(delay * 1e3, () => run(queue, { ...job, attempt: job.attempt + 1 }));
|
|
29
|
+
});
|
|
30
|
+
running.add(attempt);
|
|
31
|
+
void attempt.finally(() => running.delete(attempt));
|
|
32
|
+
};
|
|
33
|
+
return {
|
|
34
|
+
async start(specs) {
|
|
35
|
+
for (const spec of specs) queues.set(spec.name, spec);
|
|
36
|
+
},
|
|
37
|
+
async send(queue, payload, options) {
|
|
38
|
+
if (!queues.has(queue)) throw new Error(`no queue "${queue}"`);
|
|
39
|
+
if (options.id && ids.has(options.id)) return null;
|
|
40
|
+
const id = options.id ?? `memory-${++counter}`;
|
|
41
|
+
ids.add(id);
|
|
42
|
+
const job = { id, attempt: 0, payload };
|
|
43
|
+
const wait = options.startAfter ? options.startAfter.getTime() - Date.now() : 0;
|
|
44
|
+
if (wait > 0) later(wait, () => run(queue, job));
|
|
45
|
+
else queueMicrotask(() => run(queue, job));
|
|
46
|
+
return id;
|
|
47
|
+
},
|
|
48
|
+
async work(queue, handle) {
|
|
49
|
+
handlers.set(queue, handle);
|
|
50
|
+
for (const { job } of pending.get(queue) ?? []) queueMicrotask(() => run(queue, job));
|
|
51
|
+
pending.delete(queue);
|
|
52
|
+
},
|
|
53
|
+
schedules: {
|
|
54
|
+
list: async () => [...schedules.values()],
|
|
55
|
+
set: async (schedule) => void schedules.set(`${schedule.queue}:${schedule.key}`, schedule),
|
|
56
|
+
remove: async (queue, key) => void schedules.delete(`${queue}:${key}`)
|
|
57
|
+
},
|
|
58
|
+
async stop(timeoutSeconds) {
|
|
59
|
+
stopped = true;
|
|
60
|
+
for (const timer of timers) clearTimeout(timer);
|
|
61
|
+
await Promise.race([Promise.allSettled([...running]), new Promise((resolve) => setTimeout(resolve, timeoutSeconds * 1e3))]);
|
|
62
|
+
}
|
|
63
|
+
};
|
|
64
|
+
}
|
|
65
|
+
export {
|
|
66
|
+
memoryEngine
|
|
67
|
+
};
|