@coreframe/jobs 0.0.0 → 0.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,78 @@
1
+ //#region src/index.d.ts
2
+ type JobCooldown = number | `${number}${"ms" | "s" | "m" | "h" | "d"}`;
3
+ type JobInvocation<TContext> = {
4
+ context: TContext;
5
+ now: Date;
6
+ };
7
+ type JobDefinition<TContext> = {
8
+ id: string;
9
+ run(invocation: JobInvocation<TContext>): Promise<void> | void;
10
+ dev?: {
11
+ cooldown: JobCooldown;
12
+ leaseMs?: number;
13
+ onStartup?: boolean;
14
+ };
15
+ };
16
+ type JobRegistry<TContext> = readonly JobDefinition<TContext>[];
17
+ type JobStateClaim = {
18
+ status: "claimed";
19
+ leaseId: string;
20
+ } | {
21
+ status: "cooldown";
22
+ } | {
23
+ status: "running";
24
+ };
25
+ type JobStateStore = {
26
+ claim(input: {
27
+ cooldownMs: number;
28
+ jobId: string;
29
+ leaseMs: number;
30
+ now: Date;
31
+ }): Promise<JobStateClaim>;
32
+ complete(input: {
33
+ jobId: string;
34
+ leaseId: string;
35
+ now: Date;
36
+ }): Promise<void>;
37
+ fail(input: {
38
+ jobId: string;
39
+ leaseId: string;
40
+ now: Date;
41
+ }): Promise<void>;
42
+ };
43
+ type JobExecutionResult = {
44
+ durationMs: number;
45
+ error?: unknown;
46
+ id: string;
47
+ status: "completed" | "failed" | "skipped-cooldown" | "skipped-running";
48
+ };
49
+ declare const defaultJobLeaseMs: number;
50
+ declare function defineJob<TContext>(definition: JobDefinition<TContext>): JobDefinition<TContext>;
51
+ declare function defineJobRegistry<TContext>(jobs: JobRegistry<TContext>): JobRegistry<TContext>;
52
+ declare function parseJobCooldown(value: JobCooldown): number;
53
+ declare function runJobs<TContext>({
54
+ createContext,
55
+ jobs,
56
+ now,
57
+ onError
58
+ }: {
59
+ createContext(now: Date): Promise<TContext> | TContext;
60
+ jobs: JobRegistry<TContext>;
61
+ now?: Date;
62
+ onError?: "continue" | "throw";
63
+ }): Promise<JobExecutionResult[]>;
64
+ declare function runDevelopmentJobs<TContext>({
65
+ createContext,
66
+ jobs,
67
+ now,
68
+ onError,
69
+ state
70
+ }: {
71
+ createContext(now: Date): Promise<TContext> | TContext;
72
+ jobs: JobRegistry<TContext>;
73
+ now?: Date;
74
+ onError?: "continue" | "throw";
75
+ state: JobStateStore;
76
+ }): Promise<JobExecutionResult[]>;
77
+ //#endregion
78
+ export { JobCooldown, JobDefinition, JobExecutionResult, JobInvocation, JobRegistry, JobStateClaim, JobStateStore, defaultJobLeaseMs, defineJob, defineJobRegistry, parseJobCooldown, runDevelopmentJobs, runJobs };
package/dist/index.js ADDED
@@ -0,0 +1 @@
1
+ const e=600*1e3;function t(e){return s(e),e}function n(e){let t=new Set;for(let n of e){if(s(n),t.has(n.id))throw Error(`Duplicate Coreframe job ID ${JSON.stringify(n.id)}.`);t.add(n.id)}return e}function r(e){if(typeof e==`number`){if(!Number.isSafeInteger(e)||e<0)throw Error(`Coreframe job cooldowns must be non-negative integer milliseconds.`);return e}let t=/^(\d+(?:\.\d+)?)(ms|s|m|h|d)$/.exec(e);if(!t)throw Error(`Invalid Coreframe job cooldown ${JSON.stringify(e)}.`);let n=Number(t[1]),r=t[2],i=n*(r===`ms`?1:r===`s`?1e3:r===`m`?6e4:r===`h`?60*6e4:1440*6e4);if(!Number.isSafeInteger(i))throw Error(`Coreframe job cooldown is too large: ${JSON.stringify(e)}.`);return i}async function i({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 o({context:i,job:e,now:n,onError:r}));return a}async function a({createContext:e,jobs:t,now:n=new Date,onError:i=`continue`,state:a}){let s=t.filter(e=>e.dev?.onStartup);if(s.length===0)return[];let c=await e(n),l=[];for(let e of s){let t=e.dev;if(!t)continue;let s=await a.claim({cooldownMs:r(t.cooldown),jobId:e.id,leaseMs:t.leaseMs??6e5,now:n});if(s.status!==`claimed`){l.push({durationMs:0,id:e.id,status:s.status===`cooldown`?`skipped-cooldown`:`skipped-running`});continue}try{let t=await o({context:c,job:e,now:n,onError:i,state:a,leaseId:s.leaseId});l.push(t)}catch(t){if(i===`throw`)throw t;l.push({durationMs:0,error:t,id:e.id,status:`failed`})}}return l}async function o({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 s(e){if(!e.id.trim())throw Error(`Coreframe job IDs cannot be empty.`);if(e.dev){let t=r(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(t<0)throw Error(`Coreframe job ${JSON.stringify(e.id)} must have a non-negative cooldown.`)}}export{e as defaultJobLeaseMs,t as defineJob,n as defineJobRegistry,r as parseJobCooldown,a as runDevelopmentJobs,i as runJobs};
package/package.json CHANGED
@@ -1,13 +1,32 @@
1
1
  {
2
2
  "name": "@coreframe/jobs",
3
- "version": "0.0.0",
4
- "description": "",
5
- "main": "index.js",
6
- "scripts": {
7
- "test": "echo \"Error: no test specified\" && exit 1"
3
+ "version": "0.1.0",
4
+ "description": "Shared scheduled-job contracts and execution helpers for Coreframe apps",
5
+ "repository": {
6
+ "type": "git",
7
+ "url": "git+https://github.com/airlock-labs/coreframe.git",
8
+ "directory": "node/jobs"
9
+ },
10
+ "files": [
11
+ "dist/"
12
+ ],
13
+ "type": "module",
14
+ "exports": {
15
+ ".": {
16
+ "types": "./dist/index.d.ts",
17
+ "default": "./dist/index.js"
18
+ }
8
19
  },
9
- "keywords": [],
10
- "author": "",
11
- "license": "All rights reserved",
12
- "type": "commonjs"
13
- }
20
+ "devDependencies": {
21
+ "tsdown": "0.22.3",
22
+ "typescript": "6.0.3",
23
+ "vitest": "^4.1.9"
24
+ },
25
+ "scripts": {
26
+ "build": "tsdown",
27
+ "test": "vitest run --reporter=minimal",
28
+ "typecheck": "tsc --noEmit -p tsconfig.json",
29
+ "format": "oxlint --fix && oxfmt",
30
+ "lint": "oxlint"
31
+ }
32
+ }
package/readme.md DELETED
@@ -1 +0,0 @@
1
- Coming soon...