Add a way for actors to access the current message #208
Hello. I have an Actor (function) and I need to know if it is the first or fifth execution.
Maybe I should use a middleware or pipeline?
Thanks for your time.
The text was updated successfully, but these errors were encountered:
Can you use
def should_retry(retries_so_far, e): ... @actor(retry_when=should_retry) def some_fn(): ...
Otherwise, you can define a middleware that exposes the current message using a thread-local variable:
class CurrentMessage(Middleware): STATE = local() @classmethod def get_current_message(): return getattr(CurrentMessage.STATE, "message", None) def before_process_message(self, broker, message): setattr(self.STATE, "message", message) def after_process_message(self, broker, message, *, result=None, exception=None): delattr(self.STATE, "message")
I'll probably add something like this to one of the upcoming versions since having access to the current message can sometimes be useful.