From 4c9bf31032597e922d325db4f91f87c66b17aee8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ses=C3=B3stris=20Vieira?= Date: Mon, 19 Sep 2022 15:14:21 -0300 Subject: [PATCH] Adiciona cronjobs ao sistema --- docker/Dockerfile | 7 +++- sigi/apps/casas/jobs/__init__.py | 0 sigi/apps/casas/jobs/daily/__init__.py | 0 sigi/apps/casas/jobs/daily/usuario_contato.py | 41 +++++++++++++++++++ sigi/apps/casas/jobs/hourly/__init__.py | 0 sigi/apps/casas/jobs/monthly/__init__.py | 0 sigi/apps/casas/jobs/weekly/__init__.py | 0 sigi/apps/casas/jobs/yearly/__init__.py | 0 .../jobs/minutely/importa_parlamentar.py | 4 ++ sigi/apps/parlamentares/jobs/sample.py | 9 ---- 10 files changed, 51 insertions(+), 10 deletions(-) create mode 100644 sigi/apps/casas/jobs/__init__.py create mode 100644 sigi/apps/casas/jobs/daily/__init__.py create mode 100644 sigi/apps/casas/jobs/daily/usuario_contato.py create mode 100644 sigi/apps/casas/jobs/hourly/__init__.py create mode 100644 sigi/apps/casas/jobs/monthly/__init__.py create mode 100644 sigi/apps/casas/jobs/weekly/__init__.py create mode 100644 sigi/apps/casas/jobs/yearly/__init__.py delete mode 100644 sigi/apps/parlamentares/jobs/sample.py diff --git a/docker/Dockerfile b/docker/Dockerfile index 0782d8a..bd6cd2d 100644 --- a/docker/Dockerfile +++ b/docker/Dockerfile @@ -43,7 +43,7 @@ ENV AUTH_PROFILE_MODULE='' # Install env ENV RUN_PACKAGES gcc locales build-essential python3-dev graphviz \ libgraphviz-dev pkg-config libpq-dev postgresql-client \ - libsasl2-dev libldap2-dev libssl-dev vim nginx + libsasl2-dev libldap2-dev libssl-dev vim nginx cron # Install required packages RUN apt-get update && \ @@ -90,6 +90,11 @@ RUN rm -f /etc/nginx/sites-enabled/* RUN ln -s ${HOME}/etc/nginx/sites-available/sigi.vhost /etc/nginx/sites-enabled/sigi.vhost RUN mkdir -p /var/log/sigi +# schedule cron jobs +RUN crontab -l | { cat; echo "* * * * * ${HOME}/manage.py runjobs minutely >> /var/log/sigi/minutely.log"; } | crontab - +RUN crontab -l | { cat; echo "0 * * * * ${HOME}/manage.py runjobs hourly >> /var/log/sigi/hourly.log"; } | crontab - +RUN crontab -l | { cat; echo "0 0 * * * ${HOME}/manage.py runjobs daily >> /var/log/sigi/daily.log"; } | crontab - + EXPOSE 80/tcp 443/tcp ENV DEBIAN_FRONTEND=teletype diff --git a/sigi/apps/casas/jobs/__init__.py b/sigi/apps/casas/jobs/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/sigi/apps/casas/jobs/daily/__init__.py b/sigi/apps/casas/jobs/daily/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/sigi/apps/casas/jobs/daily/usuario_contato.py b/sigi/apps/casas/jobs/daily/usuario_contato.py new file mode 100644 index 0000000..a72dfe0 --- /dev/null +++ b/sigi/apps/casas/jobs/daily/usuario_contato.py @@ -0,0 +1,41 @@ +from django.db.models import Q +from django.contrib.auth.models import User +from django_extensions.management.jobs import DailyJob +from sigi.apps.casas.models import Orgao, Funcionario + + +class Job(DailyJob): + help = "Ativa / desativa usuários para os Contatos Interlegis" + + def execute(self): + # Seleciona contatos interlegis com mínimo de informações + # (nome, cpf, email). Elegíveis para fazer login no sistema + contatos = Funcionario.objects.filter( + setor="contato_interlegis" + ).exclude(Q(nome="") | Q(cpf="") | Q(email="")) + + # Ativa / atualiza usuários para os contatos interlegis elegíveis + for contato in contatos: + email = contato.email + first, *__, last = f"{contato.nome} ".split(" ") + user, created = User.objects.update_or_create( + defaults={ + "email": email, + "first_name": first, + "last_name": last, + "is_active": True, + "is_staff": False, + "is_superuser": False, + }, + username=email, + ) + print( + f"{['Updated', 'Created'][created]} user {user.username} for contato {contato.id}" + ) + # Desativa usuários de contatos que não estão na lista de elegíveis + qtd_desativados = ( + User.objects.filter(username__contains="@", is_active=True) + .exclude(username__in=contatos.values_list("email", flat=True)) + .update(is_active=False) + ) + print(f"{qtd_desativados} usuários desativados") diff --git a/sigi/apps/casas/jobs/hourly/__init__.py b/sigi/apps/casas/jobs/hourly/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/sigi/apps/casas/jobs/monthly/__init__.py b/sigi/apps/casas/jobs/monthly/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/sigi/apps/casas/jobs/weekly/__init__.py b/sigi/apps/casas/jobs/weekly/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/sigi/apps/casas/jobs/yearly/__init__.py b/sigi/apps/casas/jobs/yearly/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/sigi/apps/parlamentares/jobs/minutely/importa_parlamentar.py b/sigi/apps/parlamentares/jobs/minutely/importa_parlamentar.py index a08c380..e622895 100644 --- a/sigi/apps/parlamentares/jobs/minutely/importa_parlamentar.py +++ b/sigi/apps/parlamentares/jobs/minutely/importa_parlamentar.py @@ -23,6 +23,9 @@ class Job(MinutelyJob): if json_data is None: return json_data["inicio_processamento"] = str(datetime.now()) + print( + f"Start importing parlamentares at {json_data['inicio_processamento']}: Details: {json_data}" + ) result_final = [] # Importa parlamentares # if "resultados" in json_data: @@ -53,6 +56,7 @@ class Job(MinutelyJob): result_final.extend(result["erros"]) self.remove_files(json_data) self.send_mail(result_final, json_data) + print(f"Finish import parlamentares. Final result: {result_final}") return def get_json_data(self): diff --git a/sigi/apps/parlamentares/jobs/sample.py b/sigi/apps/parlamentares/jobs/sample.py deleted file mode 100644 index b1ae62c..0000000 --- a/sigi/apps/parlamentares/jobs/sample.py +++ /dev/null @@ -1,9 +0,0 @@ -from django_extensions.management.jobs import BaseJob - - -class Job(BaseJob): - help = "My sample job." - - def execute(self): - # executing empty sample job - pass