@antelopejs/interface-redis-scheduler 0.0.7 → 0.0.8
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/README.md +87 -13
- package/dist/antelope.test.js +1 -1
- package/dist/index.d.ts +4 -3
- package/dist/index.js +6 -5
- package/dist/index.js.map +1 -1
- package/dist/tests/index.test.js +1 -1
- package/dist/tests/index.test.js.map +1 -1
- package/docs/1.introduction.md +22 -18
- package/package.json +33 -19
package/README.md
CHANGED
|
@@ -1,28 +1,102 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
# Interface Redis Scheduler
|
|
1
|
+
# @antelopejs/interface-redis-scheduler
|
|
4
2
|
|
|
5
3
|
<div align="center">
|
|
6
|
-
<a href="https://www.npmjs.com/package/@antelopejs/interface-redis-scheduler"><img src="https://img.shields.io/npm/v/@antelopejs/interface-redis-scheduler?style=for-the-badge&labelColor=000000&color=000000"
|
|
7
|
-
<a href="./LICENSE"><img alt="License" src="https://img.shields.io/
|
|
8
|
-
<a href="https://discord.gg/sjK28QHrA7"><img src="https://img.shields.io/badge/Discord-18181B?logo=discord&style=for-the-badge&color=000000"
|
|
9
|
-
<a href="
|
|
4
|
+
<a href="https://www.npmjs.com/package/@antelopejs/interface-redis-scheduler"><img alt="npm" src="https://img.shields.io/npm/v/@antelopejs/interface-redis-scheduler?style=for-the-badge&labelColor=000000&color=000000"></a>
|
|
5
|
+
<a href="./LICENSE"><img alt="License" src="https://img.shields.io/npm/l/@antelopejs/interface-redis-scheduler?style=for-the-badge&labelColor=000000&color=000000"></a>
|
|
6
|
+
<a href="https://discord.gg/sjK28QHrA7"><img alt="Discord" src="https://img.shields.io/badge/Discord-18181B?logo=discord&style=for-the-badge&color=000000"></a>
|
|
7
|
+
<a href="https://interfaces.antelopejs.com/docs/redis-scheduler"><img alt="Docs" src="https://img.shields.io/badge/Docs-18181B?style=for-the-badge&color=000000"></a>
|
|
10
8
|
</div>
|
|
11
9
|
|
|
12
|
-
|
|
10
|
+
Task scheduling on Redis for AntelopeJS: schedule a task for a given time, and the application runs it when it is due, with up to three retries on failure.
|
|
11
|
+
|
|
12
|
+
## What it offers
|
|
13
|
+
|
|
14
|
+
- `setHandler` registers the function that runs one kind of task.
|
|
15
|
+
- `addTask` schedules a task for a Unix timestamp in milliseconds; `removeTask` cancels it.
|
|
16
|
+
- `enableListener` starts task processing; `disableListener` closes only the notification subscriber, not the pending timer.
|
|
17
|
+
- Tasks live in a Redis sorted set, so they survive restarts and every instance of the application sees the same queue.
|
|
18
|
+
|
|
19
|
+
## Implementations
|
|
20
|
+
|
|
21
|
+
| Module | Notes |
|
|
22
|
+
| ---------------------------------------------------------- | --------------------------------------------- |
|
|
23
|
+
| [`@antelopejs/redis`](https://github.com/AntelopeJS/redis) | Also implements `@antelopejs/interface-redis` |
|
|
24
|
+
|
|
25
|
+
## Install
|
|
26
|
+
|
|
27
|
+
Add the interface to the module that schedules tasks:
|
|
13
28
|
|
|
14
|
-
|
|
29
|
+
```bash
|
|
30
|
+
pnpm add @antelopejs/interface-redis-scheduler
|
|
31
|
+
```
|
|
32
|
+
|
|
33
|
+
Then add the implementation to the project:
|
|
15
34
|
|
|
16
35
|
```bash
|
|
17
|
-
|
|
36
|
+
ajs project modules add @antelopejs/redis
|
|
18
37
|
```
|
|
19
38
|
|
|
39
|
+
## Quick start
|
|
40
|
+
|
|
41
|
+
```ts
|
|
42
|
+
import { Logging } from "@antelopejs/interface-core/logging";
|
|
43
|
+
import {
|
|
44
|
+
addTask,
|
|
45
|
+
disableListener,
|
|
46
|
+
enableListener,
|
|
47
|
+
setHandler,
|
|
48
|
+
} from "@antelopejs/interface-redis-scheduler";
|
|
49
|
+
|
|
50
|
+
const ONE_HOUR_MS = 60 * 60 * 1000;
|
|
51
|
+
|
|
52
|
+
setHandler("overdue-notice", async (taskInfo) => {
|
|
53
|
+
const { memberId } = JSON.parse(taskInfo);
|
|
54
|
+
Logging.Info(`Remind member ${memberId}`);
|
|
55
|
+
});
|
|
56
|
+
|
|
57
|
+
export async function start() {
|
|
58
|
+
await enableListener();
|
|
59
|
+
await addTask(
|
|
60
|
+
"overdue-notice",
|
|
61
|
+
Date.now() + ONE_HOUR_MS,
|
|
62
|
+
JSON.stringify({ memberId: "M-1042" }),
|
|
63
|
+
);
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
export async function stop() {
|
|
67
|
+
await disableListener();
|
|
68
|
+
}
|
|
69
|
+
```
|
|
70
|
+
|
|
71
|
+
Register a handler before you schedule tasks for it: `addTask` throws for an unknown handler name.
|
|
72
|
+
|
|
73
|
+
`disableListener()` is not a full shutdown or pause: a pending timer and running handlers can continue processing tasks. See the [current lifecycle and delivery limitations](./docs/1.introduction.md#disablelistener) before using it for shutdown.
|
|
74
|
+
|
|
20
75
|
## Documentation
|
|
21
76
|
|
|
22
|
-
|
|
77
|
+
- [Introduction and scheduler reference](./docs/1.introduction.md)
|
|
78
|
+
|
|
79
|
+
## Compatibility
|
|
80
|
+
|
|
81
|
+
Requires AntelopeJS core `1.x` and the following peer dependencies:
|
|
23
82
|
|
|
24
|
-
-
|
|
83
|
+
<!-- docs-compat: ./package.json#peerDependencies -->
|
|
84
|
+
|
|
85
|
+
| Package | Range |
|
|
86
|
+
| ----------------------------- | ---------------- |
|
|
87
|
+
| `@antelopejs/interface-core` | `>=0.2.4 <1.0.0` |
|
|
88
|
+
| `@antelopejs/interface-redis` | `>=0.0.9 <1.0.0` |
|
|
89
|
+
|
|
90
|
+
## Contributing
|
|
91
|
+
|
|
92
|
+
Read [CONTRIBUTING.md](./CONTRIBUTING.md) before you open a pull request. The local checks are:
|
|
93
|
+
|
|
94
|
+
```bash
|
|
95
|
+
pnpm install
|
|
96
|
+
pnpm lint
|
|
97
|
+
pnpm test
|
|
98
|
+
```
|
|
25
99
|
|
|
26
100
|
## License
|
|
27
101
|
|
|
28
|
-
|
|
102
|
+
[Apache-2.0](./LICENSE)
|
package/dist/antelope.test.js
CHANGED
package/dist/index.d.ts
CHANGED
|
@@ -46,8 +46,9 @@ export declare function setHandler(handlerName: string, handler: HandlerType): v
|
|
|
46
46
|
*/
|
|
47
47
|
export declare function enableListener(): Promise<void>;
|
|
48
48
|
/**
|
|
49
|
-
*
|
|
50
|
-
* This
|
|
49
|
+
* Closes the Redis notification subscriber after enableListener() has completed.
|
|
50
|
+
* This does not clear the pending timer, stop running handlers, or await task completion.
|
|
51
|
+
* Tasks may continue to execute and schedule further timer checks; this is not a full shutdown.
|
|
51
52
|
*
|
|
52
53
|
* @example
|
|
53
54
|
* ```typescript
|
|
@@ -56,7 +57,7 @@ export declare function enableListener(): Promise<void>;
|
|
|
56
57
|
* // During application shutdown
|
|
57
58
|
* async function stopApp() {
|
|
58
59
|
* await disableListener();
|
|
59
|
-
* console.log('
|
|
60
|
+
* console.log('Scheduler notification subscriber closed; task processing may continue');
|
|
60
61
|
* }
|
|
61
62
|
* ```
|
|
62
63
|
*/
|
package/dist/index.js
CHANGED
|
@@ -7,9 +7,9 @@ exports.addTask = addTask;
|
|
|
7
7
|
exports.removeTask = removeTask;
|
|
8
8
|
exports.updateTimer = updateTimer;
|
|
9
9
|
exports.runTasks = runTasks;
|
|
10
|
-
const interface_core_1 = require("@antelopejs/interface-core");
|
|
11
|
-
const logging_1 = require("@antelopejs/interface-core/logging");
|
|
12
10
|
const interface_redis_1 = require("@antelopejs/interface-redis");
|
|
11
|
+
const logging_1 = require("@antelopejs/interface-core/logging");
|
|
12
|
+
const interface_core_1 = require("@antelopejs/interface-core");
|
|
13
13
|
/** Redis key used to store scheduled tasks in a sorted set */
|
|
14
14
|
const RedisKey = "SchedulerUtil.Tasks";
|
|
15
15
|
const SchedulerChannel = "SchedulerUtil";
|
|
@@ -99,8 +99,9 @@ async function enableListener() {
|
|
|
99
99
|
void updateTimer();
|
|
100
100
|
}
|
|
101
101
|
/**
|
|
102
|
-
*
|
|
103
|
-
* This
|
|
102
|
+
* Closes the Redis notification subscriber after enableListener() has completed.
|
|
103
|
+
* This does not clear the pending timer, stop running handlers, or await task completion.
|
|
104
|
+
* Tasks may continue to execute and schedule further timer checks; this is not a full shutdown.
|
|
104
105
|
*
|
|
105
106
|
* @example
|
|
106
107
|
* ```typescript
|
|
@@ -109,7 +110,7 @@ async function enableListener() {
|
|
|
109
110
|
* // During application shutdown
|
|
110
111
|
* async function stopApp() {
|
|
111
112
|
* await disableListener();
|
|
112
|
-
* console.log('
|
|
113
|
+
* console.log('Scheduler notification subscriber closed; task processing may continue');
|
|
113
114
|
* }
|
|
114
115
|
* ```
|
|
115
116
|
*/
|
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;
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":";;AAyDA,gCAEC;AAgCD,wCAgBC;AAkBD,0CAEC;AA8BD,0BAcC;AA0BD,gCAMC;AAWD,kCAyBC;AAOD,4BA6BC;AAlRD,iEAAwD;AACxD,gEAA6D;AAC7D,+DAA8D;AAQ9D,8DAA8D;AAC9D,MAAM,QAAQ,GAAG,qBAAqB,CAAC;AACvC,MAAM,gBAAgB,GAAG,eAAe,CAAC;AACzC,MAAM,sBAAsB,GAAG,QAAQ,CAAC;AACxC,MAAM,UAAU,GAAG,CAAC,CAAC;AACrB,MAAM,YAAY,GAAG,IAAI,CAAC;AAC1B,MAAM,eAAe,GAAG,QAAQ,CAAC;AACjC,MAAM,YAAY,GAAG,iCAAiC,CAAC;AAEvD,8CAA8C;AAC9C,MAAM,QAAQ,GAAG,IAAI,GAAG,EAAuB,CAAC;AAChD,wDAAwD;AACxD,MAAM,KAAK,GAAG,IAAI,iCAAgB,EAE/B,CAAC;AACJ,KAAK,CAAC,UAAU,CAAC,CAAC,WAAmB,EAAE,OAAoB,EAAE,EAAE;IAC7D,IAAI,WAAW,CAAC,OAAO,C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;QACrB,MAAM,MAAM,CAAC,IAAI,CAAC,QAAQ,EAAE,GAAG,KAAK,CAAC,CAAC;QACtC,KAAK,MAAM,IAAI,IAAI,KAAK,EAAE,CAAC;YACzB,MAAM,CAAC,GAAG,IAAI,CAAC,KAAK,CAAC,YAAY,CAAC,CAAC;YACnC,IAAI,CAAC;gBACH,IAAI,CAAC,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
|
@@ -1,8 +1,8 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
const chai_1 = require("chai");
|
|
3
4
|
const interface_redis_1 = require("@antelopejs/interface-redis");
|
|
4
5
|
const interface_redis_scheduler_1 = require("@antelopejs/interface-redis-scheduler");
|
|
5
|
-
const chai_1 = require("chai");
|
|
6
6
|
const RedisKey = "SchedulerUtil.Tasks";
|
|
7
7
|
async function cleanupRedis() {
|
|
8
8
|
const client = await (0, interface_redis_1.GetClient)();
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.test.js","sourceRoot":"","sources":["../../src/tests/index.test.ts"],"names":[],"mappings":";;AAAA,iEAAwD;AACxD,qFAO+C;
|
|
1
|
+
{"version":3,"file":"index.test.js","sourceRoot":"","sources":["../../src/tests/index.test.ts"],"names":[],"mappings":";;AAAA,+BAA8B;AAC9B,iEAAwD;AACxD,qFAO+C;AAE/C,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,GAAG,EAAE;IAC5C,EAAE,CAAC,2BAA2B,EAAE,KAAK,IAAI,EAAE,CAAC,mBAAmB,EAAE,CAAC,CAAC;IACnE,EAAE,CAAC,0CAA0C,EAAE,KAAK,IAAI,EAAE,CACxD,qBAAqB,EAAE,CAAC,CAAC;AAC7B,CAAC,CAAC,CAAC;AAEH,QAAQ,CAAC,2BAA2B,EAAE,GAAG,EAAE;IACzC,EAAE,CAAC,qCAAqC,EAAE,KAAK,IAAI,EAAE,CAAC,WAAW,EAAE,CAAC,CAAC;IACrE,EAAE,CAAC,kCAAkC,EAAE,KAAK,IAAI,EAAE,CAChD,yBAAyB,EAAE,CAAC,CAAC;AACjC,CAAC,CAAC,CAAC;AAEH,QAAQ,CAAC,8BAA8B,EAAE,GAAG,EAAE;IAC5C,EAAE,CAAC,0CAA0C,EAAE,KAAK,IAAI,EAAE,CAAC,cAAc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,CAAC,CAAC,CAAC;IAE9B,MAAM,YAAY,GAAG,MAAM,MAAM,CAAC,MAAM,CAAC,QAAQ,EAAE,CAAC,EAAE,CAAC,CAAC,CAAC,CAAC;IAC1D,IAAA,aAAM,EAAC,YAAY,CAAC,CAAC,EAAE,CAAC,IAAI,CAAC,MAAM,CAAC,CAAC,CAAC,CAAC;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
|
@@ -2,7 +2,9 @@
|
|
|
2
2
|
|
|
3
3
|
## Overview
|
|
4
4
|
|
|
5
|
-
Interface Redis Scheduler
|
|
5
|
+
Interface Redis Scheduler stores tasks in a Redis sorted set with their due timestamps as scores. A local timer checks for due tasks, and Redis pub/sub notifies listeners when the schedule changes. Handlers that throw are retried, but the current implementation does not provide exactly-once delivery or an atomic claim across listeners.
|
|
6
|
+
|
|
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.
|
|
6
8
|
|
|
7
9
|
## Getting Started
|
|
8
10
|
|
|
@@ -43,9 +45,9 @@ Handler names must not contain the `:` character, as the scheduler uses it inter
|
|
|
43
45
|
|
|
44
46
|
### Parameters
|
|
45
47
|
|
|
46
|
-
| Parameter | Type
|
|
47
|
-
| ------------- |
|
|
48
|
-
| `handlerName` | `string`
|
|
48
|
+
| Parameter | Type | Description |
|
|
49
|
+
| ------------- | --------------------------------------------- | ----------------------------------- |
|
|
50
|
+
| `handlerName` | `string` | Unique identifier for the task type |
|
|
49
51
|
| `handler` | `(taskInfo: string) => void \| Promise<void>` | Function that processes the task |
|
|
50
52
|
|
|
51
53
|
## `enableListener`
|
|
@@ -69,14 +71,16 @@ async function startApp() {
|
|
|
69
71
|
|
|
70
72
|
## `disableListener`
|
|
71
73
|
|
|
72
|
-
|
|
74
|
+
`disableListener()` returns `Promise<void>` and only calls `quit()` on the notification subscriber. Call it only after `enableListener()` has completed successfully.
|
|
75
|
+
|
|
76
|
+
> **Warning:** It does not clear the pending timer, cancel running handlers, or wait for task completion. An already scheduled timer can still execute tasks and schedule another timer through `updateTimer()`. Do not use this function as a pause, a drain, or a guarantee that no more tasks run. The current public API has no complete timer shutdown operation.
|
|
73
77
|
|
|
74
78
|
```typescript
|
|
75
79
|
import { disableListener } from "@antelopejs/interface-redis-scheduler";
|
|
76
80
|
|
|
77
81
|
async function stopApp() {
|
|
78
82
|
await disableListener();
|
|
79
|
-
console.log("
|
|
83
|
+
console.log("Notification subscriber closed; task processing may continue");
|
|
80
84
|
}
|
|
81
85
|
```
|
|
82
86
|
|
|
@@ -135,16 +139,16 @@ The function throws an error if the specified handler name has not been register
|
|
|
135
139
|
|
|
136
140
|
### Parameters
|
|
137
141
|
|
|
138
|
-
| Parameter | Type | Description
|
|
139
|
-
| ------------- | -------- |
|
|
140
|
-
| `handlerName` | `string` | The handler name associated with the task
|
|
142
|
+
| Parameter | Type | Description |
|
|
143
|
+
| ------------- | -------- | ---------------------------------------------------- |
|
|
144
|
+
| `handlerName` | `string` | The handler name associated with the task |
|
|
141
145
|
| `taskInfo` | `string` | The exact task info string used when adding the task |
|
|
142
146
|
|
|
143
147
|
## Retry Logic
|
|
144
148
|
|
|
145
149
|
The scheduler automatically retries failed tasks. When a handler throws an error, the scheduler reschedules the task with a retry counter.
|
|
146
150
|
|
|
147
|
-
- **Maximum retries:** 3 attempts
|
|
151
|
+
- **Maximum retries:** 3 retries after the initial attempt (4 attempts total)
|
|
148
152
|
- **Retry delay:** 5 seconds between each attempt
|
|
149
153
|
- **Behavior on exhaustion:** After 3 failed retries, the task is discarded and the error is logged to the console
|
|
150
154
|
|
|
@@ -167,7 +171,7 @@ The scheduler uses a single Redis sorted set with the key `SchedulerUtil.Tasks`.
|
|
|
167
171
|
|
|
168
172
|
For retried tasks, the format becomes `RETRY-{count}:handlerName:taskInfo`, where `{count}` tracks the number of retry attempts.
|
|
169
173
|
|
|
170
|
-
The scheduler also uses a Redis pub/sub channel named `SchedulerUtil` to notify listener instances when new tasks are added
|
|
174
|
+
The scheduler also uses a Redis pub/sub channel named `SchedulerUtil` to notify listener instances when new tasks are added. These notifications refresh timers; they do not lock or atomically claim tasks across processes.
|
|
171
175
|
|
|
172
176
|
## `updateTimer`
|
|
173
177
|
|
|
@@ -227,21 +231,21 @@ async function onUserSignup(userId: string, email: string) {
|
|
|
227
231
|
await addTask(
|
|
228
232
|
"send-email",
|
|
229
233
|
dueTime,
|
|
230
|
-
JSON.stringify({
|
|
234
|
+
JSON.stringify({
|
|
235
|
+
to: email,
|
|
236
|
+
subject: "Welcome!",
|
|
237
|
+
body: "Thanks for signing up.",
|
|
238
|
+
}),
|
|
231
239
|
);
|
|
232
240
|
}
|
|
233
241
|
|
|
234
242
|
async function onUserLogout(sessionId: string) {
|
|
235
243
|
// Clean up session data in 24 hours
|
|
236
244
|
const dueTime = Date.now() + 24 * 60 * 60 * 1000;
|
|
237
|
-
await addTask(
|
|
238
|
-
"cleanup-session",
|
|
239
|
-
dueTime,
|
|
240
|
-
JSON.stringify({ sessionId }),
|
|
241
|
-
);
|
|
245
|
+
await addTask("cleanup-session", dueTime, JSON.stringify({ sessionId }));
|
|
242
246
|
}
|
|
243
247
|
|
|
244
|
-
// 4.
|
|
248
|
+
// 4. Close notifications on shutdown (pending timers and handlers may continue)
|
|
245
249
|
async function shutdown() {
|
|
246
250
|
await disableListener();
|
|
247
251
|
}
|
package/package.json
CHANGED
|
@@ -1,26 +1,30 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@antelopejs/interface-redis-scheduler",
|
|
3
|
-
"version": "0.0.
|
|
4
|
-
"description": "AntelopeJS interface for Redis
|
|
3
|
+
"version": "0.0.8",
|
|
4
|
+
"description": "AntelopeJS interface for scheduling tasks on Redis, with retries on failure",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"antelopejs",
|
|
7
7
|
"interface",
|
|
8
8
|
"redis",
|
|
9
|
-
"scheduler"
|
|
9
|
+
"scheduler",
|
|
10
|
+
"tasks"
|
|
10
11
|
],
|
|
12
|
+
"homepage": "https://interfaces.antelopejs.com/docs/redis-scheduler",
|
|
13
|
+
"bugs": {
|
|
14
|
+
"url": "https://github.com/AntelopeJS/interface-redis-scheduler/issues"
|
|
15
|
+
},
|
|
16
|
+
"license": "Apache-2.0",
|
|
11
17
|
"repository": {
|
|
12
18
|
"type": "git",
|
|
13
19
|
"url": "git+https://github.com/AntelopeJS/interface-redis-scheduler.git"
|
|
14
20
|
},
|
|
15
|
-
"license": "Apache-2.0",
|
|
16
|
-
"packageManager": "pnpm@10.6.5",
|
|
17
|
-
"main": "dist/index.js",
|
|
18
|
-
"types": "dist/index.d.ts",
|
|
19
21
|
"files": [
|
|
20
22
|
"dist",
|
|
21
23
|
"docs",
|
|
22
24
|
"skills"
|
|
23
25
|
],
|
|
26
|
+
"main": "dist/index.js",
|
|
27
|
+
"types": "dist/index.d.ts",
|
|
24
28
|
"exports": {
|
|
25
29
|
".": {
|
|
26
30
|
"types": "./dist/index.d.ts",
|
|
@@ -28,38 +32,48 @@
|
|
|
28
32
|
},
|
|
29
33
|
"./package.json": "./package.json"
|
|
30
34
|
},
|
|
35
|
+
"publishConfig": {
|
|
36
|
+
"access": "public",
|
|
37
|
+
"provenance": true
|
|
38
|
+
},
|
|
31
39
|
"scripts": {
|
|
32
40
|
"build": "rimraf dist && tsc",
|
|
33
|
-
"format": "
|
|
34
|
-
"lint:fix": "
|
|
35
|
-
"lint": "
|
|
41
|
+
"format": "oxfmt .",
|
|
42
|
+
"lint:fix": "oxlint --fix",
|
|
43
|
+
"lint": "oxlint",
|
|
36
44
|
"prepack": "pnpm run build",
|
|
37
45
|
"release": "pnpm run lint && pnpm run prepack && release-it",
|
|
38
|
-
"test": "pnpm run build && ajs module test ."
|
|
46
|
+
"test": "pnpm run build && ajs module test .",
|
|
47
|
+
"format:check": "oxfmt --check .",
|
|
48
|
+
"knip": "knip"
|
|
39
49
|
},
|
|
40
50
|
"dependencies": {
|
|
41
51
|
"ioredis": "^5.10.1"
|
|
42
52
|
},
|
|
43
53
|
"devDependencies": {
|
|
44
|
-
"@antelopejs/
|
|
45
|
-
"@antelopejs/interface-
|
|
46
|
-
"@
|
|
54
|
+
"@antelopejs/core": "^1.16.0",
|
|
55
|
+
"@antelopejs/interface-core": "^0.2.4",
|
|
56
|
+
"@antelopejs/interface-redis": "^0.0.9",
|
|
57
|
+
"@antelopejs/tooling-configs": "^0.0.9",
|
|
47
58
|
"@types/chai": "^4.3.20",
|
|
48
59
|
"@types/mocha": "^10.0.10",
|
|
49
60
|
"@types/node": "^22.19.15",
|
|
50
61
|
"chai": "^4.5.0",
|
|
62
|
+
"eslint-plugin-perfectionist": "^5.11.0",
|
|
63
|
+
"knip": "^6.34.0",
|
|
64
|
+
"oxfmt": "^0.66.0",
|
|
65
|
+
"oxlint": "1.81.0",
|
|
66
|
+
"oxlint-tsgolint": "^7.0.2001",
|
|
51
67
|
"release-it": "^19.2.4",
|
|
52
68
|
"release-it-changelogen": "^0.1.0",
|
|
53
69
|
"rimraf": "^6.1.3",
|
|
54
70
|
"typescript": "^5.9.3"
|
|
55
71
|
},
|
|
56
72
|
"peerDependencies": {
|
|
57
|
-
"@antelopejs/interface-core": ">=0.
|
|
58
|
-
"@antelopejs/interface-redis": ">=0.0.
|
|
59
|
-
},
|
|
60
|
-
"publishConfig": {
|
|
61
|
-
"access": "public"
|
|
73
|
+
"@antelopejs/interface-core": ">=0.2.4 <1.0.0",
|
|
74
|
+
"@antelopejs/interface-redis": ">=0.0.9 <1.0.0"
|
|
62
75
|
},
|
|
76
|
+
"packageManager": "pnpm@10.6.5",
|
|
63
77
|
"antelopeJs": {
|
|
64
78
|
"test": "src/antelope.test.ts",
|
|
65
79
|
"skills": [
|