diff --git a/aircox/management/commands/archiver.py b/aircox/management/commands/archiver.py new file mode 100644 index 0000000..9ab545e --- /dev/null +++ b/aircox/management/commands/archiver.py @@ -0,0 +1,57 @@ +""" +Handle archiving of logs in order to keep database light and fast. The +logs are archived in gzip files, per day. +""" +import logging +from argparse import RawTextHelpFormatter + +from django.conf import settings as main_settings +from django.core.management.base import BaseCommand, CommandError +from django.utils import timezone as tz + +import aircox.settings as settings +from aircox.models import Log, Station + +logger = logging.getLogger('aircox.tools') + + +class Command (BaseCommand): + help= __doc__ + + def add_arguments (self, parser): + parser.formatter_class=RawTextHelpFormatter + group = parser.add_argument_group('actions') + group.add_argument( + '-a', '--age', type=int, + default = settings.AIRCOX_LOGS_ARCHIVES_MIN_AGE, + help='minimal age in days of logs to archive. Default is ' + 'settings.AIRCOX_LOGS_ARCHIVES_MIN_AGE' + ) + group.add_argument( + '-f', '--force', action='store_true', + help='if an archive exists yet, force it to be updated' + ) + group.add_argument( + '-k', '--keep', action='store_true', + help='keep logs in database instead of deleting them' + ) + + def handle (self, *args, age, force, keep, **options): + date = tz.now() - tz.timedelta(days = age) + + while True: + date = date.replace( + hour = 0, minute = 0, second = 0, microsecond = 0 + ) + + logger.info('archive log at date %s', date) + for station in Station.objects.all(): + Log.objects.make_archive( + station, date, force = force, keep = keep + ) + + qs = Log.objects.filter(date__lt = date) + if not qs.exists(): + break + date = qs.order_by('-date').first().date + diff --git a/aircox/settings.py b/aircox/settings.py index 4716b52..1382f60 100755 --- a/aircox/settings.py +++ b/aircox/settings.py @@ -20,9 +20,9 @@ ensure('AIRCOX_DEFAULT_USER_GROUPS', { 'add_comment', 'edit_comment', 'delete_comment', ), # ensure user can log in using Wagtail - 'Editors': None + 'Editors': None, # ensure user can publish - 'Moderators': None + 'Moderators': None, }) # Directory for the programs data diff --git a/scripts/cron_diffusions b/scripts/cron_diffusions index 49765cd..567e6e3 100755 --- a/scripts/cron_diffusions +++ b/scripts/cron_diffusions @@ -8,3 +8,8 @@ scripts/launch_in_venv ./manage.py diffusions --update --clean --check --mode au scripts/launch_in_venv ./manage.py diffusions --update --next-month --mode auto cd - +# - archiver monitoring for the next month +scripts/launch_in_venv ./manage.py archiver -a 60 +cd - + +