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
beigna opened this issue Apr 11, 2019 · 2 comments
Closed

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

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

Comments

@beigna
Copy link

beigna 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 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 modified the milestones: v1.7.0, v1.6.0 Apr 15, 2019
@beigna
Copy link
Author

beigna 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
Development

No branches or pull requests

2 participants