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

Add injections field to Actor and Message #406

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
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
3 changes: 2 additions & 1 deletion dramatiq/actor.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ def __init__(self, fn, *, broker, actor_name, queue_name, priority, options):
self.queue_name = queue_name
self.priority = priority
self.options = options
self.injections = {}
self.broker.declare_actor(self)

def message(self, *args, **kwargs):
Expand Down Expand Up @@ -142,7 +143,7 @@ def __call__(self, *args, **kwargs):
try:
self.logger.debug("Received args=%r kwargs=%r.", args, kwargs)
start = time.perf_counter()
return self.fn(*args, **kwargs)
return self.fn(*args, **{**self.injections, **kwargs})
finally:
delta = time.perf_counter() - start
self.logger.debug("Completed after %.02fms.", delta * 1000)
Expand Down
5 changes: 4 additions & 1 deletion dramatiq/message.py
Original file line number Diff line number Diff line change
Expand Up @@ -74,11 +74,14 @@ class Message(namedtuple("Message", (
"""

def __new__(cls, *, queue_name, actor_name, args, kwargs, options, message_id=None, message_timestamp=None):
return super().__new__(
self = super().__new__(
cls, queue_name, actor_name, tuple(args), kwargs, options,
message_id=message_id or generate_unique_id(),
message_timestamp=message_timestamp or int(time.time() * 1000),
)
# This is specifically not part of namedtuple so it's not part of _asdict() and such
self.injections = {}
return self

def __or__(self, other) -> pipeline:
"""Combine this message into a pipeline with "other".
Expand Down
2 changes: 1 addition & 1 deletion dramatiq/worker.py
Original file line number Diff line number Diff line change
Expand Up @@ -479,7 +479,7 @@ def process_message(self, message):
res = None
if not message.failed:
actor = self.broker.get_actor(message.actor_name)
res = actor(*message.args, **message.kwargs)
res = actor(*message.args, **{**message.injections, **message.kwargs})
if res is not None \
and message.options.get("pipe_target") is None \
and not has_results_middleware(self.broker):
Expand Down