@axiom-lattice/local-stores 5.5.2 → 5.6.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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@axiom-lattice/local-stores",
3
- "version": "5.5.2",
3
+ "version": "5.6.0",
4
4
  "description": "Local SQLite-based stores for Axiom Lattice framework",
5
5
  "main": "dist/index.js",
6
6
  "module": "dist/index.mjs",
@@ -25,8 +25,8 @@
25
25
  "@types/sql.js": "^1.4.11",
26
26
  "sql.js": "^1.14.1",
27
27
  "uuid": "^14.0.1",
28
- "@axiom-lattice/core": "13.5.1",
29
- "@axiom-lattice/protocols": "7.9.0"
28
+ "@axiom-lattice/core": "13.6.0",
29
+ "@axiom-lattice/protocols": "7.10.0"
30
30
  },
31
31
  "devDependencies": {
32
32
  "@types/jest": "^29.5.14",
@@ -52,35 +52,50 @@ describe("LocalScheduleStorage.claimScheduledRun", () => {
52
52
 
53
53
  it("claims a pending task exactly once", async () => {
54
54
  const { store } = await createStore();
55
- await store.save(buildTask("task-1"));
55
+ const task = buildTask("task-1");
56
+ await store.save(task);
56
57
 
57
- expect(await store.claimScheduledRun("task-1")).toBe(true);
58
- expect(await store.claimScheduledRun("task-1")).toBe(false);
58
+ expect(await store.claimScheduledRun(task.taskId, task.executeAt!)).toBe(true);
59
+ expect(await store.claimScheduledRun(task.taskId, task.executeAt!)).toBe(false);
59
60
 
60
- const task = await store.get("task-1");
61
- expect(task?.status).toBe(ScheduledTaskStatus.RUNNING);
61
+ const stored = await store.get(task.taskId);
62
+ expect(stored?.status).toBe(ScheduledTaskStatus.RUNNING);
62
63
  });
63
64
 
64
65
  it("returns false for an unknown task", async () => {
65
66
  const { store } = await createStore();
66
- expect(await store.claimScheduledRun("missing")).toBe(false);
67
+ expect(await store.claimScheduledRun("missing", Date.now())).toBe(false);
67
68
  });
68
69
 
69
70
  it("returns false when the task is not pending", async () => {
70
71
  const { store } = await createStore();
71
- await store.save(buildTask("task-1"));
72
- await store.update("task-1", { status: ScheduledTaskStatus.COMPLETED });
72
+ const task = buildTask("task-1", { status: ScheduledTaskStatus.COMPLETED });
73
+ await store.save(task);
73
74
 
74
- expect(await store.claimScheduledRun("task-1")).toBe(false);
75
+ expect(await store.claimScheduledRun(task.taskId, task.executeAt!)).toBe(false);
75
76
  });
76
77
 
77
- it("can claim again after a retry re-arms the task to pending", async () => {
78
+ it("does not claim a stale occurrence after a retry re-arms the task", async () => {
78
79
  const { store } = await createStore();
79
- await store.save(buildTask("task-1"));
80
+ const task = buildTask("task-1");
81
+ await store.save(task);
82
+
83
+ expect(await store.claimScheduledRun(task.taskId, task.executeAt!)).toBe(true);
84
+ const retryAt = task.executeAt! - 60_000;
85
+ await store.update(task.taskId, {
86
+ status: ScheduledTaskStatus.PENDING,
87
+ executeAt: retryAt,
88
+ });
89
+
90
+ expect(await store.claimScheduledRun(task.taskId, task.executeAt!)).toBe(false);
91
+ expect(await store.claimScheduledRun(task.taskId, retryAt)).toBe(true);
92
+ });
80
93
 
81
- expect(await store.claimScheduledRun("task-1")).toBe(true);
82
- await store.update("task-1", { status: ScheduledTaskStatus.PENDING });
83
- expect(await store.claimScheduledRun("task-1")).toBe(true);
94
+ it("does not claim an occurrence that is not due yet", async () => {
95
+ const { store } = await createStore();
96
+ const task = buildTask("task-1", { executeAt: Date.now() + 60_000 });
97
+ await store.save(task);
98
+ expect(await store.claimScheduledRun(task.taskId, task.executeAt!)).toBe(false);
84
99
  });
85
100
  });
86
101
 
@@ -154,10 +154,27 @@ export class LocalScheduleStorage implements ScheduleStorage {
154
154
  ).run(...values);
155
155
  }
156
156
 
157
- async claimScheduledRun(taskId: string): Promise<boolean> {
157
+ async claimScheduledRun(taskId: string, expectedFireAt: number): Promise<boolean> {
158
+ const now = new Date().toISOString();
159
+ const expected = new Date(expectedFireAt).toISOString();
158
160
  const result = this.db.prepare(
159
- `UPDATE lt_scheduled_tasks SET status = 'running', updated_at = ? WHERE task_id = ? AND status = 'pending'`,
160
- ).run(new Date().toISOString(), taskId);
161
+ `UPDATE lt_scheduled_tasks
162
+ SET status = 'running', updated_at = ?
163
+ WHERE task_id = ? AND status = 'pending'
164
+ AND (
165
+ (execution_type = ? AND execute_at = ? AND execute_at <= ?)
166
+ OR (execution_type = ? AND next_run_at = ? AND next_run_at <= ?)
167
+ )`,
168
+ ).run(
169
+ now,
170
+ taskId,
171
+ ScheduleExecutionType.ONCE,
172
+ expected,
173
+ now,
174
+ ScheduleExecutionType.CRON,
175
+ expected,
176
+ now,
177
+ );
161
178
  return result.changes > 0;
162
179
  }
163
180