Browse Source

Adiciona cronjobs ao sistema

pull/159/head
Sesostris Vieira 2 years ago
parent
commit
4c9bf31032
  1. 7
      docker/Dockerfile
  2. 0
      sigi/apps/casas/jobs/__init__.py
  3. 0
      sigi/apps/casas/jobs/daily/__init__.py
  4. 41
      sigi/apps/casas/jobs/daily/usuario_contato.py
  5. 0
      sigi/apps/casas/jobs/hourly/__init__.py
  6. 0
      sigi/apps/casas/jobs/monthly/__init__.py
  7. 0
      sigi/apps/casas/jobs/weekly/__init__.py
  8. 0
      sigi/apps/casas/jobs/yearly/__init__.py
  9. 4
      sigi/apps/parlamentares/jobs/minutely/importa_parlamentar.py
  10. 9
      sigi/apps/parlamentares/jobs/sample.py

7
docker/Dockerfile

@ -43,7 +43,7 @@ ENV AUTH_PROFILE_MODULE=''
# Install env # Install env
ENV RUN_PACKAGES gcc locales build-essential python3-dev graphviz \ ENV RUN_PACKAGES gcc locales build-essential python3-dev graphviz \
libgraphviz-dev pkg-config libpq-dev postgresql-client \ 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 # Install required packages
RUN apt-get update && \ 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 ln -s ${HOME}/etc/nginx/sites-available/sigi.vhost /etc/nginx/sites-enabled/sigi.vhost
RUN mkdir -p /var/log/sigi 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 EXPOSE 80/tcp 443/tcp
ENV DEBIAN_FRONTEND=teletype ENV DEBIAN_FRONTEND=teletype

0
sigi/apps/casas/jobs/__init__.py

0
sigi/apps/casas/jobs/daily/__init__.py

41
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")

0
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

4
sigi/apps/parlamentares/jobs/minutely/importa_parlamentar.py

@ -23,6 +23,9 @@ class Job(MinutelyJob):
if json_data is None: if json_data is None:
return return
json_data["inicio_processamento"] = str(datetime.now()) json_data["inicio_processamento"] = str(datetime.now())
print(
f"Start importing parlamentares at {json_data['inicio_processamento']}: Details: {json_data}"
)
result_final = [] result_final = []
# Importa parlamentares # # Importa parlamentares #
if "resultados" in json_data: if "resultados" in json_data:
@ -53,6 +56,7 @@ class Job(MinutelyJob):
result_final.extend(result["erros"]) result_final.extend(result["erros"])
self.remove_files(json_data) self.remove_files(json_data)
self.send_mail(result_final, json_data) self.send_mail(result_final, json_data)
print(f"Finish import parlamentares. Final result: {result_final}")
return return
def get_json_data(self): def get_json_data(self):

9
sigi/apps/parlamentares/jobs/sample.py

@ -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
Loading…
Cancel
Save