@quantabit/job-sdk
v1.0.2
Published
QuantaBit Job Queue SDK - Universal message queue wrapper with Redis/RabbitMQ multi-engine support
Readme
@quantabit/job-sdk (Job Queue SDK)
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.
Features
- Multi-engine support (
RedisandRabbitMQ) - Ready to use: Built on
bullmq(Redis) /amqplib(RabbitMQ), simplifying complex configurations - Standard Payload: Auto-generated
taskId, write timestamp throttle - Built-in retry/delay queue: Redis engine supports
delayandretriesparameters directly - Full-stack compatible: Works with Node.js, Next.js server, Nest, and Express projects
Installation
cd <your-project>
npm install @quantabit/job-sdkQuick Start
1. Initialize Engine
Specify engine to switch the backend implementation at any time — business code remains transparent.
import { JobQueue } from "@quantabit/job-sdk";
// Use Redis (default, suitable for common microservices/task distribution)
const jobQueue = new JobQueue({
engine: "redis",
config: {
connection: { host: "127.0.0.1", port: 6379 }, // ioredis config
},
});
// Use RabbitMQ (suitable for high-throughput, complex dead-letter queue scenarios)
const rabbitQueue = new JobQueue({
engine: "rabbitmq",
config: {
url: "amqp://127.0.0.1", // RabbitMQ connection string
},
});2. Publish Task (Publisher)
await jobQueue.connect();
// Unified publish behavior with auto-generated taskId, source info, built-in retry and delay options
const jobId = await jobQueue.publishTask(
"deploy-queue",
{
projectId: "plans",
repository: "org/repo",
commitId: "abcdef",
},
{
source: "github-intel-deploy", // Mark source
retries: 3, // Failed retry count (Redis engine)
delay: 2000, // Delay 2s before processing (requires backend support)
},
);
console.log("Task assigned, JobID:", jobId);3. Consume Task (Consumer)
Regardless of whether you use Redis or RabbitMQ, the handler function signature is consistent.
await jobQueue.connect();
jobQueue.consumeTask(
"deploy-queue",
async (data, context) => {
// data : published payload { projectId, repository, commitId }
// context: { taskId, source, timestamp, raw } - SDK provides standard context
console.log(
`[${context.source}] Received deploy task, Project: ${data.projectId}, TaskID: ${context.taskId}`,
);
// Simulate task processing logic; if an error is thrown, auto-enters failed retry pool
// throw new Error("Deploy failed");
},
{
concurrency: 5, // Concurrent processing count
},
);Best Practices & Use Cases
- Unified Admin Backend: Dispatch async tasks directly via SDK (e.g., "rebuild full-text index", "batch send emails", "export Excel").
- Eco Apps Deployment Pipeline: Listen and execute long-running tasks (e.g., running Python
verify_deployment.py) and update cloud services. - Order System Deferred Processing: Use the
delayoption to auto-cancel unpaid orders after timeout.
🌐 Brand & Links
- Official Mainnet: QuantaBit Chain
- Developer Platform: Developer Platform
- Open Platform: Open Platform
- Payment Platform: Pay Platform
- Feedback: Feedback
