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

Allow jobs to be defined as coroutines #39

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
5 changes: 3 additions & 2 deletions django_dbq/management/commands/worker.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
from simplesignals.process import WorkerProcessBase
from time import sleep
import logging
from django_dbq.utils import run_job


logger = logging.getLogger(__name__)
Expand Down Expand Up @@ -35,7 +36,7 @@ def process_job(queue_name):

try:
task_function = import_string(job.next_task)
task_function(job)
run_job(task_function, job)
job.update_next_task()
if not job.next_task:
job.state = Job.STATES.COMPLETE
Expand All @@ -51,7 +52,7 @@ def process_job(queue_name):
"Running failure hook %s for job id=%s", failure_hook_name, job.pk
)
failure_hook_function = import_string(failure_hook_name)
failure_hook_function(job, exception)
run_job(failure_hook_function, job, exception)
else:
logger.info("No failure hook for job id=%s", job.pk)

Expand Down
3 changes: 2 additions & 1 deletion django_dbq/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
import datetime
import logging
import uuid
from django_dbq.utils import run_job


logger = logging.getLogger(__name__)
Expand Down Expand Up @@ -137,7 +138,7 @@ def run_creation_hook(self):
if creation_hook_name:
logger.info("Running creation hook %s for new job", creation_hook_name)
creation_hook_function = import_string(creation_hook_name)
creation_hook_function(self)
run_job(creation_hook_function, self)

@staticmethod
def get_queue_depths():
Expand Down
8 changes: 8 additions & 0 deletions django_dbq/utils.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import asyncio


def run_job(job_function, *args, **kwargs):
if asyncio.iscoroutinefunction(job_function):
asyncio.run(job_function(*args, **kwargs))
else:
job_function(*args, **kwargs)