pglite-queue 0.1.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/LICENSE +21 -0
- package/README.md +226 -0
- package/dist/index.cjs +789 -0
- package/dist/index.cjs.map +1 -0
- package/dist/index.d.cts +157 -0
- package/dist/index.d.ts +157 -0
- package/dist/index.js +759 -0
- package/dist/index.js.map +1 -0
- package/package.json +61 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
ADDED
|
@@ -0,0 +1,226 @@
|
|
|
1
|
+
# pglite-queue
|
|
2
|
+
|
|
3
|
+
Zero-infrastructure background job queue for Node.js, powered by [PGlite](https://github.com/electric-sql/pglite) (embedded Postgres via WASM).
|
|
4
|
+
|
|
5
|
+
**BullMQ-like DX. No Redis. No external database. Just `npm install` and go.**
|
|
6
|
+
|
|
7
|
+
```
|
|
8
|
+
npm install pglite-queue @electric-sql/pglite
|
|
9
|
+
```
|
|
10
|
+
|
|
11
|
+
## Why?
|
|
12
|
+
|
|
13
|
+
Every existing job queue (BullMQ, bee-queue, agenda) requires you to run an external database. For many projects, that's unnecessary complexity. `pglite-queue` embeds a full Postgres instance directly in your Node.js process — zero infrastructure, zero Docker, zero config.
|
|
14
|
+
|
|
15
|
+
| Feature | pglite-queue | BullMQ | Agenda |
|
|
16
|
+
|---|---|---|---|
|
|
17
|
+
| External DB required | No | Redis | MongoDB |
|
|
18
|
+
| Setup time | 0 | Minutes | Minutes |
|
|
19
|
+
| Retry with backoff | Yes | Yes | Yes |
|
|
20
|
+
| Cron jobs | Yes | Yes | Yes |
|
|
21
|
+
| Priority queues | Yes | Yes | No |
|
|
22
|
+
| Concurrency control | Yes | Yes | Yes |
|
|
23
|
+
| Progress tracking | Yes | Yes | No |
|
|
24
|
+
| TypeScript-first | Yes | Yes | No |
|
|
25
|
+
| Bundle size | ~22 KB | ~150 KB | ~80 KB |
|
|
26
|
+
|
|
27
|
+
## Quick Start
|
|
28
|
+
|
|
29
|
+
```ts
|
|
30
|
+
import { Queue } from 'pglite-queue'
|
|
31
|
+
|
|
32
|
+
const queue = new Queue()
|
|
33
|
+
|
|
34
|
+
// Define a handler
|
|
35
|
+
queue.define('send-email', async (job) => {
|
|
36
|
+
console.log(`Sending email to ${job.data.to}`)
|
|
37
|
+
// your logic here
|
|
38
|
+
return { sent: true }
|
|
39
|
+
})
|
|
40
|
+
|
|
41
|
+
// Start processing
|
|
42
|
+
await queue.start()
|
|
43
|
+
|
|
44
|
+
// Add a job
|
|
45
|
+
await queue.add('send-email', { to: 'user@example.com', subject: 'Hello' })
|
|
46
|
+
```
|
|
47
|
+
|
|
48
|
+
## Features
|
|
49
|
+
|
|
50
|
+
### Retry with Exponential Backoff
|
|
51
|
+
|
|
52
|
+
```ts
|
|
53
|
+
await queue.add('flaky-api-call', { url: '...' }, {
|
|
54
|
+
retry: 5, // retry up to 5 times (6 total attempts)
|
|
55
|
+
backoff: {
|
|
56
|
+
type: 'exponential', // or 'fixed'
|
|
57
|
+
baseDelay: 1000, // 1s, 2s, 4s, 8s, 16s...
|
|
58
|
+
maxDelay: 300000, // cap at 5 minutes
|
|
59
|
+
},
|
|
60
|
+
})
|
|
61
|
+
```
|
|
62
|
+
|
|
63
|
+
### Priority Queues
|
|
64
|
+
|
|
65
|
+
```ts
|
|
66
|
+
// Lower number = higher priority
|
|
67
|
+
await queue.add('critical', data, { priority: 1 })
|
|
68
|
+
await queue.add('normal', data, { priority: 5 })
|
|
69
|
+
await queue.add('low', data, { priority: 10 })
|
|
70
|
+
```
|
|
71
|
+
|
|
72
|
+
### Delayed Jobs
|
|
73
|
+
|
|
74
|
+
```ts
|
|
75
|
+
await queue.add('reminder', data, { delay: '30m' }) // run in 30 minutes
|
|
76
|
+
await queue.add('cleanup', data, { delay: '2h' }) // run in 2 hours
|
|
77
|
+
await queue.add('precise', data, { delay: 5000 }) // run in 5000ms
|
|
78
|
+
```
|
|
79
|
+
|
|
80
|
+
Supported units: `s` (seconds), `m` (minutes), `h` (hours), `d` (days).
|
|
81
|
+
|
|
82
|
+
### Cron / Recurring Jobs
|
|
83
|
+
|
|
84
|
+
```ts
|
|
85
|
+
// Every day at 9:00 UTC
|
|
86
|
+
await queue.every('0 9 * * *', 'daily-report', { type: 'summary' })
|
|
87
|
+
|
|
88
|
+
// Every 15 minutes
|
|
89
|
+
await queue.every('*/15 * * * *', 'health-check', {})
|
|
90
|
+
|
|
91
|
+
// Weekdays at 6pm
|
|
92
|
+
await queue.every('0 18 * * 1-5', 'eod-sync', {})
|
|
93
|
+
```
|
|
94
|
+
|
|
95
|
+
Standard 5-field cron format: `minute hour day-of-month month day-of-week`
|
|
96
|
+
|
|
97
|
+
### Job Progress Tracking
|
|
98
|
+
|
|
99
|
+
```ts
|
|
100
|
+
queue.define('video-encode', async (job) => {
|
|
101
|
+
for (let i = 0; i <= 100; i += 10) {
|
|
102
|
+
await doWork()
|
|
103
|
+
await job.progress(i)
|
|
104
|
+
}
|
|
105
|
+
})
|
|
106
|
+
|
|
107
|
+
queue.on('progress', (job, pct) => {
|
|
108
|
+
console.log(`Job ${job.id}: ${pct}%`)
|
|
109
|
+
})
|
|
110
|
+
```
|
|
111
|
+
|
|
112
|
+
### Concurrency Control
|
|
113
|
+
|
|
114
|
+
```ts
|
|
115
|
+
const queue = new Queue({
|
|
116
|
+
concurrency: 5, // process up to 5 jobs in parallel
|
|
117
|
+
})
|
|
118
|
+
```
|
|
119
|
+
|
|
120
|
+
### Events
|
|
121
|
+
|
|
122
|
+
```ts
|
|
123
|
+
queue.on('active', (job) => console.log(`Started: ${job.id}`))
|
|
124
|
+
queue.on('completed', (job) => console.log(`Done: ${job.id}`))
|
|
125
|
+
queue.on('failed', (job, err) => console.log(`Failed: ${job.id} - ${err.message}`))
|
|
126
|
+
queue.on('retrying', (job, attempt) => console.log(`Retry #${attempt}: ${job.id}`))
|
|
127
|
+
queue.on('progress', (job, pct) => console.log(`${job.id}: ${pct}%`))
|
|
128
|
+
queue.on('drained', () => console.log('All jobs processed'))
|
|
129
|
+
queue.on('error', (err) => console.error('Queue error:', err))
|
|
130
|
+
```
|
|
131
|
+
|
|
132
|
+
### Persistent Storage
|
|
133
|
+
|
|
134
|
+
```ts
|
|
135
|
+
// In-memory (default) — data lost on restart
|
|
136
|
+
const queue = new Queue()
|
|
137
|
+
|
|
138
|
+
// Filesystem — survives restarts
|
|
139
|
+
const queue = new Queue({ dataDir: './my-queue-data' })
|
|
140
|
+
|
|
141
|
+
// Bring your own PGlite instance
|
|
142
|
+
import { PGlite } from '@electric-sql/pglite'
|
|
143
|
+
const db = new PGlite('./shared-db')
|
|
144
|
+
const queue = new Queue({ db })
|
|
145
|
+
```
|
|
146
|
+
|
|
147
|
+
### Graceful Shutdown
|
|
148
|
+
|
|
149
|
+
```ts
|
|
150
|
+
// Waits for active jobs to finish before stopping
|
|
151
|
+
await queue.stop()
|
|
152
|
+
|
|
153
|
+
// Or let the queue handle SIGINT/SIGTERM automatically
|
|
154
|
+
const queue = new Queue({ handleSignals: true })
|
|
155
|
+
```
|
|
156
|
+
|
|
157
|
+
### Job Management
|
|
158
|
+
|
|
159
|
+
```ts
|
|
160
|
+
// Get a specific job
|
|
161
|
+
const job = await queue.getJob(42)
|
|
162
|
+
|
|
163
|
+
// Query jobs
|
|
164
|
+
const failed = await queue.getJobs({ status: 'failed' })
|
|
165
|
+
const recent = await queue.getJobs({ task: 'send-email', limit: 10 })
|
|
166
|
+
|
|
167
|
+
// Remove a job
|
|
168
|
+
await queue.removeJob(42)
|
|
169
|
+
|
|
170
|
+
// Clean up old jobs
|
|
171
|
+
await queue.clean('completed') // remove all completed
|
|
172
|
+
await queue.clean('failed') // remove all failed
|
|
173
|
+
await queue.clean() // remove both
|
|
174
|
+
|
|
175
|
+
// Get counts
|
|
176
|
+
const counts = await queue.counts()
|
|
177
|
+
// { pending: 5, active: 2, completed: 100, failed: 3 }
|
|
178
|
+
```
|
|
179
|
+
|
|
180
|
+
## API Reference
|
|
181
|
+
|
|
182
|
+
### `new Queue(options?)`
|
|
183
|
+
|
|
184
|
+
| Option | Type | Default | Description |
|
|
185
|
+
|---|---|---|---|
|
|
186
|
+
| `dataDir` | `string` | `'memory://'` | PGlite data directory. Use a path for persistence. |
|
|
187
|
+
| `db` | `PGlite` | - | Existing PGlite instance to use |
|
|
188
|
+
| `concurrency` | `number` | `1` | Max parallel job processing |
|
|
189
|
+
| `pollInterval` | `number` | `5000` | Fallback polling interval (ms) |
|
|
190
|
+
| `shutdownTimeout` | `number` | `30000` | Max time to wait for jobs during shutdown (ms) |
|
|
191
|
+
| `handleSignals` | `boolean` | `false` | Auto-handle SIGINT/SIGTERM |
|
|
192
|
+
|
|
193
|
+
### `queue.define(task, handler, options?)`
|
|
194
|
+
|
|
195
|
+
Register a handler for a task name.
|
|
196
|
+
|
|
197
|
+
### `queue.add(task, data, options?)`
|
|
198
|
+
|
|
199
|
+
Add a job. Returns a `Job` object.
|
|
200
|
+
|
|
201
|
+
| Option | Type | Default | Description |
|
|
202
|
+
|---|---|---|---|
|
|
203
|
+
| `retry` | `number` | `0` | Number of retries (total attempts = retry + 1) |
|
|
204
|
+
| `delay` | `number \| string` | - | Delay before execution |
|
|
205
|
+
| `priority` | `number` | `0` | Lower = higher priority |
|
|
206
|
+
|
|
207
|
+
### `queue.every(cronExpr, task, data?, options?)`
|
|
208
|
+
|
|
209
|
+
Register a recurring cron job.
|
|
210
|
+
|
|
211
|
+
### `queue.start()` / `queue.stop()`
|
|
212
|
+
|
|
213
|
+
Start/stop the worker.
|
|
214
|
+
|
|
215
|
+
## How It Works
|
|
216
|
+
|
|
217
|
+
- Uses **PGlite** (Postgres compiled to WASM) as an in-process database
|
|
218
|
+
- Jobs are stored in a Postgres table with proper indexes
|
|
219
|
+
- **LISTEN/NOTIFY** triggers instant job pickup on insert (no polling delay)
|
|
220
|
+
- **FOR UPDATE SKIP LOCKED** ensures safe concurrent processing
|
|
221
|
+
- Fallback polling catches delayed jobs and edge cases
|
|
222
|
+
- On crash recovery, stalled `active` jobs are automatically reset to `pending`
|
|
223
|
+
|
|
224
|
+
## License
|
|
225
|
+
|
|
226
|
+
MIT
|