Best practices for writing activities for background jobs
Ensure that work items are processed correctly and that data is not corrupted during a node shutdown by using best practices for writing activities for background jobs.
Queue processors, job schedulers, and agents are background jobs that use an activity to implement processing logic. These activities are expected to finish processing in less than a few seconds. If an activity takes longer to complete, use the following best practices to ensure that work items are not affected during the shutdown process.
Create small work items
If possible, break down large work items into smaller work items, queue them to a queue processor, and process them individually. Use a queue processor instead of an agent because queue processors provide better performance, resiliency, fault tolerance, and flexibility than agents. Scale queue processors horizontally by increasing the number of nodes configured with the node type. Scale queue processors vertically by increasing the number of threads. For more information, see Managing queue processors in Admin Studio help.
To ensure that activity logic can gracefully handle restarts without corrupting data or introducing inconsistencies into your application, use checkpointing by saving the state of jobs in persistent storage or as messages in a queue that is appropriate to the use case.
For example, consider a scenario where a work object can have the following statuses: NEW, IN-PROGRESS, and PROCESSED. You can read all of the work object records to be processed, loop through them one by one, mark the status of the current work object as IN-PROGRESS, continue with the business logic, and when processing is finished, change the status to PROCESSED.
Divide and scale tasks
If you have background tasks that can be broken down into multiple tasks that have different performance capabilities, consider dividing them and scaling each independently. Queue processors can be scaled horizontally by increasing the number of relevant nodes and can be scaled vertically by increasing the number of threads. For more information, see Managing queue processors in Admin Studio help.
Split activities into sub-activities when possible so that the state is saved frequently, ensuring that if there is an abrupt node shutdown, the sub-activities can proceed based on the recovered state.
For more information about agents, job schedulers, and queue processors, see the Admin Studio help.