@quantabit/job-sdk 1.0.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 +110 -0
- package/dist/index.cjs +784 -0
- package/dist/index.cjs.map +1 -0
- package/dist/index.esm.js +774 -0
- package/dist/index.esm.js.map +1 -0
- package/package.json +74 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 QuantaBit Team
|
|
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,110 @@
|
|
|
1
|
+
# @quantabit/job-sdk (Job Queue SDK)
|
|
2
|
+
|
|
3
|
+
Universal message queue wrapper SDK supporting rapid migration from monolith to microservices. Internally uses a unified design pattern to seamlessly switch between **Redis** and **RabbitMQ** engines, implementing reliable task delivery, consumption, and retry mechanisms.
|
|
4
|
+
|
|
5
|
+
## Features
|
|
6
|
+
|
|
7
|
+
- **Multi-engine support (`Redis` and `RabbitMQ`)**
|
|
8
|
+
- **Ready to use**: Built on `bullmq` (Redis) / `amqplib` (RabbitMQ), simplifying complex configurations
|
|
9
|
+
- **Standard Payload**: Auto-generated `taskId`, write timestamp throttle
|
|
10
|
+
- **Built-in retry/delay queue**: Redis engine supports `delay` and `retries` parameters directly
|
|
11
|
+
- **Full-stack compatible**: Works with Node.js, Next.js server, Nest, and Express projects
|
|
12
|
+
|
|
13
|
+
## Installation
|
|
14
|
+
|
|
15
|
+
```bash
|
|
16
|
+
cd <your-project>
|
|
17
|
+
npm install @quantabit/job-sdk
|
|
18
|
+
```
|
|
19
|
+
|
|
20
|
+
## Quick Start
|
|
21
|
+
|
|
22
|
+
### 1. Initialize Engine
|
|
23
|
+
|
|
24
|
+
Specify `engine` to switch the backend implementation at any time — business code remains transparent.
|
|
25
|
+
|
|
26
|
+
```javascript
|
|
27
|
+
import { JobQueue } from "@quantabit/job-sdk";
|
|
28
|
+
|
|
29
|
+
// Use Redis (default, suitable for common microservices/task distribution)
|
|
30
|
+
const jobQueue = new JobQueue({
|
|
31
|
+
engine: "redis",
|
|
32
|
+
config: {
|
|
33
|
+
connection: { host: "127.0.0.1", port: 6379 }, // ioredis config
|
|
34
|
+
},
|
|
35
|
+
});
|
|
36
|
+
|
|
37
|
+
// Use RabbitMQ (suitable for high-throughput, complex dead-letter queue scenarios)
|
|
38
|
+
const rabbitQueue = new JobQueue({
|
|
39
|
+
engine: "rabbitmq",
|
|
40
|
+
config: {
|
|
41
|
+
url: "amqp://localhost", // RabbitMQ connection string
|
|
42
|
+
},
|
|
43
|
+
});
|
|
44
|
+
```
|
|
45
|
+
|
|
46
|
+
### 2. Publish Task (Publisher)
|
|
47
|
+
|
|
48
|
+
```javascript
|
|
49
|
+
await jobQueue.connect();
|
|
50
|
+
|
|
51
|
+
// Unified publish behavior with auto-generated taskId, source info, built-in retry and delay options
|
|
52
|
+
const jobId = await jobQueue.publishTask(
|
|
53
|
+
"deploy-queue",
|
|
54
|
+
{
|
|
55
|
+
projectId: "plans",
|
|
56
|
+
repository: "org/repo",
|
|
57
|
+
commitId: "abcdef",
|
|
58
|
+
},
|
|
59
|
+
{
|
|
60
|
+
source: "github-intel-deploy", // Mark source
|
|
61
|
+
retries: 3, // Failed retry count (Redis engine)
|
|
62
|
+
delay: 2000, // Delay 2s before processing (requires backend support)
|
|
63
|
+
},
|
|
64
|
+
);
|
|
65
|
+
|
|
66
|
+
console.log("Task assigned, JobID:", jobId);
|
|
67
|
+
```
|
|
68
|
+
|
|
69
|
+
### 3. Consume Task (Consumer)
|
|
70
|
+
|
|
71
|
+
Regardless of whether you use Redis or RabbitMQ, the `handler` function signature is consistent.
|
|
72
|
+
|
|
73
|
+
```javascript
|
|
74
|
+
await jobQueue.connect();
|
|
75
|
+
|
|
76
|
+
jobQueue.consumeTask(
|
|
77
|
+
"deploy-queue",
|
|
78
|
+
async (data, context) => {
|
|
79
|
+
// data : published payload { projectId, repository, commitId }
|
|
80
|
+
// context: { taskId, source, timestamp, raw } - SDK provides standard context
|
|
81
|
+
|
|
82
|
+
console.log(
|
|
83
|
+
`[${context.source}] Received deploy task, Project: ${data.projectId}, TaskID: ${context.taskId}`,
|
|
84
|
+
);
|
|
85
|
+
|
|
86
|
+
// Simulate task processing logic; if an error is thrown, auto-enters failed retry pool
|
|
87
|
+
// throw new Error("Deploy failed");
|
|
88
|
+
},
|
|
89
|
+
{
|
|
90
|
+
concurrency: 5, // Concurrent processing count
|
|
91
|
+
},
|
|
92
|
+
);
|
|
93
|
+
```
|
|
94
|
+
|
|
95
|
+
## Best Practices & Use Cases
|
|
96
|
+
|
|
97
|
+
1. **Unified Admin Backend**: Dispatch async tasks directly via SDK (e.g., "rebuild full-text index", "batch send emails", "export Excel").
|
|
98
|
+
2. **Eco Apps Deployment Pipeline**: Listen and execute long-running tasks (e.g., running Python `verify_deployment.py`) and update cloud services.
|
|
99
|
+
3. **Order System Deferred Processing**: Use the `delay` option to auto-cancel unpaid orders after timeout.
|
|
100
|
+
|
|
101
|
+
|
|
102
|
+
|
|
103
|
+
---
|
|
104
|
+
|
|
105
|
+
## 🌐 Brand & Links
|
|
106
|
+
- Official Mainnet: [QuantaBit Chain](https://qbitchain.io/)
|
|
107
|
+
- Developer Platform: [Developer Platform](https://developer.quantabit.io/)
|
|
108
|
+
- Open Platform: [Open Platform](https://open.quantabit.io/)
|
|
109
|
+
- Payment Platform: [Pay Platform](https://pay.qbitwallet.io/)
|
|
110
|
+
- Feedback: [Feedback](https://xwin.live/qbit)
|