Browse Source

Aprimoramentos no controle de cronjobs

pull/172/head
Sesóstris Vieira 9 months ago
parent
commit
a8281d22b3
  1. 8
      sigi/apps/utils/admin.py
  2. 15
      sigi/apps/utils/jobs/job_controller.py
  3. 22
      sigi/apps/utils/migrations/0004_alter_jobschedule_options_cronjob_manter_logs.py
  4. 10
      sigi/apps/utils/models.py

8
sigi/apps/utils/admin.py

@ -66,7 +66,13 @@ class CronjobAdmin(admin.ModelAdmin):
"get_schedule", "get_schedule",
"get_runner", "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") readonly_fields = ("job_name", "app_name", "get_help")
inlines = [JobScheduleInline] inlines = [JobScheduleInline]

15
sigi/apps/utils/jobs/job_controller.py

@ -24,6 +24,7 @@ class Job(BaseJob):
self.sync_new_jobs() self.sync_new_jobs()
self.run_scheduled() self.run_scheduled()
self.schedule_jobs() self.schedule_jobs()
self.remove_old_logs()
def remove_old_jobs(self): def remove_old_jobs(self):
"""Remover das tabelas os jobs que foram removidos do código""" """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"\t\tAgendado job {sched.job.job_name} "
f"para {localize(sched.iniciar)}" 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}'")

22
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'),
),
]

10
sigi/apps/utils/models.py

@ -50,6 +50,14 @@ class Cronjob(models.Model):
"</a>" "</a>"
), ),
) )
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: class Meta:
ordering = ("app_name", "job_name") ordering = ("app_name", "job_name")
@ -141,7 +149,7 @@ class JobSchedule(models.Model):
) )
class Meta: class Meta:
ordering = ("iniciar",) ordering = ("-iniciar",)
verbose_name = _("Agenda de execução") verbose_name = _("Agenda de execução")
verbose_name_plural = _("Agenda de execuções") verbose_name_plural = _("Agenda de execuções")

Loading…
Cancel
Save