diff --git a/sigi/apps/utils/admin.py b/sigi/apps/utils/admin.py index 3130a4d..87558a5 100644 --- a/sigi/apps/utils/admin.py +++ b/sigi/apps/utils/admin.py @@ -66,7 +66,13 @@ class CronjobAdmin(admin.ModelAdmin): "get_schedule", "get_runner", ) - fields = ["job_name", "app_name", "get_help", "expressao_cron"] + fields = [ + "job_name", + "app_name", + "get_help", + "expressao_cron", + "manter_logs", + ] readonly_fields = ("job_name", "app_name", "get_help") inlines = [JobScheduleInline] diff --git a/sigi/apps/utils/jobs/job_controller.py b/sigi/apps/utils/jobs/job_controller.py index a53c500..00b39d9 100644 --- a/sigi/apps/utils/jobs/job_controller.py +++ b/sigi/apps/utils/jobs/job_controller.py @@ -24,6 +24,7 @@ class Job(BaseJob): self.sync_new_jobs() self.run_scheduled() self.schedule_jobs() + self.remove_old_logs() def remove_old_jobs(self): """Remover das tabelas os jobs que foram removidos do código""" @@ -88,3 +89,17 @@ class Job(BaseJob): f"\t\tAgendado job {sched.job.job_name} " f"para {localize(sched.iniciar)}" ) + + def remove_old_logs(self): + print("\tExcluir logs antigos...") + for job in Cronjob.objects.exclude(manter_logs=0): + limite = timezone.localtime() - timezone.timedelta( + days=job.manter_logs + ) + result = JobSchedule.objects.filter( + job=job, + status=JobSchedule.STATUS_CONCLUIDO, + iniciado__lt=limite, + ).delete() + if result[0] > 0: + print(f"\t\t{result[0]} logs excluídos do job '{job}'") diff --git a/sigi/apps/utils/migrations/0004_alter_jobschedule_options_cronjob_manter_logs.py b/sigi/apps/utils/migrations/0004_alter_jobschedule_options_cronjob_manter_logs.py new file mode 100644 index 0000000..2bec7fb --- /dev/null +++ b/sigi/apps/utils/migrations/0004_alter_jobschedule_options_cronjob_manter_logs.py @@ -0,0 +1,22 @@ +# Generated by Django 4.2.7 on 2024-03-26 11:44 + +from django.db import migrations, models + + +class Migration(migrations.Migration): + + dependencies = [ + ('utils', '0003_config'), + ] + + operations = [ + migrations.AlterModelOptions( + name='jobschedule', + options={'ordering': ('-iniciar',), 'verbose_name': 'Agenda de execução', 'verbose_name_plural': 'Agenda de execuções'}, + ), + migrations.AddField( + model_name='cronjob', + name='manter_logs', + field=models.PositiveIntegerField(default=30, help_text='Número de dias que os logs de execução serão mantidos na base de dados. Zero significa que o log jamais será apagado.', verbose_name='dias para manter log'), + ), + ] diff --git a/sigi/apps/utils/models.py b/sigi/apps/utils/models.py index dbe7879..0642b11 100644 --- a/sigi/apps/utils/models.py +++ b/sigi/apps/utils/models.py @@ -50,6 +50,14 @@ class Cronjob(models.Model): "" ), ) + manter_logs = models.PositiveIntegerField( + _("dias para manter log"), + help_text=_( + "Número de dias que os logs de execução serão mantidos " + "na base de dados. Zero significa que o log jamais será apagado." + ), + default=30, + ) class Meta: ordering = ("app_name", "job_name") @@ -141,7 +149,7 @@ class JobSchedule(models.Model): ) class Meta: - ordering = ("iniciar",) + ordering = ("-iniciar",) verbose_name = _("Agenda de execução") verbose_name_plural = _("Agenda de execuções")