Skip to content

Commit

Permalink
Remove cases where task names or aliases may clobber other tasks
Browse files Browse the repository at this point in the history
  • Loading branch information
ewjoachim committed Dec 5, 2021
1 parent afd7cfd commit 30ebf30
Showing 1 changed file with 19 additions and 13 deletions.
32 changes: 19 additions & 13 deletions procrastinate/blueprints.py
Original file line number Diff line number Diff line change
Expand Up @@ -86,24 +86,30 @@ def _check_stack(self):
)

def _register_task(self, task: "tasks.Task") -> None:
# 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 = {}
if task.name in self.tasks:
raise exceptions.TaskAlreadyRegistered(
f"A task named {task.name} was already registered"
)
to_add[task.name] = task
self._add_task(task=task, name=task.name, to=to_add)

for alias in task.aliases:
if alias in self.tasks:
raise exceptions.TaskAlreadyRegistered(
f"A task with alias {alias} was already registered"
)

to_add[alias] = task
self._add_task(task=task, name=alias, to=to_add)

self.tasks.update(to_add)

def add_task_alias(self, task: "tasks.Task", alias: str):
def _add_task(
self, task: "tasks.Task", name: str, to: Optional[dict] = None
) -> None:

if name in self.tasks:
raise exceptions.TaskAlreadyRegistered(
f"A task named {name} was already registered"
)

result_dict = self.tasks if to is None else to
result_dict[name] = task

def add_task_alias(self, task: "tasks.Task", alias: str) -> None:
"""
Add an alias to a task. This can be useful if a task was in a given
Blueprint and moves to a different blueprint.
Expand All @@ -115,7 +121,7 @@ def add_task_alias(self, task: "tasks.Task", alias: str):
alias : str
New alias (including potential namespace, separated with ``:``)
"""
self.tasks[alias] = task
self._add_task(task=task, name=alias)

def add_tasks_from(self, blueprint: "Blueprint", *, namespace: str) -> None:
"""
Expand Down

0 comments on commit 30ebf30

Please sign in to comment.