@antelopejs/interface-redis-scheduler 0.0.8 → 0.0.9
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/index.js +10 -2
- package/dist/index.js.map +1 -1
- package/dist/tests/index.test.js +80 -19
- package/dist/tests/index.test.js.map +1 -1
- package/docs/1.introduction.md +62 -17
- package/docs/assets/task-delivery.svg +18 -0
- package/package.json +12 -3
package/dist/index.js
CHANGED
|
@@ -217,6 +217,15 @@ async function updateTimer() {
|
|
|
217
217
|
}
|
|
218
218
|
}
|
|
219
219
|
}
|
|
220
|
+
const TakeDueTasksScript = `
|
|
221
|
+
local due = redis.call("ZRANGEBYSCORE", KEYS[1], 0, ARGV[1])
|
|
222
|
+
redis.call("ZREMRANGEBYSCORE", KEYS[1], 0, ARGV[1])
|
|
223
|
+
return due
|
|
224
|
+
`;
|
|
225
|
+
async function takeDueTasks(client) {
|
|
226
|
+
const tasks = await client.eval(TakeDueTasksScript, 1, RedisKey, Date.now());
|
|
227
|
+
return Array.isArray(tasks) ? tasks.map(String) : [];
|
|
228
|
+
}
|
|
220
229
|
/**
|
|
221
230
|
* Executes all due tasks
|
|
222
231
|
* This is called internally by the timer when tasks are due for execution
|
|
@@ -225,9 +234,8 @@ async function updateTimer() {
|
|
|
225
234
|
async function runTasks() {
|
|
226
235
|
locked = true;
|
|
227
236
|
const client = await (0, interface_redis_1.GetClient)();
|
|
228
|
-
const tasks = await client
|
|
237
|
+
const tasks = await takeDueTasks(client);
|
|
229
238
|
if (tasks.length > 0) {
|
|
230
|
-
await client.zrem(RedisKey, ...tasks);
|
|
231
239
|
for (const task of tasks) {
|
|
232
240
|
const m = task.match(RetryPattern);
|
|
233
241
|
try {
|
package/dist/index.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":";;AAyDA,gCAEC;AAgCD,wCAgBC;AAkBD,0CAEC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C,IAAI,QAAQ,CAAC,GAAG,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,EAAE,CAAC;oBAC5B,MAAM,QAAQ,CAAC,GAAG,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,EAAE,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC;gBACnC,CAAC;YACH,CAAC;YAAC,OAAO,GAAG,EAAE,CAAC;gBACb,iBAAO,CAAC,KAAK,CAAC,GAAG,CAAC,CAAC;gBACnB,IAAI,CAAC,EAAE,CAAC;oBACN,MAAM,UAAU,GAAG,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,QAAQ,CAAC,CAAC,CAAC,CAAC,CAAC,EAAE,EAAE,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC;oBACjD,IAAI,UAAU,GAAG,UAAU,EAAE,CAAC;wBAC5B,MAAM,MAAM,CAAC,IAAI,CACf,QAAQ,EACR,IAAI,CAAC,GAAG,EAAE,GAAG,YAAY,EACzB,SAAS,UAAU,GAAG,CAAC,IAAI,CAAC,CAAC,CAAC,CAAC,IAAI,CAAC,CAAC,CAAC,CAAC,EAAE,CAC1C,CAAC;oBACJ,CAAC;gBACH,CAAC;YACH,CAAC;QACH,CAAC;IACH,CAAC;IACD,MAAM,GAAG,KAAK,CAAC;IACf,MAAM,WAAW,EAAE,CAAC;AACtB,CAAC"}
|
package/dist/tests/index.test.js
CHANGED
|
@@ -2,7 +2,7 @@
|
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
3
|
const chai_1 = require("chai");
|
|
4
4
|
const interface_redis_1 = require("@antelopejs/interface-redis");
|
|
5
|
-
const
|
|
5
|
+
const index_1 = require("../index");
|
|
6
6
|
const RedisKey = "SchedulerUtil.Tasks";
|
|
7
7
|
async function cleanupRedis() {
|
|
8
8
|
const client = await (0, interface_redis_1.GetClient)();
|
|
@@ -27,20 +27,81 @@ describe("Redis Scheduler - enableListener / disableListener", () => {
|
|
|
27
27
|
describe("Redis Scheduler - runTasks", () => {
|
|
28
28
|
it("should execute due tasks", async () => RunDueTasksTest());
|
|
29
29
|
it("should not execute future tasks", async () => SkipFutureTasksTest());
|
|
30
|
+
it("should keep a task rescheduled while due tasks are collected", async () => {
|
|
31
|
+
await cleanupRedis();
|
|
32
|
+
const results = [];
|
|
33
|
+
(0, index_1.setHandler)("test-reschedule", (taskInfo) => {
|
|
34
|
+
results.push(taskInfo);
|
|
35
|
+
});
|
|
36
|
+
const client = await (0, interface_redis_1.GetClient)();
|
|
37
|
+
const later = Date.now() + 60000;
|
|
38
|
+
await client.zadd(RedisKey, Date.now() - 1000, "test-reschedule:renewal");
|
|
39
|
+
await Promise.all([
|
|
40
|
+
(0, index_1.runTasks)(),
|
|
41
|
+
(0, index_1.addTask)("test-reschedule", later, "renewal"),
|
|
42
|
+
]);
|
|
43
|
+
(0, chai_1.expect)(results).to.deep.equal(["renewal"]);
|
|
44
|
+
(0, chai_1.expect)(await client.zscore(RedisKey, "test-reschedule:renewal")).to.equal(String(later));
|
|
45
|
+
await cleanupRedis();
|
|
46
|
+
});
|
|
47
|
+
it("should keep due tasks when reading them fails", async () => {
|
|
48
|
+
await cleanupRedis();
|
|
49
|
+
const results = [];
|
|
50
|
+
(0, index_1.setHandler)("test-read-failure", (taskInfo) => {
|
|
51
|
+
results.push(taskInfo);
|
|
52
|
+
});
|
|
53
|
+
const client = await (0, interface_redis_1.GetClient)();
|
|
54
|
+
const due = Date.now() - 1000;
|
|
55
|
+
await client.zadd(RedisKey, due, "test-read-failure:renewal");
|
|
56
|
+
if (!isMockClient(client))
|
|
57
|
+
throw new Error("The test project must use the Redis mock");
|
|
58
|
+
const mock = client;
|
|
59
|
+
const store = mock.data;
|
|
60
|
+
let failed = false;
|
|
61
|
+
const failFirstRead = (key) => {
|
|
62
|
+
if (!failed && key === RedisKey) {
|
|
63
|
+
failed = true;
|
|
64
|
+
throw new Error("ERR injected read failure");
|
|
65
|
+
}
|
|
66
|
+
};
|
|
67
|
+
mock.data = {
|
|
68
|
+
...store,
|
|
69
|
+
get: (key) => (failFirstRead(key), store.get(key)),
|
|
70
|
+
has: (key) => (failFirstRead(key), store.has(key)),
|
|
71
|
+
};
|
|
72
|
+
let rejected = false;
|
|
73
|
+
try {
|
|
74
|
+
await (0, index_1.runTasks)();
|
|
75
|
+
}
|
|
76
|
+
catch {
|
|
77
|
+
rejected = true;
|
|
78
|
+
}
|
|
79
|
+
finally {
|
|
80
|
+
mock.data = store;
|
|
81
|
+
}
|
|
82
|
+
(0, chai_1.expect)(failed).to.equal(true);
|
|
83
|
+
(0, chai_1.expect)(rejected).to.equal(true);
|
|
84
|
+
(0, chai_1.expect)(results).to.deep.equal([]);
|
|
85
|
+
(0, chai_1.expect)(await client.zscore(RedisKey, "test-read-failure:renewal")).to.equal(String(due));
|
|
86
|
+
await cleanupRedis();
|
|
87
|
+
});
|
|
30
88
|
});
|
|
89
|
+
function isMockClient(client) {
|
|
90
|
+
return "data" in client;
|
|
91
|
+
}
|
|
31
92
|
describe("Redis Scheduler - retry logic", () => {
|
|
32
93
|
it("should retry failed tasks up to 3 times", async () => RetryFailedTasksTest());
|
|
33
94
|
});
|
|
34
95
|
async function RegisterHandlerTest() {
|
|
35
96
|
let called = false;
|
|
36
|
-
(0,
|
|
97
|
+
(0, index_1.setHandler)("test-register", () => {
|
|
37
98
|
called = true;
|
|
38
99
|
});
|
|
39
100
|
(0, chai_1.expect)(called).to.equal(false);
|
|
40
101
|
}
|
|
41
102
|
async function RejectColonInNameTest() {
|
|
42
103
|
try {
|
|
43
|
-
(0,
|
|
104
|
+
(0, index_1.setHandler)("invalid:name", () => { });
|
|
44
105
|
chai_1.expect.fail("should have thrown");
|
|
45
106
|
}
|
|
46
107
|
catch (err) {
|
|
@@ -49,9 +110,9 @@ async function RejectColonInNameTest() {
|
|
|
49
110
|
}
|
|
50
111
|
async function AddTaskTest() {
|
|
51
112
|
await cleanupRedis();
|
|
52
|
-
(0,
|
|
113
|
+
(0, index_1.setHandler)("test-add", () => { });
|
|
53
114
|
const dueTime = Date.now() + 60000;
|
|
54
|
-
await (0,
|
|
115
|
+
await (0, index_1.addTask)("test-add", dueTime, "task-data");
|
|
55
116
|
const client = await (0, interface_redis_1.GetClient)();
|
|
56
117
|
const members = await client.zrange(RedisKey, 0, -1);
|
|
57
118
|
(0, chai_1.expect)(members).to.include("test-add:task-data");
|
|
@@ -59,7 +120,7 @@ async function AddTaskTest() {
|
|
|
59
120
|
}
|
|
60
121
|
async function AddTaskUnknownHandlerTest() {
|
|
61
122
|
try {
|
|
62
|
-
await (0,
|
|
123
|
+
await (0, index_1.addTask)("nonexistent-handler", Date.now(), "data");
|
|
63
124
|
chai_1.expect.fail("should have thrown");
|
|
64
125
|
}
|
|
65
126
|
catch (err) {
|
|
@@ -68,17 +129,17 @@ async function AddTaskUnknownHandlerTest() {
|
|
|
68
129
|
}
|
|
69
130
|
async function RemoveTaskTest() {
|
|
70
131
|
await cleanupRedis();
|
|
71
|
-
(0,
|
|
132
|
+
(0, index_1.setHandler)("test-remove", () => { });
|
|
72
133
|
const client = await (0, interface_redis_1.GetClient)();
|
|
73
134
|
await client.zadd(RedisKey, Date.now() + 60000, "test-remove:remove-data");
|
|
74
|
-
await (0,
|
|
135
|
+
await (0, index_1.removeTask)("test-remove", "remove-data");
|
|
75
136
|
const members = await client.zrange(RedisKey, 0, -1);
|
|
76
137
|
(0, chai_1.expect)(members).to.not.include("test-remove:remove-data");
|
|
77
138
|
await cleanupRedis();
|
|
78
139
|
}
|
|
79
140
|
async function RemoveTaskUnknownHandlerTest() {
|
|
80
141
|
try {
|
|
81
|
-
await (0,
|
|
142
|
+
await (0, index_1.removeTask)("nonexistent-handler", "data");
|
|
82
143
|
chai_1.expect.fail("should have thrown");
|
|
83
144
|
}
|
|
84
145
|
catch (err) {
|
|
@@ -86,8 +147,8 @@ async function RemoveTaskUnknownHandlerTest() {
|
|
|
86
147
|
}
|
|
87
148
|
}
|
|
88
149
|
async function EnableDisableListenerTest() {
|
|
89
|
-
await (0,
|
|
90
|
-
await (0,
|
|
150
|
+
await (0, index_1.enableListener)();
|
|
151
|
+
await (0, index_1.disableListener)();
|
|
91
152
|
}
|
|
92
153
|
async function EnableListenerUsesLazyConnectTest() {
|
|
93
154
|
const client = await (0, interface_redis_1.GetClient)();
|
|
@@ -97,21 +158,21 @@ async function EnableListenerUsesLazyConnectTest() {
|
|
|
97
158
|
duplicateOptions = override;
|
|
98
159
|
return originalDuplicate(override);
|
|
99
160
|
});
|
|
100
|
-
await (0,
|
|
161
|
+
await (0, index_1.enableListener)();
|
|
101
162
|
(0, chai_1.expect)(duplicateOptions).to.deep.include({ lazyConnect: true });
|
|
102
|
-
await (0,
|
|
163
|
+
await (0, index_1.disableListener)();
|
|
103
164
|
client.duplicate = originalDuplicate;
|
|
104
165
|
}
|
|
105
166
|
async function RunDueTasksTest() {
|
|
106
167
|
await cleanupRedis();
|
|
107
168
|
const results = [];
|
|
108
|
-
(0,
|
|
169
|
+
(0, index_1.setHandler)("test-run", (taskInfo) => {
|
|
109
170
|
results.push(taskInfo);
|
|
110
171
|
});
|
|
111
172
|
const client = await (0, interface_redis_1.GetClient)();
|
|
112
173
|
await client.zadd(RedisKey, Date.now() - 1000, "test-run:payload1");
|
|
113
174
|
await client.zadd(RedisKey, Date.now() - 500, "test-run:payload2");
|
|
114
|
-
await (0,
|
|
175
|
+
await (0, index_1.runTasks)();
|
|
115
176
|
(0, chai_1.expect)(results).to.include("payload1");
|
|
116
177
|
(0, chai_1.expect)(results).to.include("payload2");
|
|
117
178
|
const remaining = await client.zrange(RedisKey, 0, -1);
|
|
@@ -121,12 +182,12 @@ async function RunDueTasksTest() {
|
|
|
121
182
|
async function SkipFutureTasksTest() {
|
|
122
183
|
await cleanupRedis();
|
|
123
184
|
const results = [];
|
|
124
|
-
(0,
|
|
185
|
+
(0, index_1.setHandler)("test-future", (taskInfo) => {
|
|
125
186
|
results.push(taskInfo);
|
|
126
187
|
});
|
|
127
188
|
const client = await (0, interface_redis_1.GetClient)();
|
|
128
189
|
await client.zadd(RedisKey, Date.now() + 60000, "test-future:future-data");
|
|
129
|
-
await (0,
|
|
190
|
+
await (0, index_1.runTasks)();
|
|
130
191
|
(0, chai_1.expect)(results).to.have.length(0);
|
|
131
192
|
const remaining = await client.zrange(RedisKey, 0, -1);
|
|
132
193
|
(0, chai_1.expect)(remaining).to.have.length(1);
|
|
@@ -135,13 +196,13 @@ async function SkipFutureTasksTest() {
|
|
|
135
196
|
async function RetryFailedTasksTest() {
|
|
136
197
|
await cleanupRedis();
|
|
137
198
|
let callCount = 0;
|
|
138
|
-
(0,
|
|
199
|
+
(0, index_1.setHandler)("test-retry", () => {
|
|
139
200
|
callCount++;
|
|
140
201
|
throw new Error("intentional failure");
|
|
141
202
|
});
|
|
142
203
|
const client = await (0, interface_redis_1.GetClient)();
|
|
143
204
|
await client.zadd(RedisKey, Date.now() - 1000, "test-retry:retry-data");
|
|
144
|
-
await (0,
|
|
205
|
+
await (0, index_1.runTasks)();
|
|
145
206
|
(0, chai_1.expect)(callCount).to.equal(1);
|
|
146
207
|
const retryMembers = await client.zrange(RedisKey, 0, -1);
|
|
147
208
|
(0, chai_1.expect)(retryMembers).to.have.length(1);
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.test.js","sourceRoot":"","sources":["../../src/tests/index.test.ts"],"names":[],"mappings":";;AAAA,+BAA8B;AAC9B,iEAAwD;AACxD,
|
|
1
|
+
{"version":3,"file":"index.test.js","sourceRoot":"","sources":["../../src/tests/index.test.ts"],"names":[],"mappings":";;AAAA,+BAA8B;AAC9B,iEAAwD;AACxD,oCAOkB;AAElB,MAAM,QAAQ,GAAG,qBAAqB,CAAC;AAEvC,KAAK,UAAU,YAAY;IACzB,MAAM,MAAM,GAAG,MAAM,IAAA,2BAAS,GAAE,CAAC;IACjC,MAAM,MAAM,CAAC,GAAG,CAAC,QAAQ,CAAC,CAAC;AAC7B,CAAC;AAED,QAAQ,CAAC,8BAA8B,EAAE,GA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AAC;IACvC,IAAA,aAAM,EAAC,YAAY,CAAC,CAAC,CAAC,CAAC,CAAC,EAAE,CAAC,KAAK,CAAC,iCAAiC,CAAC,CAAC;IAEpE,MAAM,YAAY,EAAE,CAAC;AACvB,CAAC"}
|
package/docs/1.introduction.md
CHANGED
|
@@ -6,6 +6,17 @@ Interface Redis Scheduler stores tasks in a Redis sorted set with their due time
|
|
|
6
6
|
|
|
7
7
|
Use one active listener for a queue and make handlers idempotent. Concurrent listeners can read the same due tasks before either removes them, and a process failure after removal but before handler completion can lose a task. Redis persistence protects queued entries only according to the server's persistence configuration.
|
|
8
8
|
|
|
9
|
+
## Schedule a task safely
|
|
10
|
+
|
|
11
|
+
1. Register the task type with [`setHandler`](#sethandler), including in processes that enqueue or remove tasks.
|
|
12
|
+
2. Start one active [`enableListener`](#enablelistener) for the queue after registering its handlers.
|
|
13
|
+
3. Save the serialized payload, then call [`addTask`](#addtask) with a due time in milliseconds.
|
|
14
|
+
4. Use that exact payload for [`removeTask`](#removetask); do not recreate timestamps when canceling.
|
|
15
|
+
|
|
16
|
+
{width="480"}
|
|
17
|
+
|
|
18
|
+
**Decide before adopting:** this scheduler fits work whose loss or repetition your application can tolerate or reconcile. It does not promise exactly-once execution, durable in-flight ownership, or a full shutdown/drain API. Review [retry logic](#retry-logic) and [shutdown limits](#disablelistener) before production use.
|
|
19
|
+
|
|
9
20
|
## Getting Started
|
|
10
21
|
|
|
11
22
|
Install the package in your project:
|
|
@@ -16,6 +27,36 @@ npm install @antelopejs/interface-redis-scheduler
|
|
|
16
27
|
|
|
17
28
|
> **Note:** This package depends on `@antelopejs/interface-redis`. You must have it installed and configured for the scheduler to connect to Redis.
|
|
18
29
|
|
|
30
|
+
## Try one scheduled task
|
|
31
|
+
|
|
32
|
+
Run this from an initialized application against a disposable Redis database,
|
|
33
|
+
with no other scheduler listener. It logs a reminder after about one minute;
|
|
34
|
+
it does not send an email or charge a payment.
|
|
35
|
+
|
|
36
|
+
```typescript
|
|
37
|
+
import {
|
|
38
|
+
setHandler,
|
|
39
|
+
enableListener,
|
|
40
|
+
addTask,
|
|
41
|
+
} from "@antelopejs/interface-redis-scheduler";
|
|
42
|
+
|
|
43
|
+
setHandler("docs-reminder", (taskInfo) => {
|
|
44
|
+
console.log("Reminder:", taskInfo);
|
|
45
|
+
});
|
|
46
|
+
|
|
47
|
+
await enableListener();
|
|
48
|
+
const taskInfo = JSON.stringify({
|
|
49
|
+
jobId: "demo-001",
|
|
50
|
+
message: "Review your draft",
|
|
51
|
+
});
|
|
52
|
+
await addTask("docs-reminder", Date.now() + 60_000, taskInfo);
|
|
53
|
+
```
|
|
54
|
+
|
|
55
|
+
Keep the process alive to observe the reminder. The due time is a Unix timestamp
|
|
56
|
+
in milliseconds, not a delay. Save `taskInfo` if you may later remove the queued
|
|
57
|
+
entry; [removal is not a cancellation guarantee](#removetask). The public API does
|
|
58
|
+
not fully stop timers, so end this disposable process when finished.
|
|
59
|
+
|
|
19
60
|
## Core Concepts
|
|
20
61
|
|
|
21
62
|
The scheduler operates around three main ideas:
|
|
@@ -26,7 +67,7 @@ The scheduler operates around three main ideas:
|
|
|
26
67
|
|
|
27
68
|
## `setHandler`
|
|
28
69
|
|
|
29
|
-
The `setHandler` function registers a handler for a specific task type. Each handler has a unique name and a callback function that receives the task data as a string.
|
|
70
|
+
The `setHandler` function registers a handler for a specific task type. Each handler has a unique name and a callback function that receives the task data as a string. The `sendEmail` function below belongs to your application; it is not a scheduler export.
|
|
30
71
|
|
|
31
72
|
```typescript
|
|
32
73
|
import { setHandler } from "@antelopejs/interface-redis-scheduler";
|
|
@@ -91,22 +132,26 @@ The `addTask` function schedules a task for execution at a specific time. The ta
|
|
|
91
132
|
```typescript
|
|
92
133
|
import { addTask } from "@antelopejs/interface-redis-scheduler";
|
|
93
134
|
|
|
94
|
-
async function scheduleReminder(
|
|
135
|
+
async function scheduleReminder(recipient: string) {
|
|
95
136
|
// Schedule for 3 days from now
|
|
96
137
|
const dueTime = Date.now() + 3 * 24 * 60 * 60 * 1000;
|
|
97
138
|
|
|
98
139
|
const taskInfo = JSON.stringify({
|
|
99
|
-
|
|
100
|
-
|
|
140
|
+
recipient,
|
|
141
|
+
subject: "Review your draft",
|
|
142
|
+
body: "Your draft is waiting for review.",
|
|
101
143
|
createdAt: new Date().toISOString(),
|
|
102
144
|
});
|
|
103
145
|
|
|
104
146
|
await addTask("send-email", dueTime, taskInfo);
|
|
147
|
+
return taskInfo; // Persist this exact string for removal.
|
|
105
148
|
}
|
|
106
149
|
```
|
|
107
150
|
|
|
108
151
|
The function throws an error if the specified handler name has not been registered.
|
|
109
152
|
|
|
153
|
+
The pair `handlerName` + `taskInfo` identifies the queued member. Adding the same pair again changes its due time rather than creating another independent task. Include a stable job ID in the payload when two otherwise identical jobs must remain distinct.
|
|
154
|
+
|
|
110
155
|
### Parameters
|
|
111
156
|
|
|
112
157
|
| Parameter | Type | Description |
|
|
@@ -117,19 +162,13 @@ The function throws an error if the specified handler name has not been register
|
|
|
117
162
|
|
|
118
163
|
## `removeTask`
|
|
119
164
|
|
|
120
|
-
The `removeTask` function
|
|
165
|
+
The `removeTask` function removes the original queued entry, if it is still present. Pass the saved `taskInfo` string returned by `scheduleReminder`, rather than reconstructing the JSON.
|
|
121
166
|
|
|
122
167
|
```typescript
|
|
123
168
|
import { removeTask } from "@antelopejs/interface-redis-scheduler";
|
|
124
169
|
|
|
125
|
-
async function
|
|
126
|
-
|
|
127
|
-
userId,
|
|
128
|
-
type: "password_reminder",
|
|
129
|
-
createdAt: "2025-04-05T12:00:00.000Z", // Must match exactly
|
|
130
|
-
});
|
|
131
|
-
|
|
132
|
-
await removeTask("send-email", taskInfo);
|
|
170
|
+
async function removeQueuedReminder(savedTaskInfo: string) {
|
|
171
|
+
await removeTask("send-email", savedTaskInfo);
|
|
133
172
|
}
|
|
134
173
|
```
|
|
135
174
|
|
|
@@ -137,6 +176,8 @@ The function throws an error if the specified handler name has not been register
|
|
|
137
176
|
|
|
138
177
|
> **Warning:** The `taskInfo` must be an exact string match of the original task. If the serialized JSON differs in key order or whitespace, the removal fails silently.
|
|
139
178
|
|
|
179
|
+
Removal targets the original queued member only. It does not stop a running handler or remove a retry stored with a `RETRY-` prefix. If cancellation must survive races and retries, make the handler check application-owned cancellation state before applying effects.
|
|
180
|
+
|
|
140
181
|
### Parameters
|
|
141
182
|
|
|
142
183
|
| Parameter | Type | Description |
|
|
@@ -150,7 +191,7 @@ The scheduler automatically retries failed tasks. When a handler throws an error
|
|
|
150
191
|
|
|
151
192
|
- **Maximum retries:** 3 retries after the initial attempt (4 attempts total)
|
|
152
193
|
- **Retry delay:** 5 seconds between each attempt
|
|
153
|
-
- **Behavior on exhaustion:** After 3 failed retries, the task is discarded
|
|
194
|
+
- **Behavior on exhaustion:** After 3 failed retries, the task is discarded; failures are reported through `Logging.Error`
|
|
154
195
|
|
|
155
196
|
No additional code is required to enable retries. The mechanism is built into the task execution loop.
|
|
156
197
|
|
|
@@ -158,13 +199,17 @@ No additional code is required to enable retries. The mechanism is built into th
|
|
|
158
199
|
import { setHandler } from "@antelopejs/interface-redis-scheduler";
|
|
159
200
|
|
|
160
201
|
// If this handler throws, the scheduler retries up to 3 times
|
|
161
|
-
setHandler("
|
|
202
|
+
setHandler("refresh-summary", async (taskInfo) => {
|
|
162
203
|
const data = JSON.parse(taskInfo);
|
|
163
|
-
await
|
|
204
|
+
await refreshSummary(data);
|
|
164
205
|
// On failure: retry after 5s, then 5s, then 5s, then discard
|
|
165
206
|
});
|
|
166
207
|
```
|
|
167
208
|
|
|
209
|
+
`refreshSummary` is an application function that recomputes a replaceable summary.
|
|
210
|
+
Choose retryable, idempotent work; a retry is not proof that the previous attempt
|
|
211
|
+
had no effect. Do not use this sketch as a durable payment-processing queue.
|
|
212
|
+
|
|
168
213
|
## Redis Storage
|
|
169
214
|
|
|
170
215
|
The scheduler uses a single Redis sorted set with the key `SchedulerUtil.Tasks`. Each member in the sorted set follows the format `handlerName:taskInfo`, and its score is the due timestamp in milliseconds.
|
|
@@ -197,7 +242,7 @@ await runTasks();
|
|
|
197
242
|
|
|
198
243
|
## Complete Example
|
|
199
244
|
|
|
200
|
-
|
|
245
|
+
This integration sketch shows handler registration, listener startup, scheduling, and subscriber cleanup. `sendEmail`, `deleteSession`, and `refreshSummary` in these examples stand for application functions you supply; they are not scheduler exports. The examples require a configured Redis implementation and do not run a service from this page.
|
|
201
246
|
|
|
202
247
|
```typescript
|
|
203
248
|
import {
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
<svg xmlns="http://www.w3.org/2000/svg" width="480" height="760" viewBox="0 0 480 760" role="img" aria-labelledby="title desc">
|
|
2
|
+
<title id="title">Task delivery and the in-flight gap</title>
|
|
3
|
+
<desc id="desc">A timer reads due tasks from a sorted set and removes them before calling handlers. Successful handlers finish. Thrown errors requeue after five seconds, up to three retries. A crash after removal can lose work; pub/sub only refreshes timers.</desc>
|
|
4
|
+
<defs><marker id="arrow" markerWidth="8" markerHeight="8" refX="7" refY="4" orient="auto"><path d="M0 0 8 4 0 8" fill="#627d98"/></marker></defs>
|
|
5
|
+
<rect width="480" height="760" rx="16" fill="#f4f7fb"/>
|
|
6
|
+
<g font-family="system-ui, sans-serif" fill="#17324d" font-size="18">
|
|
7
|
+
<text x="24" y="36" font-size="24" font-weight="700">Remove first. Run the handler.</text><text x="24" y="70">One listener · idempotent handlers</text><text x="24" y="99">Application-owned recovery</text>
|
|
8
|
+
<rect x="24" y="126" width="400" height="86" rx="10" fill="#e2edf8" stroke="#b0c9e0"/><text x="44" y="160" font-size="22" font-weight="700">Queued in Redis</text><text x="44" y="190">Score = due time (milliseconds)</text>
|
|
9
|
+
<path d="M224 212V246" stroke="#627d98" stroke-width="2" marker-end="url(#arrow)"/>
|
|
10
|
+
<rect x="24" y="258" width="400" height="86" rx="10" fill="#fff0cf"/><text x="44" y="292" font-size="22" font-weight="700">Read + remove due tasks</text><text x="44" y="322">Not an atomic claim</text>
|
|
11
|
+
<path d="M224 344V378" stroke="#627d98" stroke-width="2" marker-end="url(#arrow)"/>
|
|
12
|
+
<rect x="24" y="390" width="400" height="86" rx="10" fill="#17324d"/><text x="44" y="424" fill="#fff" font-size="22" font-weight="700">Run handler</text><text x="44" y="454" fill="#d5e5f3">Success → done</text>
|
|
13
|
+
<path d="M424 433H448V169H424" fill="none" stroke="#627d98" stroke-width="2" stroke-dasharray="5 5" marker-end="url(#arrow)"/>
|
|
14
|
+
<text x="24" y="514" font-weight="600">Dashed path: handler throws</text><text x="24" y="544">Retry after 5s · at most 3 retries</text>
|
|
15
|
+
<rect x="24" y="573" width="432" height="164" rx="8" fill="#fbe6e2"/>
|
|
16
|
+
<text x="44" y="606" font-size="20" font-weight="600">Crash after removal?</text><text x="44" y="635">The in-flight task can be lost.</text><text x="44" y="669" font-size="16">Pub/sub refreshes timers, not ownership.</text><text x="44" y="699" font-size="16">disableListener() closes notifications only.</text>
|
|
17
|
+
</g>
|
|
18
|
+
</svg>
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@antelopejs/interface-redis-scheduler",
|
|
3
|
-
"version": "0.0.
|
|
3
|
+
"version": "0.0.9",
|
|
4
4
|
"description": "AntelopeJS interface for scheduling tasks on Redis, with retries on failure",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"antelopejs",
|
|
@@ -45,7 +45,15 @@
|
|
|
45
45
|
"release": "pnpm run lint && pnpm run prepack && release-it",
|
|
46
46
|
"test": "pnpm run build && ajs module test .",
|
|
47
47
|
"format:check": "oxfmt --check .",
|
|
48
|
-
"knip": "knip"
|
|
48
|
+
"knip": "knip",
|
|
49
|
+
"prepare": "if git rev-parse --is-inside-work-tree >/dev/null 2>&1 && command -v lefthook >/dev/null 2>&1; then lefthook install; fi",
|
|
50
|
+
"checks": "antelopejs-checks",
|
|
51
|
+
"size": "antelopejs-check-size",
|
|
52
|
+
"ratchet": "antelopejs-ratchet",
|
|
53
|
+
"sync": "antelopejs-sync",
|
|
54
|
+
"ratchet:update": "antelopejs-ratchet --update",
|
|
55
|
+
"sync:check": "antelopejs-sync --check",
|
|
56
|
+
"typecheck": "tsc --noEmit"
|
|
49
57
|
},
|
|
50
58
|
"dependencies": {
|
|
51
59
|
"ioredis": "^5.10.1"
|
|
@@ -54,13 +62,14 @@
|
|
|
54
62
|
"@antelopejs/core": "^1.16.0",
|
|
55
63
|
"@antelopejs/interface-core": "^0.2.4",
|
|
56
64
|
"@antelopejs/interface-redis": "^0.0.9",
|
|
57
|
-
"@antelopejs/tooling-configs": "^0.0
|
|
65
|
+
"@antelopejs/tooling-configs": "^0.2.0",
|
|
58
66
|
"@types/chai": "^4.3.20",
|
|
59
67
|
"@types/mocha": "^10.0.10",
|
|
60
68
|
"@types/node": "^22.19.15",
|
|
61
69
|
"chai": "^4.5.0",
|
|
62
70
|
"eslint-plugin-perfectionist": "^5.11.0",
|
|
63
71
|
"knip": "^6.34.0",
|
|
72
|
+
"lefthook": "^2.2.2",
|
|
64
73
|
"oxfmt": "^0.66.0",
|
|
65
74
|
"oxlint": "1.81.0",
|
|
66
75
|
"oxlint-tsgolint": "^7.0.2001",
|