Skip to content

Activity worker refactor #860

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

Merged
merged 2 commits into from
May 17, 2025
Merged

Activity worker refactor #860

merged 2 commits into from
May 17, 2025

Conversation

dandavison
Copy link
Contributor

@dandavison dandavison commented May 8, 2025

This is pure refactoring for readability. I found it very helpful while creating a Nexus worker that is similar in some respects to the activity worker. All this PR does is move us to this pattern:

        try:
            await self._execute_activity(start, running_activity, completion) # 170 lines
        except BaseException as err:
            # 80 lines of error handling plus sending completion

i.e. separating the execution logic from the error handling, since it's very hard to read a try...except with 170 lines in the middle.

@dandavison dandavison marked this pull request as ready for review May 8, 2025 22:30
@dandavison dandavison requested a review from a team as a code owner May 8, 2025 22:30
Copy link
Member

@cretz cretz left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not the biggest fan of de-inlining single-use logic for running an activity from run_activity when it is only used in one place. If anything, I could understand exception handling being de-inlined if it was really too long for you. But not enough disagreement to prevent the change.

@dandavison
Copy link
Contributor Author

dandavison commented May 16, 2025

Not the biggest fan of de-inlining single-use logic for running an activity from run_activity when it is only used in one place. If anything, I could understand exception handling being de-inlined if it was really too long for you.

As explained in the intro, the logic inside the try...except was 170 lines long. That's always considered unacceptably long in Python codebases in my experience, and it's standard to de-inline in such situations even though it will be single use. Standards may differ in some compiled languages, but in dynamic languages like Python there's a strong emphasis on readability because of the risk of making mistakes that won't be discovered until run-time (and that risk was even greater in the pre-static-typing era in which Python coding style conventions became establised).

@cretz
Copy link
Member

cretz commented May 16, 2025

As explained in the intro, the logic inside the try...except was 170 lines long. That's always considered unacceptably long in Python codebases in my experience

Meh mostly only long because of the forced wrapping, it's not that many statements. Regardless, if this is too long for you, my suggestion is to extract out the except body, not the try body. Forcing users to hop so many levels deep for the primary logic makes code hard to understand.

@dandavison dandavison force-pushed the activity-worker-refactor branch from 33e32c4 to 1aa3165 Compare May 16, 2025 14:56
@dandavison
Copy link
Contributor Author

Regardless, if this is too long for you, my suggestion is to extract out the except body, not the try body. Forcing users to hop so many levels deep for the primary logic makes code hard to understand.

As I've said, in Python codebases with good style conventions, one never places 170 lines nested inside a try statement. You might have missed the addition I made:

Standards may differ in some compiled languages, but in dynamic languages like Python there's a strong emphasis on readability because of the risk of making mistakes that won't be discovered until run-time (and that risk was even greater in the pre-static-typing era in which Python coding style conventions became establised).

@@ -201,6 +201,7 @@ async def drain_poll_queue(self) -> None:

# Only call this after run()/drain_poll_queue() have returned. This will not
# raise an exception.
# TODO(dan): check accuracy of this comment; I would say it *does* raise an exception.
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@cretz can you help resolve this TODO / question?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think it's probably a mostly true statement because the _run_activity is written to hopefully trap exceptions, but I think the intention was to set return_exceptions=True instead of False below, so that's probably what we should do here.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks, yes that's exactly what I was thinking. Updated TODO comment to that effect and leaving it in.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would be totally ok changing the parameter

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

OK, yes let's investigate that. But this is a pure refactoring PR and I don't like mixing functional changes with refactoring.

@dandavison dandavison force-pushed the activity-worker-refactor branch from 1aa3165 to 365f567 Compare May 16, 2025 17:28
@dandavison dandavison force-pushed the activity-worker-refactor branch from 990e89f to cbb26d7 Compare May 17, 2025 13:44
@dandavison dandavison merged commit e9d3b01 into main May 17, 2025
18 checks passed
@dandavison dandavison deleted the activity-worker-refactor branch May 17, 2025 15:38
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants