diff --git a/docker-compose.build.yml b/docker-compose.build.yml index 157a6f5..11cabe5 100644 --- a/docker-compose.build.yml +++ b/docker-compose.build.yml @@ -1,5 +1,6 @@ services: backend: + image: facturio build: context: . args: diff --git a/docker-compose.yml b/docker-compose.yml index 3171f5f..58fd6d2 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -41,6 +41,10 @@ services: <<: *x-django command: ["/app/scripts/run-dramatiq.sh"] + scheduler: + <<: *x-django + command: [ "/app/scripts/run-scheduler.sh" ] + gotenberg: image: gotenberg/gotenberg:8 diff --git a/facturio/settings/base.py b/facturio/settings/base.py index ed820ad..5acb987 100644 --- a/facturio/settings/base.py +++ b/facturio/settings/base.py @@ -47,6 +47,7 @@ MY_APPS = [ 'accounts.apps.AccountConfig', 'subjects.apps.SubjectsConfig', 'invoices.apps.InvoicesConfig', + 'utils.apps.UtilsConfig', ] THIRD_PARTY_APPS = [ diff --git a/scripts/run-scheduler.sh b/scripts/run-scheduler.sh new file mode 100755 index 0000000..7c9ff8e --- /dev/null +++ b/scripts/run-scheduler.sh @@ -0,0 +1,10 @@ +#!/usr/bin/env bash + +set -o nounset +set -o xtrace +set -o errexit +set -o pipefail + + +echo "Running scheduler..." +./manage.py runscheduler diff --git a/utils/__init__.py b/utils/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/utils/apps.py b/utils/apps.py new file mode 100644 index 0000000..7527884 --- /dev/null +++ b/utils/apps.py @@ -0,0 +1,5 @@ +from django.apps import AppConfig + + +class UtilsConfig(AppConfig): + name = 'utils' diff --git a/utils/management/__init__.py b/utils/management/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/utils/management/commands/__init__.py b/utils/management/commands/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/facturio/management/commands/run_scheduler.py b/utils/management/commands/runscheduler.py similarity index 84% rename from facturio/management/commands/run_scheduler.py rename to utils/management/commands/runscheduler.py index 183a999..d9e4550 100644 --- a/facturio/management/commands/run_scheduler.py +++ b/utils/management/commands/runscheduler.py @@ -4,11 +4,12 @@ from copy import deepcopy from apscheduler.schedulers.blocking import BlockingScheduler from apscheduler.triggers.cron import CronTrigger +from apscheduler.triggers.interval import IntervalTrigger from django.conf import settings from django.core.management.base import BaseCommand from django_apscheduler import util +from django_apscheduler.jobstores import DjangoJobStore from django_apscheduler.models import DjangoJobExecution -from django_apscheduler.models import DjangoJobStore from django_dramatiq.models import Task logger = logging.getLogger(__name__) @@ -18,17 +19,12 @@ DEFAULT_JOB_KWARGS = { "replace_existing": True, } -# For cron definitions use https://crontab.guru/ - -daily_cron = CronTrigger.from_crontab("0 0 * * *") -weekly_cron = CronTrigger.from_crontab("0 0 * * 0") -monthly_cron = CronTrigger.from_crontab("30 3 1 * *") -daily_export_cron = CronTrigger.from_crontab("0 5 * * *") +DAY_SEC = 24*60*60 PERIODIC_JOBS = [ { - "task": "not_working.yet", - "trigger": daily_cron, + "task": "utils.tasks:send_queued_mail_task.send", + "trigger": IntervalTrigger(seconds=30), }, ] @@ -38,7 +34,7 @@ PERIODIC_JOBS = [ # unusable or are obsolete are closed before and after your job has run. # You should use it to wrap any jobs that you schedule that access the Django database in any way. @util.close_old_connections -def delete_old_job_executions(max_age: int = 604_800) -> None: +def delete_old_job_executions(max_age: int = 7 * DAY_SEC) -> None: """ This job deletes APScheduler job execution entries older than `max_age` from the database. It helps to prevent the database from filling up with old historical records that are no @@ -53,7 +49,7 @@ def delete_old_job_executions(max_age: int = 604_800) -> None: class Command(BaseCommand): help = "Runs APScheduler." # noqa: A003 - scheduler = None + scheduler: BlockingScheduler = None def prepare_scheduler(self): self.stdout.write(self.style.NOTICE("Preparing scheduler")) @@ -92,7 +88,6 @@ class Command(BaseCommand): self.prepare_scheduler() self.add_jobs() - # signal.signal(signal.SIGINT, self.handle_shutdown) signal.signal(signal.SIGTERM, self.handle_shutdown) try: diff --git a/utils/tasks.py b/utils/tasks.py new file mode 100644 index 0000000..7c4082b --- /dev/null +++ b/utils/tasks.py @@ -0,0 +1,7 @@ +import dramatiq +from django.core.management import call_command + + +@dramatiq.actor() +def send_queued_mail_task(): + call_command('send_queued_mail')