From a71bfd53068f47f9c0d64b90d3d37830dcc4c195 Mon Sep 17 00:00:00 2001 From: "Loan (LSE)" Date: Mon, 24 Apr 2023 09:31:10 +0000 Subject: [PATCH] [FIX] website: delete visitors by batch in cron Before this commit: The scheduled action will repeatedly time out if the number of visitors to remove is too high. In the case of the client, he has ~ 4 millions inactive visitors to remove. This might happen after a bot spam attack. After this commit: The visitors are removed by batch of 1000, reducing the number of old visitors with time opw-3256349 closes odoo/odoo#119757 X-original-commit: b69917ec0e508f8354d831525c5c48ee79b5967a Signed-off-by: Romain Derie (rde) --- addons/website/models/website_visitor.py | 14 ++++++++++++-- 1 file changed, 12 insertions(+), 2 deletions(-) diff --git a/addons/website/models/website_visitor.py b/addons/website/models/website_visitor.py index 8ba9a3c5f42..e8c2b9fdb93 100644 --- a/addons/website/models/website_visitor.py +++ b/addons/website/models/website_visitor.py @@ -6,10 +6,12 @@ from psycopg2 import sql import hashlib import pytz +import threading from odoo import fields, models, api, _ from odoo.addons.base.models.res_partner import _tz_get from odoo.exceptions import UserError +from odoo.tools import split_every from odoo.tools.misc import _format_time_ago from odoo.http import request from odoo.osv import expression @@ -338,8 +340,16 @@ class WebsiteVisitor(models.Model): Visitors were previously archived but we came to the conclusion that archived visitors have very little value and bloat the database for no reason. """ - - self.env['website.visitor'].sudo().search(self._inactive_visitors_domain()).unlink() + 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, + visitor_model.browse, + ): + inactive_visitors_batch.unlink() + if auto_commit: + self.env.cr.commit() def _inactive_visitors_domain(self): """ This method defines the domain of visitors that can be cleaned. By