fix cron tasks

This commit is contained in:
Her Email 2023-11-11 20:33:55 -05:00 committed by Henri Dickson
parent 3314ab3dd6
commit 0415ce8f06
3 changed files with 23 additions and 4 deletions

View file

@ -1,11 +1,25 @@
import random
from time import sleep
from django.core.management.base import BaseCommand
from loguru import logger
from catalog.jobs import * # noqa
from common.models import JobManager
from common.models import BaseJob, JobManager
from mastodon.jobs import * # noqa
from users.jobs import * # noqa
# @JobManager.register
# class DummyJob(BaseJob):
# interval = timedelta(seconds=10)
# def run(self):
# logger.info("Dummy job started")
# if random.choice([0, 1]) == 0:
# raise Exception("Dummy job randomly failed")
# sleep(3)
# logger.info("Dummy job stopped")
class Command(BaseCommand):
help = "Schedule timed jobs"

View file

@ -42,4 +42,4 @@ class Command(BaseCommand):
self.style.SUCCESS(f"{registry.key} {repr(job)}")
)
except Exception as e:
print(f"Error fetching {registry.key} {job_id}")
print(f"Fetching {registry.key} {job_id} error: {e}")

View file

@ -7,7 +7,7 @@ from rq.registry import ScheduledJobRegistry
class BaseJob:
interval = timedelta(hours=1)
interval = timedelta(hours=1) # don't set it less than 1 minute
@classmethod
def cancel(cls):
@ -27,7 +27,12 @@ class BaseJob:
job_id = cls.__name__
logger.info(f"Scheduling job: {job_id} in {cls.interval}")
django_rq.get_queue("cron").enqueue_in(
cls.interval, cls._run, job_id=job_id, result_ttl=0, failure_ttl=0
cls.interval,
cls._run,
job_id=job_id,
result_ttl=-1,
failure_ttl=-1,
job_timeout=cls.interval.seconds - 5,
)
@classmethod