Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion queue_job/__manifest__.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@


{'name': 'Job Queue',
'version': '10.0.1.0.0',
'version': '10.0.2.0.0',
'author': 'Camptocamp,ACSONE SA/NV,Odoo Community Association (OCA)',
'website': 'https://github.com/OCA/queue',
'license': 'AGPL-3',
Expand Down
31 changes: 25 additions & 6 deletions queue_job/models/queue_job.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@
import logging
from datetime import datetime, timedelta

from odoo import models, fields, api, exceptions, _
from odoo import models, fields, api, exceptions, registry, _
from psycopg2 import OperationalError

from ..job import STATES, DONE, PENDING, Job
from ..fields import JobSerialized
Expand Down Expand Up @@ -220,16 +221,34 @@ def _needaction_domain_get(self):
return [('state', '=', 'failed')]

@api.model
def autovacuum(self):
def autovacuum(self, jobs_set=1000, use_new_cursor=False):
""" Delete all jobs done since more than ``_removal_interval`` days.

Uses sets of jobs to ensure that no timeout occurs
Called from a cron.
"""
job_obj = self.env['queue.job']
deadline = datetime.now() - timedelta(days=self._removal_interval)
jobs = self.search(
jobs_ids = self.search(
[('date_done', '<=', fields.Datetime.to_string(deadline))],
)
jobs.unlink()
).ids

while jobs_ids:
if use_new_cursor:
cr = registry(self._cr.dbname).cursor()
self = self.with_env(self.env(cr=cr))
try:
jobs = job_obj.browse(jobs_ids[:jobs_set])
jobs_ids = jobs_ids[jobs_set:]
jobs.unlink()
if use_new_cursor:
cr.commit()
except OperationalError:
if use_new_cursor:
cr.rollback()
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@rousseldenis IMO all the logic should be enclosed into a try except finally block

    if use_new_cursor:
        cr = registry(self._cr.dbname).cursor()
    try:
        self = self.with_env(self.env(cr=cr))
        jobs = job_obj.browse(jobs_ids[:jobs_set])
        jobs_ids = jobs_ids[jobs_set:]
        jobs.unlink()
        if use_new_cursor:
            cr.commit()
    except OperationalError:
         if use_new_cursor:
            cr.rollback()
            raise
    finally:
        if use_new_cursor:
            cr.close()

raise
finally:
if use_new_cursor:
cr.close()
return True

@api.multi
Expand Down