[FIX] auditlog: Allow passing a chunk size for autovacuum

pull/2528/head
Holger Brunn 2022-05-10 13:00:13 +02:00 committed by Stefan Rijnhart
parent 491b70de64
commit ff27aa473c
4 changed files with 18 additions and 4 deletions

View File

@ -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 .. 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 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 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 individual records through the `View Logs` action. The second group is the

View File

@ -13,7 +13,7 @@ class AuditlogAutovacuum(models.TransientModel):
_description = "Auditlog - Delete old logs" _description = "Auditlog - Delete old logs"
@api.model @api.model
def autovacuum(self, days): def autovacuum(self, days, chunk_size=None):
"""Delete all logs older than ``days``. This includes: """Delete all logs older than ``days``. This includes:
- CRUD logs (create, read, write, unlink) - CRUD logs (create, read, write, unlink)
- HTTP requests - HTTP requests
@ -26,9 +26,12 @@ class AuditlogAutovacuum(models.TransientModel):
data_models = ("auditlog.log", "auditlog.http.request", "auditlog.http.session") data_models = ("auditlog.log", "auditlog.http.request", "auditlog.http.session")
for data_model in data_models: for data_model in data_models:
records = self.env[data_model].search( 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) nb_records = len(records)
records.unlink() with self.env.norecompute():
records.unlink()
_logger.info("AUTOVACUUM - %s '%s' records deleted", nb_records, data_model) _logger.info("AUTOVACUUM - %s '%s' records deleted", nb_records, data_model)
return True return True

View File

@ -20,6 +20,10 @@ To activate it and/or change the delay, go to the
.. image:: ../static/description/autovacuum.png .. 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 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 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 individual records through the `View Logs` action. The second group is the

View File

@ -3,7 +3,7 @@
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en"> <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head> <head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<meta name="generator" content="Docutils 0.15.1: http://docutils.sourceforge.net/" /> <meta name="generator" content="Docutils: http://docutils.sourceforge.net/" />
<title>Audit Log</title> <title>Audit Log</title>
<style type="text/css"> <style type="text/css">
@ -401,6 +401,9 @@ To activate it and/or change the delay, go to the
<cite>Configuration / Technical / Automation / Scheduled Actions</cite> menu and edit the <cite>Configuration / Technical / Automation / Scheduled Actions</cite> menu and edit the
<cite>Auto-vacuum audit logs</cite> entry:</p> <cite>Auto-vacuum audit logs</cite> entry:</p>
<img alt="https://raw.githubusercontent.com/OCA/server-tools/16.0/auditlog/static/description/autovacuum.png" src="https://raw.githubusercontent.com/OCA/server-tools/16.0/auditlog/static/description/autovacuum.png" /> <img alt="https://raw.githubusercontent.com/OCA/server-tools/16.0/auditlog/static/description/autovacuum.png" src="https://raw.githubusercontent.com/OCA/server-tools/16.0/auditlog/static/description/autovacuum.png" />
<p>In case youre 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.</p>
<p>There are two possible groups configured to which one may belong. The first <p>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 is the Auditlog User group. This group has read-only access to the auditlogs of
individual records through the <cite>View Logs</cite> action. The second group is the individual records through the <cite>View Logs</cite> action. The second group is the