From d22d382664b2a1b786cb1c31735b240a47d9e264 Mon Sep 17 00:00:00 2001 From: antonag32 Date: Mon, 22 Jan 2024 10:34:09 -0600 Subject: [PATCH] [REF] website: customizable batch/limit on _cron_unlink_old_visitors In b69917e[1] the cron implementation was changed to unlink old visitors in batches of 1000 records. This was meant to deal with memory/timeout errors when dealing with large amounts of records. However it still searches for records with no limit, which in high record count scenarios and based on instance resources may still generate memory/timeout errors. Technically it could be considered "fine" for the cron to timeout since every batch is committed, so previously unlinked records are not rolled back and the cron should eventually delete them all. However there are some edge cases where memory/time out errors would not be fine, like the cron failing during the first batch, which means no unlink operations would be committed to the database. Errors that are "fine" also generate noise and leave administrators wondering which errors they should ignore and which they should not. It also alarms non-technical customers since after all, they are seeing a reported error. Therefore the search limit and batch size have been added as arguments to the cron. This is completely opt-in since they have the previous values as their defaults. This makes it easy to customize and tune the performance of the job accordingly if required. [1] https://github.com/odoo/odoo/commit/b69917ec0e508f8354d831525c5c48ee79b5967a X-original-commit: e8a4b1f6a239753f66aa827e8e0d67ca4270dba3 Part-of: odoo/odoo#153707 --- addons/website/models/website_visitor.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/addons/website/models/website_visitor.py b/addons/website/models/website_visitor.py index ffb1d58c621..9ddd1649ebf 100644 --- a/addons/website/models/website_visitor.py +++ b/addons/website/models/website_visitor.py @@ -330,7 +330,7 @@ class WebsiteVisitor(models.Model): self.website_track_ids.visitor_id = target.id self.unlink() - def _cron_unlink_old_visitors(self): + def _cron_unlink_old_visitors(self, batch_size=1000, limit=None): """ Unlink inactive visitors (see '_inactive_visitors_domain' for details). @@ -340,8 +340,8 @@ class WebsiteVisitor(models.Model): auto_commit = not getattr(threading.current_thread(), 'testing', False) visitor_model = self.env['website.visitor'] for inactive_visitors_batch in split_every( - 1000, - visitor_model.sudo().search(self._inactive_visitors_domain()).ids, + batch_size, + visitor_model.sudo().search(self._inactive_visitors_domain(), limit=limit).ids, visitor_model.browse, ): inactive_visitors_batch.unlink()