Skip to content

Commit

Permalink
fix cron tasks
Browse files Browse the repository at this point in the history
  • Loading branch information
Her Email committed Nov 12, 2023
1 parent 3314ab3 commit b04da18
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 4 deletions.
16 changes: 15 additions & 1 deletion common/management/commands/cron.py
Original file line number Diff line number Diff line change
@@ -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"
Expand Down
2 changes: 1 addition & 1 deletion common/management/commands/jobs.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,4 +42,4 @@ def handle(self, *args, **options):
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}")
9 changes: 7 additions & 2 deletions common/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@


class BaseJob:
interval = timedelta(hours=1)
interval = timedelta(hours=1) # don't set it less than 1 minute

@classmethod
def cancel(cls):
Expand All @@ -27,7 +27,12 @@ def schedule(cls):
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
Expand Down

0 comments on commit b04da18

Please sign in to comment.