task working?

This commit is contained in:
Jakub Kropáček 2025-03-03 21:51:01 +01:00
parent 442118fdc7
commit 98b1705d81
10 changed files with 35 additions and 12 deletions

View file

@ -1,5 +1,6 @@
services: services:
backend: backend:
image: facturio
build: build:
context: . context: .
args: args:

View file

@ -41,6 +41,10 @@ services:
<<: *x-django <<: *x-django
command: ["/app/scripts/run-dramatiq.sh"] command: ["/app/scripts/run-dramatiq.sh"]
scheduler:
<<: *x-django
command: [ "/app/scripts/run-scheduler.sh" ]
gotenberg: gotenberg:
image: gotenberg/gotenberg:8 image: gotenberg/gotenberg:8

View file

@ -47,6 +47,7 @@ MY_APPS = [
'accounts.apps.AccountConfig', 'accounts.apps.AccountConfig',
'subjects.apps.SubjectsConfig', 'subjects.apps.SubjectsConfig',
'invoices.apps.InvoicesConfig', 'invoices.apps.InvoicesConfig',
'utils.apps.UtilsConfig',
] ]
THIRD_PARTY_APPS = [ THIRD_PARTY_APPS = [

10
scripts/run-scheduler.sh Executable file
View file

@ -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

0
utils/__init__.py Normal file
View file

5
utils/apps.py Normal file
View file

@ -0,0 +1,5 @@
from django.apps import AppConfig
class UtilsConfig(AppConfig):
name = 'utils'

View file

View file

View file

@ -4,11 +4,12 @@ from copy import deepcopy
from apscheduler.schedulers.blocking import BlockingScheduler from apscheduler.schedulers.blocking import BlockingScheduler
from apscheduler.triggers.cron import CronTrigger from apscheduler.triggers.cron import CronTrigger
from apscheduler.triggers.interval import IntervalTrigger
from django.conf import settings from django.conf import settings
from django.core.management.base import BaseCommand from django.core.management.base import BaseCommand
from django_apscheduler import util from django_apscheduler import util
from django_apscheduler.jobstores import DjangoJobStore
from django_apscheduler.models import DjangoJobExecution from django_apscheduler.models import DjangoJobExecution
from django_apscheduler.models import DjangoJobStore
from django_dramatiq.models import Task from django_dramatiq.models import Task
logger = logging.getLogger(__name__) logger = logging.getLogger(__name__)
@ -18,17 +19,12 @@ DEFAULT_JOB_KWARGS = {
"replace_existing": True, "replace_existing": True,
} }
# For cron definitions use https://crontab.guru/ DAY_SEC = 24*60*60
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 * * *")
PERIODIC_JOBS = [ PERIODIC_JOBS = [
{ {
"task": "not_working.yet", "task": "utils.tasks:send_queued_mail_task.send",
"trigger": daily_cron, "trigger": IntervalTrigger(seconds=30),
}, },
] ]
@ -38,7 +34,7 @@ PERIODIC_JOBS = [
# unusable or are obsolete are closed before and after your job has run. # 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. # You should use it to wrap any jobs that you schedule that access the Django database in any way.
@util.close_old_connections @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. 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 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): class Command(BaseCommand):
help = "Runs APScheduler." # noqa: A003 help = "Runs APScheduler." # noqa: A003
scheduler = None scheduler: BlockingScheduler = None
def prepare_scheduler(self): def prepare_scheduler(self):
self.stdout.write(self.style.NOTICE("Preparing scheduler")) self.stdout.write(self.style.NOTICE("Preparing scheduler"))
@ -92,7 +88,6 @@ class Command(BaseCommand):
self.prepare_scheduler() self.prepare_scheduler()
self.add_jobs() self.add_jobs()
# signal.signal(signal.SIGINT, self.handle_shutdown)
signal.signal(signal.SIGTERM, self.handle_shutdown) signal.signal(signal.SIGTERM, self.handle_shutdown)
try: try:

7
utils/tasks.py Normal file
View file

@ -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')