BoxLang 🚀 A New JVM Dynamic Language Learn More...
Automatic job batching for ColdBox cbq. When a job exceeds a configurable item threshold, it automatically splits the workload into smaller parallel batches while preserving chained job execution.
box install cbq-autobatch
Inject the AutoBatch service and configure via props:
component extends="cbq.models.Jobs.AbstractJob" {
property name="autoBatch" inject="AutoBatch@cbqAutoBatch";
function handle() {
var props = getProperties();
// Batch configuration - all in props
param props.autoBatch = true;
param props.batchSize = 20;
param props.batchItemsKey = "accounts"; // key containing struct to chunk
param props.batchIdKey = "accountIDs"; // key to populate with chunk keys
// ... resolve your items ...
props.accounts = loadAccounts();
// Check for auto-batching - just pass this and props
var result = autoBatch.evaluate( this, props );
if ( result.batched ) {
return result.result;
}
// Continue with single-job execution for items under threshold
processItems( props.accounts );
}
}
All configuration lives in props (with sensible defaults):
| Prop | Default | Description |
|---|---|---|
autoBatch
| false
| Enable auto-batching |
batchSize
| 10
| Items per batch |
batchQueue
| "default"
| Queue/connection for batch jobs |
batchItemsKey
| "items"
| Key in props containing struct to chunk |
batchIdKey
| ""
| Key to populate with chunk keys (optional) |
batchMaxAttempts
| 2
| Retry attempts per batch job |
batchBackoff
| 60
| Seconds between retries |
batchTimeout
| 2400
| Job timeout in seconds |
batchAllowFailures
| true
| Continue batch if some jobs fail |
batchFinally
| - | Job/chain to append after chained jobs complete |
batchCarryover
| []
| Props to pass to children; if empty, passes ALL props |
| Prop | Value | Description |
|---|---|---|
bBatchChild
| true
| Flags this job as a batch child |
batchIndex
| 1-N
| 1-based index of this chunk |
batchTotal
| N
| Total number of chunks |
Use batchFinally in props to append a job after all
chained jobs:
// Single job object
props.batchFinally = cbq.job( "notification", { message : "All done!" } );
// Struct definition (converted to job)
props.batchFinally = {
job : "reports.cleanup",
properties : { bForce : true }
};
// Array of jobs
props.batchFinally = [
cbq.job( "reports.summary", {} ),
cbq.job( "notification", { message : "Reports complete" } )
];
autoBatch is
enabled and items exceed batchSize, batching kicks inbatchSize
bBatchChild = true to flag as a batch childbatchIndex and batchTotal for progress trackingautoBatch = false to prevent infinite recursionfinally() callbackbatchFinally is
appended after existing chainsConfigure defaults in your config/ColdBox.cfc:
moduleSettings = {
cbqAutoBatch : {
defaultBatchSize : 10,
defaultBatchQueue : "default",
defaultMaxAttempts : 2,
defaultBackoff : 60,
defaultTimeout : 2400,
defaultAllowFailures : true
}
};
Batched jobs receive batchIndex and
batchTotal in their props:
function before() {
var props = getProperties();
if ( props.keyExists( "batchIndex" ) ) {
notify( "Processing batch #props.batchIndex# of #props.batchTotal#" );
}
}
Use bBatchChild to prevent after() from
running on child jobs (run only on parent/finally):
function after() {
var props = getProperties();
if ( !( props.bBatchChild ?: false ) ) {
super.after(); // Only run on parent or finally job
}
}
By default (empty batchCarryover), ALL props are passed
to child jobs, substituting the chunked items. Use
batchCarryover to explicitly limit which props are passed:
// Default: all props carried over (recommended for most cases)
var result = autoBatch.evaluate( this, props );
// Explicit: only pass specific props to children
props.batchCarryover = [ "sessionGUID", "parentLogID", "bDebug" ];
var result = autoBatch.evaluate( this, props );
MIT
$
box install cbq-autobatch