queue icon indicating copy to clipboard operation
queue copied to clipboard

Automatically reset "Started/Enquired" jobs to "Pedning" on Odoo Start

Open ventor-dev opened this issue 4 years ago • 7 comments

Problem

When the Odoo server crashes or is otherwise force-stopped, running jobs are interrupted while the runner has no chance to know they have been aborted. In such situations, jobs may remain in started or enqueued state after the Odoo server is halted. Since the runner has no way to know if they are actually running or not, and does not know for sure if it is safe to restart the jobs, it does not attempt to restart them automatically. Such stale jobs therefore fill the running queue and prevent other jobs to start. You must therefore requeue them manually, either from the Jobs view, or by running the following SQL statement before starting Odoo: update queue_job set state='pending' where state in ('started', 'enqueued')

Result of this - channel is lost as system is thinking that job is started, while it is not really doing anything

Solution

This problem exists since beginning of time (means from the beginning of queue_job). So I guess lot's of brilliant brains were considering different solutions. But I was thinking that problem sounds not so hard so be solved with below approach.

  1. Currently we are running Job Runner Thread on Odoo Startup using this patch https://github.com/OCA/queue/blob/14.0/queue_job/jobrunner/init.py#L69

  2. Than we will call initialize_database() method before we are starting processing jobs https://github.com/OCA/queue/blob/14.0/queue_job/jobrunner/runner.py#L501

  3. And here we already have possibility to connect to the database. https://github.com/OCA/queue/blob/14.0/queue_job/jobrunner/runner.py#L414

So I suggest method that will run this script below before. We of course can make "SELECT FOR UPDATE" to be on the safe side and do not conflict with other processes that may query for the same records.

update queue_job set state='pending' where state in ('started', 'enqueued')

TO me this fix sounds safe.

But I believe @guewen you was considering this already. So before suggesting PR, maybe you see issues with above method?

ventor-dev avatar Nov 03 '21 12:11 ventor-dev

This kind of exists already. It's not just connected to Odoo starting up, but it's executed by the cron.

https://github.com/OCA/queue/blob/92eae4b427578068c5d9256d51bf672938bc9422/queue_job/data/queue_data.xml#L4-L12

https://github.com/OCA/queue/blob/92eae4b427578068c5d9256d51bf672938bc9422/queue_job/models/queue_job.py#L269-L282

mlaitinen avatar Dec 02 '21 14:12 mlaitinen

There hasn't been any activity on this issue in the past 6 months, so it has been marked as stale and it will be closed automatically if no further activity occurs in the next 30 days. If you want this issue to never become stale, please ask a PSC member to apply the "no stale" label.

github-actions[bot] avatar Jun 05 '22 12:06 github-actions[bot]

A proposed implementation is in https://github.com/OCA/queue/pull/423

sbidoul avatar Jul 01 '22 13:07 sbidoul

Hi, where I work, we have the same problem. I've uploaded a pull request with the changes we've that solves this problem. I hope it can be as useful to you as it has been to us.

angelvilaplana avatar Sep 04 '23 10:09 angelvilaplana

@angelvilaplana thanks for your suggestion, that looks interesting

@mlaitinen it is not really working. I'm not sure why. But this cron never do what it should do (cleanup stuck jobs)

ventor-dev avatar Sep 04 '23 14:09 ventor-dev

@ventor-dev You might have to tweak the cron method call arguments.

def requeue_stuck_jobs(self, enqueued_delta=5, started_delta=0):

The cron job calls this method without arguments, which means that by default the job only deals with enqueued jobs, not started. In my experience more jobs are stuck in the "started" state, so just changing the cron call to

model.requeue_stuck_jobs(5, 15)

will take care of jobs stuck in the started state for more than 15 minutes.

mlaitinen avatar Sep 05 '23 11:09 mlaitinen

@mlaitinen thanks!

ventor-dev avatar Sep 06 '23 16:09 ventor-dev