@coreframe/jobs 0.1.2-next.1 → 3.0.0-beta.1
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/development-state.d.ts +1 -2
- package/dist/development-state.js +1 -1
- package/dist/index.d.ts +5 -35
- package/dist/index.js +1 -1
- package/package.json +1 -1
|
@@ -5,8 +5,7 @@ import { JobCooldown } from "./definition.js";
|
|
|
5
5
|
type DevelopmentJobDefinition = {
|
|
6
6
|
id: string;
|
|
7
7
|
dev: {
|
|
8
|
-
|
|
9
|
-
oncePerSession?: boolean;
|
|
8
|
+
cooldown: JobCooldown;
|
|
10
9
|
leaseMs?: number;
|
|
11
10
|
onStartup: true;
|
|
12
11
|
};
|
|
@@ -1 +1 @@
|
|
|
1
|
-
import{parseJobCooldown as e}from"./definition.js";function t(t){if(!t.id.trim())throw Error(`Coreframe job IDs cannot be empty.`);e(t.dev.cooldown
|
|
1
|
+
import{parseJobCooldown as e}from"./definition.js";function t(t){if(!t.id.trim())throw Error(`Coreframe job IDs cannot be empty.`);e(t.dev.cooldown);let n=t.dev.leaseMs??6e5;if(!Number.isSafeInteger(n)||n<=0)throw Error(`Coreframe job ${JSON.stringify(t.id)} must have a positive lease.`);return t}function n(e){let n=new Set;for(let r of e.jobs){if(t(r),n.has(r.id))throw Error(`Duplicate Coreframe development job ID ${JSON.stringify(r.id)}.`);n.add(r.id)}return e}function r({jobs:t,now:n,records:r}){let i=new Map(t.map(e=>[e.id,e])),a=new Map(r.map(e=>[e.jobId,e]));return[...new Set([...i.keys(),...a.keys()])].sort().map(t=>{let r=i.get(t),o=a.get(t),s=r?e(r.dev.cooldown):null,c=o?.lastSucceededAt??null,l=o?.leaseUntil??null,u=l?l>n?`active`:`expired`:`none`,d;return d=r?!o||!c?`never-run`:c.getTime()+s>n.getTime()?`cooldown`:`due`:`unknown`,{cooldownMs:s,eligibility:d,id:t,lastSucceededAt:c,lease:u,leaseMs:r?.dev.leaseMs??(r?6e5:null),leaseUntil:l,nextEligibleAt:s===null||!c?null:new Date(c.getTime()+s),persisted:!!o,registration:r?`configured`:`orphaned`}})}export{t as defineDevelopmentJob,n as defineDevelopmentJobs,r as inspectDevelopmentJobStates};
|
package/dist/index.d.ts
CHANGED
|
@@ -4,15 +4,13 @@ import { DevelopmentJobDefinition, DevelopmentJobStateAdmin, DevelopmentJobState
|
|
|
4
4
|
//#region src/index.d.ts
|
|
5
5
|
type JobInvocation<TContext> = {
|
|
6
6
|
context: TContext;
|
|
7
|
-
now: Date;
|
|
8
|
-
sessionId?: string;
|
|
7
|
+
now: Date;
|
|
9
8
|
};
|
|
10
9
|
type JobDefinition<TContext> = {
|
|
11
10
|
id: string;
|
|
12
11
|
run(invocation: JobInvocation<TContext>): Promise<void> | void;
|
|
13
12
|
dev?: {
|
|
14
|
-
|
|
15
|
-
oncePerSession?: boolean;
|
|
13
|
+
cooldown: JobCooldown;
|
|
16
14
|
leaseMs?: number;
|
|
17
15
|
onStartup?: boolean;
|
|
18
16
|
};
|
|
@@ -27,15 +25,6 @@ type JobStateClaim = {
|
|
|
27
25
|
status: "running";
|
|
28
26
|
};
|
|
29
27
|
type JobStateStore = {
|
|
30
|
-
/**
|
|
31
|
-
* Atomically persist a receipt for (sessionId, jobId) before execution. Return false if already
|
|
32
|
-
* claimed. Receipts must survive failures, lease expiry, Worker restarts, and scheduling-state
|
|
33
|
-
* resets. Required for oncePerSession jobs.
|
|
34
|
-
*/
|
|
35
|
-
claimSession?(input: {
|
|
36
|
-
jobId: string;
|
|
37
|
-
sessionId: string;
|
|
38
|
-
}): Promise<boolean>;
|
|
39
28
|
claim(input: {
|
|
40
29
|
cooldownMs: number;
|
|
41
30
|
jobId: string;
|
|
@@ -57,7 +46,7 @@ type JobExecutionResult = {
|
|
|
57
46
|
durationMs: number;
|
|
58
47
|
error?: unknown;
|
|
59
48
|
id: string;
|
|
60
|
-
status: "completed" | "failed" | "skipped-cooldown" | "skipped-running"
|
|
49
|
+
status: "completed" | "failed" | "skipped-cooldown" | "skipped-running";
|
|
61
50
|
};
|
|
62
51
|
declare function defineJob<TContext>(definition: JobDefinition<TContext>): JobDefinition<TContext>;
|
|
63
52
|
declare function defineJobRegistry<TContext>(jobs: JobRegistry<TContext>): JobRegistry<TContext>;
|
|
@@ -72,37 +61,18 @@ declare function runJobs<TContext>({
|
|
|
72
61
|
now?: Date;
|
|
73
62
|
onError?: "continue" | "throw";
|
|
74
63
|
}): Promise<JobExecutionResult[]>;
|
|
75
|
-
/**
|
|
76
|
-
* Checks startup jobs against persisted cooldowns and leases. Session jobs also consume a durable
|
|
77
|
-
* receipt before calling the handler. A crash after that claim can leave the handler unexecuted;
|
|
78
|
-
* this is at-most-once, not a guarantee of successful execution. Missing session identity or
|
|
79
|
-
* claimSession support is rejected before creating context or claiming any jobs.
|
|
80
|
-
*
|
|
81
|
-
* @example
|
|
82
|
-
* ```ts
|
|
83
|
-
* defineJob({
|
|
84
|
-
* id: "development.prepare",
|
|
85
|
-
* dev: { onStartup: true, oncePerSession: true },
|
|
86
|
-
* run({ sessionId }) {
|
|
87
|
-
* console.log(`Preparing session ${sessionId}`)
|
|
88
|
-
* },
|
|
89
|
-
* })
|
|
90
|
-
* ```
|
|
91
|
-
*/
|
|
92
64
|
declare function runDevelopmentJobs<TContext>({
|
|
93
65
|
createContext,
|
|
94
66
|
jobs,
|
|
95
67
|
now,
|
|
96
68
|
onError,
|
|
97
|
-
state
|
|
98
|
-
sessionId
|
|
69
|
+
state
|
|
99
70
|
}: {
|
|
100
71
|
createContext(now: Date): Promise<TContext> | TContext;
|
|
101
72
|
jobs: JobRegistry<TContext>;
|
|
102
73
|
now?: Date;
|
|
103
74
|
onError?: "continue" | "throw";
|
|
104
|
-
state: JobStateStore;
|
|
105
|
-
sessionId?: string;
|
|
75
|
+
state: JobStateStore;
|
|
106
76
|
}): Promise<JobExecutionResult[]>;
|
|
107
77
|
//#endregion
|
|
108
78
|
export { DevelopmentJobDefinition, DevelopmentJobStateAdmin, DevelopmentJobStateRecord, DevelopmentJobStateResetResult, DevelopmentJobStatus, DevelopmentJobs, JobCooldown, JobDefinition, JobExecutionResult, JobInvocation, JobRegistry, JobStateClaim, JobStateStore, defaultJobLeaseMs, defineDevelopmentJob, defineDevelopmentJobs, defineJob, defineJobRegistry, inspectDevelopmentJobStates, parseJobCooldown, runDevelopmentJobs, runJobs };
|
package/dist/index.js
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
import{defaultJobLeaseMs as e,parseJobCooldown as t}from"./definition.js";import{defineDevelopmentJob as n,defineDevelopmentJobs as r,inspectDevelopmentJobStates as i}from"./development-state.js";function a(e){return u(e),e}function o(e){let t=new Set;for(let n of e){if(u(n),t.has(n.id))throw Error(`Duplicate Coreframe job ID ${JSON.stringify(n.id)}.`);t.add(n.id)}return e}async function s({createContext:e,jobs:t,now:n=new Date,onError:r=`continue`}){let i=await e(n),a=[];for(let e of t)a.push(await l({context:i,job:e,now:n,onError:r}));return a}async function c({createContext:e,jobs:n,now:r=new Date,onError:i=`continue`,state:a
|
|
1
|
+
import{defaultJobLeaseMs as e,parseJobCooldown as t}from"./definition.js";import{defineDevelopmentJob as n,defineDevelopmentJobs as r,inspectDevelopmentJobStates as i}from"./development-state.js";function a(e){return u(e),e}function o(e){let t=new Set;for(let n of e){if(u(n),t.has(n.id))throw Error(`Duplicate Coreframe job ID ${JSON.stringify(n.id)}.`);t.add(n.id)}return e}async function s({createContext:e,jobs:t,now:n=new Date,onError:r=`continue`}){let i=await e(n),a=[];for(let e of t)a.push(await l({context:i,job:e,now:n,onError:r}));return a}async function c({createContext:e,jobs:n,now:r=new Date,onError:i=`continue`,state:a}){let o=n.filter(e=>e.dev?.onStartup);if(o.length===0)return[];let s=await e(r),c=[];for(let e of o){let n=e.dev;if(!n)continue;let o=await a.claim({cooldownMs:t(n.cooldown),jobId:e.id,leaseMs:n.leaseMs??6e5,now:r});if(o.status!==`claimed`){c.push({durationMs:0,id:e.id,status:o.status===`cooldown`?`skipped-cooldown`:`skipped-running`});continue}try{let t=await l({context:s,job:e,now:r,onError:i,state:a,leaseId:o.leaseId});c.push(t)}catch(t){if(i===`throw`)throw t;c.push({durationMs:0,error:t,id:e.id,status:`failed`})}}return c}async function l({context:e,job:t,leaseId:n,now:r,onError:i,state:a}){let o=Date.now();try{return await t.run({context:e,now:r}),a&&n&&await a.complete({jobId:t.id,leaseId:n,now:r}),{durationMs:Date.now()-o,id:t.id,status:`completed`}}catch(e){if(a&&n&&await a.fail({jobId:t.id,leaseId:n,now:r}),i===`throw`)throw e;return{durationMs:Date.now()-o,error:e,id:t.id,status:`failed`}}}function u(e){if(!e.id.trim())throw Error(`Coreframe job IDs cannot be empty.`);if(e.dev){let n=t(e.dev.cooldown);if(!Number.isSafeInteger(e.dev.leaseMs??6e5)||(e.dev.leaseMs??6e5)<=0)throw Error(`Coreframe job ${JSON.stringify(e.id)} must have a positive lease.`);if(n<0)throw Error(`Coreframe job ${JSON.stringify(e.id)} must have a non-negative cooldown.`)}}export{e as defaultJobLeaseMs,n as defineDevelopmentJob,r as defineDevelopmentJobs,a as defineJob,o as defineJobRegistry,i as inspectDevelopmentJobStates,t as parseJobCooldown,c as runDevelopmentJobs,s as runJobs};
|