Skip to content

Commit

Permalink
Housekeeping Settings (#3821)
Browse files Browse the repository at this point in the history
* Add new settings for controlling how long logged data is retained

* Update existing tasks to use new user-configurable values

- Also add a task to delete failed task logs

* Add background task to remove old notification logs
  • Loading branch information
SchrodingersGat authored Oct 20, 2022
1 parent c3f6b75 commit 4ca2aa6
Show file tree
Hide file tree
Showing 3 changed files with 110 additions and 12 deletions.
85 changes: 73 additions & 12 deletions InvenTree/InvenTree/tasks.py
Original file line number Diff line number Diff line change
Expand Up @@ -226,22 +226,51 @@ def heartbeat():

@scheduled_task(ScheduledTask.DAILY)
def delete_successful_tasks():
"""Delete successful task logs which are more than a month old."""
"""Delete successful task logs which are older than a specified period"""
try:
from django_q.models import Success

from common.models import InvenTreeSetting

days = InvenTreeSetting.get_setting('INVENTREE_DELETE_TASKS_DAYS', 30)
threshold = timezone.now() - timedelta(days=days)

# Delete successful tasks
results = Success.objects.filter(
started__lte=threshold
)

if results.count() > 0:
logger.info(f"Deleting {results.count()} successful task records")
results.delete()

except AppRegistryNotReady: # pragma: no cover
logger.info("Could not perform 'delete_successful_tasks' - App registry not ready")
return

threshold = timezone.now() - timedelta(days=30)

results = Success.objects.filter(
started__lte=threshold
)
@scheduled_task(ScheduledTask.DAILY)
def delete_failed_tasks():
"""Delete failed task logs which are older than a specified period"""

try:
from django_q.models import Failure

from common.models import InvenTreeSetting

if results.count() > 0:
logger.info(f"Deleting {results.count()} successful task records")
results.delete()
days = InvenTreeSetting.get_setting('INVENTREE_DELETE_TASKS_DAYS', 30)
threshold = timezone.now() - timedelta(days=days)

# Delete failed tasks
results = Failure.objects.filter(
started__lte=threshold
)

if results.count() > 0:
logger.info(f"Deleting {results.count()} failed task records")
results.delete()

except AppRegistryNotReady: # pragma: no cover
logger.info("Could not perform 'delete_failed_tasks' - App registry not ready")


@scheduled_task(ScheduledTask.DAILY)
Expand All @@ -250,8 +279,10 @@ def delete_old_error_logs():
try:
from error_report.models import Error

# Delete any error logs more than 30 days old
threshold = timezone.now() - timedelta(days=30)
from common.models import InvenTreeSetting

days = InvenTreeSetting.get_setting('INVENTREE_DELETE_ERRORS_DAYS', 30)
threshold = timezone.now() - timedelta(days=days)

errors = Error.objects.filter(
when__lte=threshold,
Expand All @@ -264,7 +295,37 @@ def delete_old_error_logs():
except AppRegistryNotReady: # pragma: no cover
# Apps not yet loaded
logger.info("Could not perform 'delete_old_error_logs' - App registry not ready")
return


@scheduled_task(ScheduledTask.DAILY)
def delete_old_notifications():
"""Delete old notification logs"""

try:
from common.models import (InvenTreeSetting, NotificationEntry,
NotificationMessage)

days = InvenTreeSetting.get_setting('INVENTREE_DELETE_NOTIFICATIONS_DAYS', 30)
threshold = timezone.now() - timedelta(days=days)

items = NotificationEntry.objects.filter(
updated__lte=threshold
)

if items.count() > 0:
logger.info(f"Deleted {items.count()} old notification entries")
items.delete()

items = NotificationMessage.objects.filter(
creation__lte=threshold
)

if items.count() > 0:
logger.info(f"Deleted {items.count()} old notification messages")
items.delete()

except AppRegistryNotReady:
logger.info("Could not perform 'delete_old_notifications' - App registry not ready")


@scheduled_task(ScheduledTask.DAILY)
Expand Down
33 changes: 33 additions & 0 deletions InvenTree/common/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -893,6 +893,39 @@ def save(self, *args, **kwargs):
'default': True,
},

'INVENTREE_DELETE_TASKS_DAYS': {
'name': _('Delete Old Tasks'),
'description': _('Background task results will be deleted after specified number of days'),
'default': 30,
'units': 'days',
'validator': [
int,
MinValueValidator(7),
]
},

'INVENTREE_DELETE_ERRORS_DAYS': {
'name': _('Delete Error Logs'),
'description': _('Error logs will be deleted after specified number of days'),
'default': 30,
'units': 'days',
'validator': [
int,
MinValueValidator(7)
]
},

'INVENTREE_DELETE_NOTIFICATIONS_DAYS': {
'name': _('Delete Noficiations'),
'description': _('User notifications will be deleted after specified number of days'),
'default': 30,
'units': 'days',
'validator': [
int,
MinValueValidator(7),
]
},

'BARCODE_ENABLE': {
'name': _('Barcode Support'),
'description': _('Enable barcode scanner support'),
Expand Down
4 changes: 4 additions & 0 deletions InvenTree/templates/InvenTree/settings/global.html
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,10 @@
{% include "InvenTree/settings/setting.html" with key="INVENTREE_REQUIRE_CONFIRM" icon="fa-check" %}
{% include "InvenTree/settings/setting.html" with key="INVENTREE_TREE_DEPTH" icon="fa-sitemap" %}
{% include "InvenTree/settings/setting.html" with key="INVENTREE_BACKUP_ENABLE" icon="fa-hdd" %}
<tr><td colspan='5'></td></tr>
{% include "InvenTree/settings/setting.html" with key="INVENTREE_DELETE_TASKS_DAYS" icon="fa-calendar-alt" %}
{% include "InvenTree/settings/setting.html" with key="INVENTREE_DELETE_ERRORS_DAYS" icon="fa-calendar-alt" %}
{% include "InvenTree/settings/setting.html" with key="INVENTREE_DELETE_NOTIFICATIONS_DAYS" icon="fa-calendar-alt" %}
</tbody>
</table>

Expand Down

0 comments on commit 4ca2aa6

Please sign in to comment.