Technical
Introduction
The Batch Manager module enable an easy and powerful way to manage batch processes with thread paralelization, stats, error control and recovery mode out of the box.
The main capabilities are:
- Setup a Batch Job to execute whatever APEX Method you need.
- Set the number of parallel Threads.
- Automated Throttling mechanism to avoid hitting paralell batch limits.
- Customizable Hooks to add extra feature to the Batch Manager.
- Recovery Mode when the batch has not finished correctly.
- Ability to schedule batch executions.
- Out of the Box stats.
The Architecture
The Batch Manager is based both in Apex Classes and Salesforce Objects.
Type | Name | Description |
---|---|---|
Apex Class | BatchExecutor | It’s in charge of the Batch orchestration and execution. It contains the required methods to execute and manage a Batch. |
Apex Class | BatchJob | It handle the execution of a single Batch Job |
Apex Class | BatchJobUtils | It contains an @InvocableAction to allow the execution of a Batch from Flows |
Apex Interface | BatchableJob | It defines the required method that any Job must implements in order to be able to execute it with the Batch Manager |
Object | Batch Job | It represent a Batch Job and it’s where you setup the different options for the Job Execution as the number of threads |
Object | Batch Thread | It groups Batch Requests to be executed in the same Thread. These Threads are automatically created by the BatchExecutor based on the Job configuration and the related Requests |
Object | Batch Request | It contains a single operation definition. In the request you must set the desired parameters to pass to the execution of the Job’s specified BatchableJob |
--- title: Batch Manager Architecture --- erDiagram "BatchExecutor Class" ||--|| "BatchJob Class" : "" "BatchJob Class" ||--|| "Batch Job Object" : "" "Batch Job Object" ||--}| "Batch Thread Object": "" "Batch Job Object" ||--}| "BatchableJob Interface": "" "Batch Thread Object" ||--}| "Batch Request Object": ""
How does it work?
- The first thing you need to execute a Batch it’s a Batch Job record. In this Record you will need to setup different Job options.
- Once you have a Job you need to add as many Batch Requests as you want to the Batch Job.
- In order to execute the Batch you can do it from the Batch Job record Page, from a Flow or through APEX.
- When the Batch Job is executed:
- The number of required threads are calculated based on the Job setup and the number of requests.
- The Threads are created.
- The different requests are spread around all the different Threads.
- The Threads are executed:
- For each request in the thread, the Batch Manager will call the processRequest() method from the specified BatchableJob implementation.
- The Request will be updated with the resulting status, error, execution date, etc.
Creating Jobs and Requests
Both Batch Jobs and Batch Request are Salesforce Objects, so if you want to create a Batch and its related requests you only need to:
- Create a Batch Job record and setup the different options.
- Create the desired Batch Requests Records and link them to the created Batch Job.
Batch Job Fields
Field Name | Type | Required | Example | Description |
---|---|---|---|---|
Name | Autonumber | Y | JOB-000001 | The unique Batch Job identifier |
Batchable Job Class | String | Y | MyBatchableJobImplementation | The name of the BatchableJob implementation to be executed in this job |
Threads | Number | N | 4 | The number of threads to create for this job. If is left blank it will be 1. |
Retry Failed | boolean | N | true | Sets if the batch should reprocess the failed requests. You must rerun manually the Batch again. |
Scheduled Date | Datetime | N | 2023-05-23 11:00:00 | The date-time to execute this Batch Job. Keep in mind that it will be executed as soon as possible but it’s not warrantied to be executed in that exact time. |
Status | Text | Read Only | New | The Batch Job status. It’s updated automatically. |
Start Time | Date | Read Only | 2023-05-23 11:00:00 | The date-time when the execution started. |
End Time | Date | Read Only | 2023-05-23 12:00:00 | The date-time when the execution ended. |
Request | Number | Read Only | 23 | Number of requests related to the Batch Job |
Success | Number | Read Only | 10 | Number of succeeded requests |
Errors | Number | Read Only | 5 | Number of failed requests |
Success Ratio | Percentage | Read Only | 98% | Ratio of Sucess / Error records. |
% Completed | Percentage | Read Only | 98% | Number of requests completed (Success or Failed) |
Batch Request Fields
Field Name | Type | Required | Example | Description |
---|---|---|---|---|
Name | Autonumber | Y | JOB-000001 | The unique Batch Job identifier |
Input | String | Y | {} | The parameters to be passed to the BatchableJob class in JSON format. |
Status | Text | Read Only | New | The Batch Job status. It’s updated automatically. |
Message | Text | Read Only | Success! | The resulting message from the execution. It must be set by the BatachleJob class |
Scheduled Date | Datetime | N | 2023-05-23 11:00:00 | The date-time to execute this request. Keep in mind that it will be executed as soon as possible but it’s not warrantied to be executed in that exact time. |
Start Time | Date | Read Only | 2023-05-23 11:00:00 | The date-time when the request execution started. |
End Time | Date | Read Only | 2023-05-23 12:00:00 | The date-time when the request execution ended. |
Number of Retries | Number | Read Only | 2 | The number of times that the request was executed. |
Creating a Job Implementation
In order to create a Batch Job implementation you need to create a Class that implements the BatchableJob interface. Later, you will need to specify the name of this class in the Batch Job Batchable Job Class field.
You can find detailed documentation in the [BatchableJob Interface Documentation]({{< relref “batchablejobinterface” >}})
Number of Threads
The Batch Manager is based in the Salesforce’s Batch functionality, and each thread is related to one Batch. This means, that the governors limits apply and you can only execute a maximun of 5 batch. If you set the Number of Threads to a higher number they will be added to apex Flex Queue.
Keep in mind that the Apex Flex Queue can only hold up to 100 batches, so the Batch Manager will cap any value higher than 100 to the maximun number of available slots in the Apex Flex Queue.
In the future, the Batch Manager will only execute up to 5 threads, and as soon as one finishes, the BatchExecutor will execute the next pending thread if there is any.