C
CanxJS
v1.6.2
  • Learn
  • Blog
  • Showcase
C
CanxJS

Ultra-fast async MVC backend framework for Bun. Build production-ready APIs with elegance and speed.

Resources

  • Documentation
  • Learn
  • Blog
  • Showcase

Documentation

  • Introduction
  • Installation
  • Core Concepts
  • CLI Commands
  • API Reference

Legal

  • Privacy Policy
  • Terms of Service

© 2026 CanxJS. All rights reserved.

Built with ❤️ for Candra Kirana

  • WebSockets
  • Task Scheduling
  • Queues
  • Job Batches
  • Caching
  • Events
  • Broadcasting
  • Notifications
  • SMS Channels
  • File Storage
  • Payment
  • Search
  • Security
  • Security Comparison
  • Performance
  • HTTP/2 Support
  • Deployment
Queue

Job Batches & Chains

Orchestrate your background jobs. Execute jobs in sequence with Chains, or in parallel as a Batch with completion callbacks.

Job Chains

Job chaining allows you to specify a list of queued jobs that should be run in sequence locally or distributed. If one job in the sequence fails, the rest of the jobs will not be run.

controller.ts
1import { Bus } from 'canxjs';
2import { OptimizeImage } from './jobs/OptimizeImage';
3import { SendNotification } from './jobs/SendNotification';
4
5// Run jobs in sequence. If one fails, the rest are not run.
6await Bus.chain([
7 new OptimizeImage(imageId),
8 new SendNotification(userId),
9]).dispatch();

Job Batches

Job batching allows you to execute a batch of jobs and then perform some action when the batch has completed executing.

service.ts
1import { Bus } from 'canxjs';
2
3// Run jobs in parallel and track completion
4const batch = await Bus.batch([
5 new ProcessImport(file1),
6 new ProcessImport(file2),
7 new ProcessImport(file3),
8])
9.then(() => {
10 // Callback when all jobs complete successfully
11 console.log('Batch finished!');
12})
13.catch(() => {
14 // Callback if a job fails
15 console.log('Batch failed!');
16})
17.finally(() => {
18 // Always run
19})
20.dispatch();
21
22console.log(`Batch ID: ${batch.id}`);

Interacting with Batches

Your job classes should use the Batchable trait (mixin) to interact with the batch they belong to.

jobs/ProcessImport.ts
1import { Batchable, Dispatchable, InteractsWithBatch } from 'canxjs';
2
3export class ProcessImport implements Dispatchable, Batchable {
4 use(InteractsWithBatch);
5
6 async handle() {
7 if (this.batch().cancelled()) {
8 return;
9 }
10
11 // Process...
12 }
13}

Queue Basics