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 a way for actors to access the current message #208

Closed
nachopro opened this issue Apr 11, 2019 · 2 comments
Closed

Add a way for actors to access the current message #208

nachopro opened this issue Apr 11, 2019 · 2 comments
Milestone

Comments

@nachopro
Copy link

@nachopro nachopro commented Apr 11, 2019

Hello. I have an Actor (function) and I need to know if it is the first or fifth execution.
(based on his retry number I must execute a complementary code)

Maybe I should use a middleware or pipeline?

Thanks for your time.

@Bogdanp
Copy link
Owner

@Bogdanp Bogdanp commented Apr 12, 2019

Can you use retry_when?

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.

@Bogdanp Bogdanp changed the title How can I know the retry number? Add a way for actors to access the current message Apr 12, 2019
@Bogdanp Bogdanp added this to the v1.7.0 milestone Apr 12, 2019
@Bogdanp Bogdanp closed this in 48e59ec Apr 15, 2019
@Bogdanp Bogdanp removed this from the v1.7.0 milestone Apr 15, 2019
@Bogdanp Bogdanp added this to the v1.6.0 milestone Apr 15, 2019
@nachopro
Copy link
Author

@nachopro nachopro commented Apr 15, 2019

@Bogdanp I was away from the PC these days :(
I switched my code to work with callbacks (with some introduced bugs :P) but this feature is a great solution!

I'm very glad with your response. Thanks you very much.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Linked pull requests

Successfully merging a pull request may close this issue.

None yet
2 participants