Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix cron tasks #373

Merged
merged 2 commits into from
Nov 12, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Binary file not shown.