Skip to content

Commit

Permalink
A task always has a Blueprint
Browse files Browse the repository at this point in the history
but to configure an task, their blueprint needs to be an App
  • Loading branch information
ewjoachim committed Dec 5, 2021
1 parent 30ebf30 commit 2838bc1
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 8 deletions.
10 changes: 7 additions & 3 deletions procrastinate/blueprints.py
Original file line number Diff line number Diff line change
Expand Up @@ -86,10 +86,12 @@ def _check_stack(self):
)

def _register_task(self, task: "tasks.Task") -> None:
from procrastinate import tasks

# Each call to _add_task may raise TaskAlreadyRegistered.
# We're using an intermediary dict to make sure that if the registration
# is interrupted midway though, self.tasks is left unmodified.
to_add = {}
to_add: Dict[str, tasks.Task] = {}
self._add_task(task=task, name=task.name, to=to_add)

for alias in task.aliases:
Expand Down Expand Up @@ -150,9 +152,11 @@ def add_tasks_from(self, blueprint: "Blueprint", *, namespace: str) -> None:
raise exceptions.TaskAlreadyRegistered(
f"A namespace named {namespace} was already registered"
)
# Modify existing tasks and other blueprint to add the namespace
# Modify existing tasks and other blueprint to add the namespace, and
# set the blueprint
for task in set(blueprint.tasks.values()):
task.add_namespace(namespace)
task.blueprint = self
blueprint.tasks = new_tasks

# Finally, add the namespaced tasks to this namespace
Expand Down Expand Up @@ -226,7 +230,7 @@ def _wrap(func: Callable[..., "tasks.Task"]):

task = tasks.Task(
func,
app=None,
blueprint=self,
queue=queue,
lock=lock,
queueing_lock=queueing_lock,
Expand Down
13 changes: 8 additions & 5 deletions procrastinate/tasks.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@
import logging
from typing import Any, Callable, Dict, List, Optional

from procrastinate import app, exceptions, jobs, manager
from procrastinate import app as app_module
from procrastinate import blueprints, exceptions, jobs, manager
from procrastinate import retry as retry_module
from procrastinate import types, utils

Expand Down Expand Up @@ -72,7 +73,7 @@ def __init__(
self,
func: Callable,
*,
app: Optional[app.App],
blueprint: blueprints.Blueprint,
# task naming
name: Optional[str] = None,
aliases: Optional[List[str]] = None,
Expand All @@ -85,7 +86,7 @@ def __init__(
queueing_lock: Optional[str] = None,
):
self.queue = queue
self.app = app
self.blueprint = blueprint
self.func: Callable = func
self.aliases = aliases if aliases else []
self.retry_strategy = retry_module.get_retry_strategy(retry)
Expand Down Expand Up @@ -177,12 +178,14 @@ def configure(
ValueError
If you try to define both schedule_at and schedule_in
"""
if self.app is None:
if not isinstance(self.blueprint, app_module.App):
raise exceptions.UnboundTaskError

app = self.blueprint

return configure_task(
name=self.name,
job_manager=self.app.job_manager,
job_manager=app.job_manager,
lock=lock if lock is not None else self.lock,
queueing_lock=(
queueing_lock if queueing_lock is not None else self.queueing_lock
Expand Down

0 comments on commit 2838bc1

Please sign in to comment.