@efebia/queue 0.0.6 → 0.0.7
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 +118 -0
- package/package.json +1 -1
package/README.md
ADDED
|
@@ -0,0 +1,118 @@
|
|
|
1
|
+
# @efebia/queue
|
|
2
|
+
|
|
3
|
+
Abstract base class for building message queue consumers, with a built-in in-memory implementation.
|
|
4
|
+
|
|
5
|
+
## Install
|
|
6
|
+
|
|
7
|
+
```bash
|
|
8
|
+
npm install @efebia/queue
|
|
9
|
+
```
|
|
10
|
+
|
|
11
|
+
## Usage
|
|
12
|
+
|
|
13
|
+
### Extending `Queue`
|
|
14
|
+
|
|
15
|
+
Implement the three abstract methods to connect any queue backend:
|
|
16
|
+
|
|
17
|
+
```ts
|
|
18
|
+
import { Queue, QueueCallback, SentMessage } from '@efebia/queue';
|
|
19
|
+
|
|
20
|
+
type MyMessage = { userId: string; action: string };
|
|
21
|
+
|
|
22
|
+
class MyQueue extends Queue<RawSQSMessage, MyMessage> {
|
|
23
|
+
async readMessages() {
|
|
24
|
+
// fetch messages from your backend, return [] or undefined if none
|
|
25
|
+
return await fetchFromBackend();
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
async processMessage(raw: RawSQSMessage, callback?: QueueCallback<MyMessage>) {
|
|
29
|
+
const parsed = parse(raw);
|
|
30
|
+
const state = { del: true };
|
|
31
|
+
await callback?.(parsed.name, parsed.params, state);
|
|
32
|
+
if (state.del) await deleteFromBackend(raw);
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
async sendMessage(body: SentMessage<MyMessage>) {
|
|
36
|
+
await sendToBackend(JSON.stringify(body));
|
|
37
|
+
}
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
const queue = new MyQueue({ sleepTimeout: 3000 });
|
|
41
|
+
|
|
42
|
+
// Start the polling loop
|
|
43
|
+
queue.start(async (name, params, state) => {
|
|
44
|
+
console.log(name, params);
|
|
45
|
+
// set state.del = false to prevent deletion
|
|
46
|
+
});
|
|
47
|
+
```
|
|
48
|
+
|
|
49
|
+
### Using `MemoryQueue`
|
|
50
|
+
|
|
51
|
+
A ready-to-use in-memory queue for testing or simple use cases:
|
|
52
|
+
|
|
53
|
+
```ts
|
|
54
|
+
import { MemoryQueue } from '@efebia/queue';
|
|
55
|
+
|
|
56
|
+
type Job = { task: string };
|
|
57
|
+
|
|
58
|
+
const queue = new MemoryQueue<Job>({});
|
|
59
|
+
|
|
60
|
+
// Send a message
|
|
61
|
+
await queue.send({ name: 'doWork', params: { task: 'hello' } });
|
|
62
|
+
|
|
63
|
+
// Start consuming
|
|
64
|
+
queue.start(async (name, params, state) => {
|
|
65
|
+
console.log(`Processing ${name}:`, params);
|
|
66
|
+
});
|
|
67
|
+
|
|
68
|
+
// Stop the loop
|
|
69
|
+
queue.stop();
|
|
70
|
+
```
|
|
71
|
+
|
|
72
|
+
## API
|
|
73
|
+
|
|
74
|
+
### `Queue<TReceivedMessage, TMessage>` (abstract)
|
|
75
|
+
|
|
76
|
+
**Constructor options (`QueueOptions`):**
|
|
77
|
+
|
|
78
|
+
| Option | Type | Default | Description |
|
|
79
|
+
|--------|------|---------|-------------|
|
|
80
|
+
| `sleepTimeout` | `number` | `5000` | Milliseconds to wait after an error before retrying |
|
|
81
|
+
| `errorCallback` | `(error) => void` | `console.error` | Called on uncaught errors in the polling loop |
|
|
82
|
+
|
|
83
|
+
**Abstract methods to implement:**
|
|
84
|
+
|
|
85
|
+
| Method | Description |
|
|
86
|
+
|--------|-------------|
|
|
87
|
+
| `readMessages()` | Fetch the next batch of messages. Return `undefined` to skip the current iteration. |
|
|
88
|
+
| `processMessage(message, callback?)` | Process a single message and invoke the callback. |
|
|
89
|
+
| `sendMessage(body)` | Deliver a message to the underlying queue backend. |
|
|
90
|
+
|
|
91
|
+
**Methods:**
|
|
92
|
+
|
|
93
|
+
- `start(callback, canThrow?)` — begins the infinite polling loop
|
|
94
|
+
- `stop()` — signals the loop to exit after the current iteration
|
|
95
|
+
- `resume(callback)` — restarts a stopped queue
|
|
96
|
+
- `isRunning()` — returns `true` if the loop is active
|
|
97
|
+
- `send(message)` — sends a message, automatically adding `createdAt` to params
|
|
98
|
+
|
|
99
|
+
**Callback signature:**
|
|
100
|
+
|
|
101
|
+
```ts
|
|
102
|
+
(name: string, params: TMessage, state: { del: boolean }) => void | Promise<void>
|
|
103
|
+
```
|
|
104
|
+
|
|
105
|
+
Set `state.del = false` inside the callback to prevent the message from being deleted after processing.
|
|
106
|
+
|
|
107
|
+
### `MemoryQueue<TMessage>`
|
|
108
|
+
|
|
109
|
+
Extends `Queue` with an in-memory message array. Useful for tests and local development.
|
|
110
|
+
|
|
111
|
+
### Types
|
|
112
|
+
|
|
113
|
+
- `Message<TParams>` — `{ name: string; params: TParams }`
|
|
114
|
+
- `SentMessage<TParams>` — `Message` with `params.createdAt: Date` added
|
|
115
|
+
|
|
116
|
+
## License
|
|
117
|
+
|
|
118
|
+
MIT
|