You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
The EquipChain platform requires reliable execution of background tasks that should not block API responses or require user-facing synchronization. These tasks include: scheduled billing cycles (aggregating meter readings and computing charges), periodic report generation (daily/monthly usage reports), data synchronization with the Soroban blockchain (verifying transaction statuses, syncing contract state), webhook delivery retries (from Issue #14), and cache warming for frequently accessed data.
This issue implements a background job queue system that can schedule, execute, and monitor deferred or recurring tasks. The system should support: immediate job execution (fire-and-forget), delayed execution (schedule a job for a future time), recurring/cron-based jobs (e.g., every hour, daily at midnight), job prioritization (high, normal, low), concurrency control (max N jobs running simultaneously), job retry with backoff on failure, and job result/status tracking for monitoring.
For the initial implementation, the queue can use an in-memory store (with Bull/BullMQ for Redis-backed production use planned as a future upgrade). The queue should expose a clean API: queue.add(name, data, options), queue.schedule(name, data, cronExpression), queue.getStatus(jobId), queue.cancel(jobId). Each job type should be defined as a handler function in a dedicated src/jobs/ directory.
Technical Context & Impact
Dependencies: For MVP, no external dependencies — use Node.js EventEmitter and setTimeout. For production, plan migration to bullmq (Redis-backed). Environment variables: JOB_CONCURRENCY (default 5), JOB_RETRY_ATTEMPTS (default 3).
Architecture: New src/services/queue.js implementing the queue. New src/jobs/ directory with handler files: billing.job.js, reports.job.js, sync.job.js, webhookRetry.job.js, cacheWarm.job.js. An src/services/scheduler.js for cron-based scheduling.
Impact: Enables asynchronous processing that is essential for production features. Without this, billing calculations would block API responses, webhook retries would be unreliable, and scheduled reports would require manual triggering.
Step-by-Step Implementation Guide
Create Queue Service: Write src/services/queue.js implementing an in-memory job queue. Features: add(type, data, options) returns job ID, process() pulls from queue with concurrency control, onComplete/onFailed callbacks, retry logic with exponential backoff, and job status tracking (queued, running, completed, failed).
Create Job Handlers: Create src/jobs/billing.job.js — aggregates readings and computes charges. src/jobs/reports.job.js — generates daily/monthly summary reports. src/jobs/sync.job.js — syncs on-chain data with local state. Keep handlers as placeholder functions initially, with detailed implementation in linked issues.
Create Scheduler: Write src/services/scheduler.js that manages cron-like recurring jobs. Use setInterval for MVP (e.g., run billing every hour). Store scheduled job configurations and provide schedule(name, cronExpression, handler) and cancelSchedule(name).
Integrate with Application: In src/index.js, initialize the queue and scheduler on startup. Register job handlers. Start the queue processor. Add health check endpoint exposing queue stats (waiting, active, completed, failed counts).
Description
The EquipChain platform requires reliable execution of background tasks that should not block API responses or require user-facing synchronization. These tasks include: scheduled billing cycles (aggregating meter readings and computing charges), periodic report generation (daily/monthly usage reports), data synchronization with the Soroban blockchain (verifying transaction statuses, syncing contract state), webhook delivery retries (from Issue #14), and cache warming for frequently accessed data.
This issue implements a background job queue system that can schedule, execute, and monitor deferred or recurring tasks. The system should support: immediate job execution (fire-and-forget), delayed execution (schedule a job for a future time), recurring/cron-based jobs (e.g., every hour, daily at midnight), job prioritization (high, normal, low), concurrency control (max N jobs running simultaneously), job retry with backoff on failure, and job result/status tracking for monitoring.
For the initial implementation, the queue can use an in-memory store (with Bull/BullMQ for Redis-backed production use planned as a future upgrade). The queue should expose a clean API:
queue.add(name, data, options),queue.schedule(name, data, cronExpression),queue.getStatus(jobId),queue.cancel(jobId). Each job type should be defined as a handler function in a dedicatedsrc/jobs/directory.Technical Context & Impact
bullmq(Redis-backed). Environment variables:JOB_CONCURRENCY(default 5),JOB_RETRY_ATTEMPTS(default 3).src/services/queue.jsimplementing the queue. Newsrc/jobs/directory with handler files:billing.job.js,reports.job.js,sync.job.js,webhookRetry.job.js,cacheWarm.job.js. Ansrc/services/scheduler.jsfor cron-based scheduling.Step-by-Step Implementation Guide
src/services/queue.jsimplementing an in-memory job queue. Features:add(type, data, options)returns job ID,process()pulls from queue with concurrency control,onComplete/onFailedcallbacks, retry logic with exponential backoff, and job status tracking (queued, running, completed, failed).src/jobs/billing.job.js— aggregates readings and computes charges.src/jobs/reports.job.js— generates daily/monthly summary reports.src/jobs/sync.job.js— syncs on-chain data with local state. Keep handlers as placeholder functions initially, with detailed implementation in linked issues.src/services/scheduler.jsthat manages cron-like recurring jobs. UsesetIntervalfor MVP (e.g., run billing every hour). Store scheduled job configurations and provideschedule(name, cronExpression, handler)andcancelSchedule(name).src/index.js, initialize the queue and scheduler on startup. Register job handlers. Start the queue processor. Add health check endpoint exposing queue stats (waiting, active, completed, failed counts).tests/unit/queue.test.jstesting job lifecycle: add, process, complete, fail, retry. Test concurrency limits. Createtests/unit/scheduler.test.jstesting recurring job execution timing.Verification & Testing Steps