@ecosy/schedule 0.1.0 → 0.1.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.
Files changed (63) hide show
  1. package/dist/cron.d.ts +44 -0
  2. package/dist/cron.d.ts.map +1 -0
  3. package/dist/cron.js +2 -0
  4. package/dist/cron.js.map +1 -0
  5. package/dist/cron.mjs +2 -0
  6. package/dist/cron.mjs.map +1 -0
  7. package/dist/hook/index.d.ts +32 -0
  8. package/dist/hook/index.d.ts.map +1 -0
  9. package/dist/hook/index.js +2 -0
  10. package/dist/hook/index.js.map +1 -0
  11. package/dist/hook/index.mjs +2 -0
  12. package/dist/hook/index.mjs.map +1 -0
  13. package/{src/index.ts → dist/index.d.ts} +1 -0
  14. package/dist/index.d.ts.map +1 -0
  15. package/dist/index.js +2 -0
  16. package/dist/index.js.map +1 -0
  17. package/dist/index.mjs +2 -0
  18. package/dist/index.mjs.map +1 -0
  19. package/dist/registry.d.ts +39 -0
  20. package/dist/registry.d.ts.map +1 -0
  21. package/dist/registry.js +2 -0
  22. package/dist/registry.js.map +1 -0
  23. package/dist/registry.mjs +2 -0
  24. package/dist/registry.mjs.map +1 -0
  25. package/dist/runner.d.ts +12 -0
  26. package/dist/runner.d.ts.map +1 -0
  27. package/dist/runner.js +2 -0
  28. package/dist/runner.js.map +1 -0
  29. package/dist/runner.mjs +2 -0
  30. package/dist/runner.mjs.map +1 -0
  31. package/dist/schedule.d.ts +121 -0
  32. package/dist/schedule.d.ts.map +1 -0
  33. package/dist/schedule.js +2 -0
  34. package/dist/schedule.js.map +1 -0
  35. package/dist/schedule.mjs +2 -0
  36. package/dist/schedule.mjs.map +1 -0
  37. package/dist/source/index.d.ts +37 -0
  38. package/dist/source/index.d.ts.map +1 -0
  39. package/dist/source/index.js +3 -0
  40. package/dist/source/index.js.map +1 -0
  41. package/dist/source/index.mjs +3 -0
  42. package/dist/source/index.mjs.map +1 -0
  43. package/dist/task.d.ts +60 -0
  44. package/dist/task.d.ts.map +1 -0
  45. package/dist/task.js +2 -0
  46. package/dist/task.js.map +1 -0
  47. package/dist/task.mjs +2 -0
  48. package/dist/task.mjs.map +1 -0
  49. package/{src/types.ts → dist/types.d.ts} +55 -67
  50. package/dist/types.d.ts.map +1 -0
  51. package/dist/types.js +2 -0
  52. package/dist/types.js.map +1 -0
  53. package/dist/types.mjs +2 -0
  54. package/dist/types.mjs.map +1 -0
  55. package/package.json +25 -9
  56. package/src/cron.ts +0 -368
  57. package/src/hook/index.ts +0 -101
  58. package/src/registry.ts +0 -100
  59. package/src/runner.ts +0 -150
  60. package/src/schedule.ts +0 -387
  61. package/src/source/index.ts +0 -126
  62. package/src/task.ts +0 -124
  63. package/tsconfig.json +0 -23
package/src/task.ts DELETED
@@ -1,124 +0,0 @@
1
- import { nextFire, parseCron, type CronExpression } from "./cron";
2
- import type { TaskDefinition } from "./types";
3
-
4
- export interface TaskStatus {
5
- key: string;
6
- enabled: boolean;
7
- expression: string;
8
- timezone: string;
9
- nextAt: Date | null;
10
- running: boolean;
11
- lastRunAt: Date | null;
12
- lastOk: boolean | null;
13
- runs: number;
14
- failures: number;
15
- }
16
-
17
- /**
18
- * One scheduled task: its definition, when it fires next, and how it has been
19
- * doing.
20
- *
21
- * The split between the two matters at sync time. A definition that changed
22
- * gets a freshly computed schedule — the new expression decides the next fire,
23
- * not an adjustment of the old one — while the history carries over, because
24
- * "when did this last succeed" is the question people ask after an edit, not
25
- * before it.
26
- */
27
- export class Task {
28
- readonly key: string;
29
-
30
- private definition: TaskDefinition;
31
- private cron: CronExpression;
32
-
33
- nextAt: Date | null = null;
34
- running = false;
35
-
36
- lastRunAt: Date | null = null;
37
- lastOk: boolean | null = null;
38
- runs = 0;
39
- failures = 0;
40
-
41
- constructor(definition: TaskDefinition, from: Date = new Date()) {
42
- this.key = definition.key;
43
- this.definition = definition;
44
- this.cron = parseCron(definition.expression);
45
- this.schedule(from);
46
- }
47
-
48
- get enabled(): boolean {
49
- return this.definition.enabled !== false;
50
- }
51
-
52
- get timezone(): string {
53
- return this.definition.timezone ?? "UTC";
54
- }
55
-
56
- get current(): TaskDefinition {
57
- return this.definition;
58
- }
59
-
60
- /** Recomputes the next fire from scratch. History is untouched. */
61
- private schedule(from: Date): void {
62
- this.nextAt = this.enabled ? nextFire(this.cron, from, this.timezone) : null;
63
- }
64
-
65
- /**
66
- * Applies a changed definition.
67
- *
68
- * Only reparses when the expression or zone actually moved: reparsing on
69
- * every sync would reset `nextAt` each time and, for anything firing less
70
- * often than the sync interval, push the fire permanently into the future.
71
- */
72
- update(definition: TaskDefinition, from: Date = new Date()): void {
73
- const rescheduled =
74
- definition.expression !== this.definition.expression ||
75
- definition.timezone !== this.definition.timezone ||
76
- (definition.enabled !== false) !== this.enabled;
77
-
78
- this.definition = definition;
79
-
80
- if (rescheduled) {
81
- this.cron = parseCron(definition.expression);
82
- this.schedule(from);
83
- }
84
- }
85
-
86
- due(now: Date): boolean {
87
- return this.enabled && !this.running && this.nextAt !== null && this.nextAt <= now;
88
- }
89
-
90
- /**
91
- * Moves to the fire after this one.
92
- *
93
- * Called the moment a run is picked up, before it finishes, so a task that
94
- * takes longer than its interval does not push its own schedule along behind
95
- * it. Overlap is prevented by `running`, not by delaying the clock.
96
- */
97
- advance(): Date {
98
- const fired = this.nextAt ?? new Date();
99
- this.nextAt = nextFire(this.cron, fired, this.timezone);
100
- return fired;
101
- }
102
-
103
- record(ok: boolean, at: Date): void {
104
- this.runs++;
105
- if (!ok) this.failures++;
106
- this.lastRunAt = at;
107
- this.lastOk = ok;
108
- }
109
-
110
- status(): TaskStatus {
111
- return {
112
- key: this.key,
113
- enabled: this.enabled,
114
- expression: this.definition.expression,
115
- timezone: this.timezone,
116
- nextAt: this.nextAt,
117
- running: this.running,
118
- lastRunAt: this.lastRunAt,
119
- lastOk: this.lastOk,
120
- runs: this.runs,
121
- failures: this.failures,
122
- };
123
- }
124
- }
package/tsconfig.json DELETED
@@ -1,23 +0,0 @@
1
- {
2
- "compilerOptions": {
3
- "target": "ESNext",
4
- "lib": ["dom", "dom.iterable", "esnext"],
5
- "allowJs": true,
6
- "skipLibCheck": true,
7
- "strict": true,
8
- "forceConsistentCasingInFileNames": true,
9
- "esModuleInterop": true,
10
- "module": "ESNext",
11
- "moduleResolution": "bundler",
12
- "resolveJsonModule": true,
13
- "isolatedModules": true,
14
- "jsx": "react-jsx",
15
- "declaration": true,
16
- "declarationMap": true,
17
- "sourceMap": true,
18
- "rootDir": "./src",
19
- "outDir": "./dist"
20
- },
21
- "include": ["src/**/*"],
22
- "exclude": ["node_modules", "dist"]
23
- }