[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) <rde@odoo.com>
This commit is contained in:
Loan (LSE)
2023-04-27 00:52:15 +02:00
committed by Romain Derie
parent 2c5096823b
commit a71bfd5306
+12 -2
View File
@@ -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