From ff27aa473c8fb59c50d3f34341fbc9185df2b2ab Mon Sep 17 00:00:00 2001 From: Holger Brunn Date: Tue, 10 May 2022 13:00:13 +0200 Subject: [PATCH] [FIX] auditlog: Allow passing a chunk size for autovacuum --- auditlog/README.rst | 4 ++++ auditlog/models/autovacuum.py | 9 ++++++--- auditlog/readme/USAGE.rst | 4 ++++ auditlog/static/description/index.html | 5 ++++- 4 files changed, 18 insertions(+), 4 deletions(-) diff --git a/auditlog/README.rst b/auditlog/README.rst index e860ba111..66125fa98 100644 --- a/auditlog/README.rst +++ b/auditlog/README.rst @@ -58,6 +58,10 @@ To activate it and/or change the delay, go to the .. image:: https://raw.githubusercontent.com/OCA/server-tools/16.0/auditlog/static/description/autovacuum.png +In case you're having trouble with the amount of records to delete per run, +you can pass the amount of records to delete for one model per run as the second +parameter, the default is to delete all records in one go. + There are two possible groups configured to which one may belong. The first is the Auditlog User group. This group has read-only access to the auditlogs of individual records through the `View Logs` action. The second group is the diff --git a/auditlog/models/autovacuum.py b/auditlog/models/autovacuum.py index 4803a1d6e..bf56fc529 100644 --- a/auditlog/models/autovacuum.py +++ b/auditlog/models/autovacuum.py @@ -13,7 +13,7 @@ class AuditlogAutovacuum(models.TransientModel): _description = "Auditlog - Delete old logs" @api.model - def autovacuum(self, days): + def autovacuum(self, days, chunk_size=None): """Delete all logs older than ``days``. This includes: - CRUD logs (create, read, write, unlink) - HTTP requests @@ -26,9 +26,12 @@ class AuditlogAutovacuum(models.TransientModel): data_models = ("auditlog.log", "auditlog.http.request", "auditlog.http.session") for data_model in data_models: records = self.env[data_model].search( - [("create_date", "<=", fields.Datetime.to_string(deadline))] + [("create_date", "<=", fields.Datetime.to_string(deadline))], + limit=chunk_size, + order="create_date asc", ) nb_records = len(records) - records.unlink() + with self.env.norecompute(): + records.unlink() _logger.info("AUTOVACUUM - %s '%s' records deleted", nb_records, data_model) return True diff --git a/auditlog/readme/USAGE.rst b/auditlog/readme/USAGE.rst index 05c61d882..12bf66c12 100644 --- a/auditlog/readme/USAGE.rst +++ b/auditlog/readme/USAGE.rst @@ -20,6 +20,10 @@ To activate it and/or change the delay, go to the .. image:: ../static/description/autovacuum.png +In case you're having trouble with the amount of records to delete per run, +you can pass the amount of records to delete for one model per run as the second +parameter, the default is to delete all records in one go. + There are two possible groups configured to which one may belong. The first is the Auditlog User group. This group has read-only access to the auditlogs of individual records through the `View Logs` action. The second group is the diff --git a/auditlog/static/description/index.html b/auditlog/static/description/index.html index 0b44b0115..029f935f0 100644 --- a/auditlog/static/description/index.html +++ b/auditlog/static/description/index.html @@ -3,7 +3,7 @@ - + Audit Log