Chunk-Oriented Script Module
You can define what a custom step does using a chunk-oriented CommonJS script module that reads and processes items in chunks of size S. If the list contains more items than can be processed in one chunk, a new chunk is started. A chunk-oriented script can include any series of processing steps, not just database transactions. A job step that uses a chunk-oriented script allows fine-grained progress monitoring because the number of elements that are written is updated each time a chunk is finished.
Chunk Processing Example
In this example, a list of 15 orders is exported to a file. Each chunk includes four orders.
Chunk | Steps |
---|---|
Chunk 1 |
|
Chunk 2 |
|
Chunk 3 |
|
Chunk 4 |
This chunk read, processed, and wrote fewer items than the previous chunks, because only three items were left on the list. |
Function | Required or Optional | Purpose |
---|---|---|
read-function | Required | Returns one item or nothing if there are no more items. |
process-function | Required | Transforms items and applies business logic to them. It receives the item returned by the read function, performs a process, and returns one item.The item returned can be the same item that the read function returned if no processing logic is necessary, or it can be a new item of a different type. If the process function returns nothing, then the read function item is filtered and doesn't appear in the list of items to be written later. |
write-function | Required | Receives a list of items. The list size matches the chunk size or, if the number of items in the last available chunk is smaller, it is smaller. The write function returns nothing. |
total-count-function | Optional | Returns the total number of items that are available. Called by the framework exactly once before chunk processing begins. A known total count allows better monitoring, for example, to show that 50 of 100 items have already been processed. |
before-step-function | Optional | Executed before a chunk step begins. Implements logic before all items of all chunks are read, processed, and written. |
before-chunk-function | Optional | Executed before a chunk begins. Implements logic before a chunk of S items is read, processed, and written. |
after-chunk-function | Optional | Executed after a chunk finishes. Implements logic after a chunk of S items has been read, processed, and written successfully. |
after-step-function | Optional | Executed after a chunk step finished successfully. Implements logic after all items of all chunks are read, processed, and written successfully. |
dw.job.JobStepExecution
object. The
dw.job.JobStepExecution
object allows read-only access to information about
the current step execution and job execution. You can't define the exit status for a
chunk-oriented script module. Chunk modules always finish with either OK or ERROR. The following example shows a chunk-oriented CommonJS module.
var system = require( 'dw/system' );
var catalog = require( 'dw/catalog' );
var job = require( 'dw/job' );
var io = require( 'dw/io' );
var products;
var fileWriter;
exports.beforeStep = function( parameters, stepExecution )
{
fileWriter = new io.FileWriter( new io.File( io.File.IMPEX ), parameters.ExportFileName );
products = catalog.ProductMgr.queryAllSiteProducts();
fileWriter.writeLine( "<products>" );
}
exports.getTotalCount = function( parameters, stepExecution )
{
return products.count;
}
exports.read = function( parameters, stepExecution )
{
if( products.hasNext() )
{
return products.next();
}
}
exports.process = function( product, parameters, stepExecution )
{
if( product.isOnline() )
{
return "<product>" + product.getID() + "</product>";
}
}
exports.write = function( lines, parameters, stepExecution )
{
for ( i = 0; i < lines.size(); i++ )
{
fileWriter.writeLine( lines.get(i) );
}
}
exports.afterStep = function( success, parameters, stepExecution )
{
if( success )
{
fileWriter.writeLine( "</products>" );
}
fileWriter.close();
products.close();