Surviving Odoo's 120 Second Cron Limit
This article explores practical strategies to handle long-running jobs in Odoo, including breaking down tasks, using intermediate states, and leveraging external tools to overcome the 120-second cron limit.

Odoo's cron jobs have a default timeout of 120 seconds, which can be a significant limitation for long-running tasks. This timeout, controlled by the limit_time_real_cron setting in the Odoo configuration file, can cause critical processes to fail, leading to issues like incomplete database backups and failed mass mailings. For instance, a backup job that continuously loops and fills up the disk, or a scheduled mailing task that gets repeatedly interrupted, both highlight the need for a solution to handle these long-running jobs effectively.
In this article, we will explore how to design and implement resumable long jobs in Odoo to overcome the 120-second cron limit. We will discuss practical strategies, including breaking down tasks, using intermediate states, and leveraging external tools, to ensure your jobs complete successfully without running afoul of the timeout.
Understanding the Timeout
The 120-second timeout for cron jobs in Odoo is a safety mechanism to prevent workers from consuming too many resources. This limit is defined in the odoo.conf file under the limit_time_real_cron setting. Despite increasing other timeout settings, this particular limit remains a common pain point for many users.
For example, the error message in the logs shows that a cron worker was killed after 120 seconds:
2019-12-02 06:43:04,711 4493 ERROR ? odoo.service.server: WorkerCron (4518) timeout after 120s
This issue can lead to incomplete tasks and repeated failures, which can be especially problematic for critical operations like database backups and mass mailings.
Breaking Down Tasks
One effective strategy to handle long-running jobs is to break them into smaller, manageable chunks. For instance, if you are processing a large number of records, you can divide the task into smaller batches. This approach ensures that each batch completes within the 120-second limit, allowing the entire job to progress without interruption.
Here’s an example of how you can implement batch processing in a Python function:
def process_large_dataset(self, dataset, batch_size=1000):
for i in range(0, len(dataset), batch_size):
batch = dataset[i:i + batch_size]
self.process_batch(batch)
In this example, the process_large_dataset function processes the dataset in batches of 1000 records at a time. You can adjust the batch_size based on the specific requirements and performance characteristics of your system.
Using Intermediate States
Another approach is to use intermediate states to track the progress of a job. This method involves saving the state of the job between runs, allowing it to resume where it left off. This is particularly useful for tasks that involve external systems or large datasets that cannot be easily divided.
For example, you can create a model to store the state of a long-running job:
class LongRunningJob(models.Model):
_name = 'long.running.job'
_description = 'Long Running Job'
name = fields.Char(required=True)
state = fields.Selection([
('new', 'New'),
('in_progress', 'In Progress'),
('completed', 'Completed')
], default='new')
progress = fields.Integer(default=0)
During each cron job run, you can update the state and progress of the job:
def process_long_running_job(self):
job = self.env['long.running.job'].search([('state', '=', 'in_progress')], limit=1)
if job:
# Perform a segment of the job
job.progress += 10
if job.progress >= 100:
job.state = 'completed'
else:
job.state = 'in_progress'
This way, even if the job is interrupted, it can resume from the last saved state in the next cron run.
Leveraging External Tools
For more complex or resource-intensive tasks, you might consider using external tools or services. Tools like Celery, a distributed task queue, can help manage long-running jobs more efficiently. Celery allows you to offload tasks to a separate worker process, which can run independently of the Odoo cron worker.
Here’s a basic example of how you can set up a Celery task:
from celery import Celery
app = Celery('tasks', broker='pyamqp://guest@localhost//')
@app.task
def long_running_task():
# Your long-running task logic here
pass
You can then trigger these tasks from within Odoo using a cron job:
def trigger_long_running_task(self):
long_running_task.delay()
By offloading the task to Celery, you can avoid the 120-second timeout and ensure that your jobs complete successfully.
Monitoring and Logging
Effective monitoring and logging are crucial for diagnosing and resolving issues with long-running jobs. Ensure that your jobs log progress and any errors they encounter. This will help you identify where the job is failing and make necessary adjustments.
For example, you can add logging statements to your job:
import logging
_logger = logging.getLogger(__name__)
def process_long_running_job(self):
job = self.env['long.running.job'].search([('state', '=', 'in_progress')], limit=1)
if job:
_logger.info(f'Starting job: {job.name} at progress: {job.progress}')
# Perform a segment of the job
job.progress += 10
if job.progress >= 100:
job.state = 'completed'
_logger.info(f'Completed job: {job.name}')
else:
job.state = 'in_progress'
_logger.info(f'Job {job.name} progressed to: {job.progress}')
Regularly review the logs to ensure that your jobs are running as expected and address any issues promptly.
Where This Approach Does Not Fit
While the strategies discussed in this article can help you manage and complete long-running jobs within Odoo’s 120-second cron limit, they are not a one-size-fits-all solution. For example, tasks that require real-time or near-real-time processing might not benefit from these approaches. Additionally, highly resource-intensive tasks that cannot be easily broken down or moved to an external system may still face challenges.
It’s essential to evaluate the specific requirements and constraints of your jobs and choose the most appropriate approach. In some cases, you might need to consider more advanced solutions, such as distributed computing frameworks or serverless architectures, to handle extremely large or complex tasks.
Always monitor the performance and resource usage of your jobs to ensure that they are running efficiently and do not impact the overall stability and performance of your Odoo instance.
This article draws on Odoo timeout killing cron and an answer to it, on the Stack Exchange network, licensed under CC BY-SA 4.0.
- #limit_time_real_cron
- #odoo cron timeout



Comments