meta data for this page
Cronjobs - Key concepts and pitfalls
The Hybris documentation on this module is very detailed and they isn't much to add to it. Cronjobs are required in every SAP Hybris project, so it is worth to understand them. Here I will describe their use cases and pitfalls.
- Allows the execution of tasks periodically or on-demand (manually triggered, usually by another system)
- Job: The action to execute.
- CronJob: It is the context of the job and contains the configuration required by the job. It stores the status of the job.
- Trigger: It defines how frequently the job is going to be run.
- A cronjob may run in only one node of the cluster.
- A cronjob can be started using ant runcronjob.
- A cronjob may run asynchronously or synchronously.
- The cronjob isn't removed from the database when it completes.
- If you need to prevent two jobs from running simultaneously, you would have to create your own locking mechanism to avoid this situation.
- Each execution of a job generates a log which it is saved in the cronjob.
- To run any action periodically. The import of product, price and categories data is usually implemented using cronjobs which read a directory periodically and import the data from CSV files.
- To allow the customer to start a process manually using Backoffice and to easily log the output of the job. I once created a job to check if all the products had a valid price.
- When the server shutdowns, the thread running the job is killed and finally blocks aren't run. If you implement some locking mechanism for the jobs, this must be take into account.
- When the servers shutdowns, all the jobs are stopped. After the server is restart, all the jobs are started again, if they have an active trigger.
- integration Tests with cronjobs: It isn't possible to run a cronjob inside a transaction because the logger fails to save the log in the database. A workaround is to test the job class using a mock cronjob instance. By doing this, you can use transactions in the integration test.
- After updating to Hybris 5.5.1 the triggers of old cronjobs must be updated so they are work again. Here are the details.
- Sometimes since Hybris 5.5.1 the cronjobs stop being triggered automatically because the trigger task associated with it was wrongly removed by the system. This an easy to fix bug: Just run an Update Running System and Hybris will recreate the missing trigger tasks. You only need to check “Update System”. In Hybris 6.0 the code of the trigger class was changed and probably this bug was corrected.
Good to know
- When the property cronjob.timertask.loadonstartup is false, the triggers with cron expressions aren't started. With it you may turn off the cronjobs temporally when running a heavy process like the import of product data. But it is preferably to add code to your jobs to prevent them from running if the heavy process hasn't finished yet —to have a locking mechanism—. This property can be changed on runtime on HAC.
–Based on Hybris 6.4