-
Notifications
You must be signed in to change notification settings - Fork 510
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
fix(crons): Make monitor
async friendly
#2912
Merged
Merged
Conversation
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
sentrivana
changed the title
fix(crons): Make @monitor work with async functions
fix(crons): Make Mar 27, 2024
monitor
work with async functions
sentrivana
changed the title
fix(crons): Make
fix(crons): Make Mar 27, 2024
monitor
work with async functionsmonitor
async friendly
szokeasaurusrex
approved these changes
Mar 27, 2024
antonpirker
approved these changes
Mar 28, 2024
3 tasks
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Our
monitor
decorator/context manager is not async friendly. If you wrap an async function in it,monitor
will only wrap the creation of the coroutine, rather than its execution, which means it'll exit early. It won't capture the actual duration of the execution of the function, and the cron run will always be reported as success.To better illustrate, try this out:
If you execute this, you'll see multiple fishy things:
debug=True
)To fix this, we need special handling for the async case where we actually execute the coroutine and wait until it's finished before reporting.
Implementation Notes
This is annoying to implement cleanly because of Python 2 compatibility. Python 2 will throw
SyntaxErrors
when it encounters anyasync def
s. We could use anexec
for the async part, but this is ugly.Instead, I'm implementing the Python2+3-friendly part of the decorator in the main
monitor
class and outsourcing the problematic__call__
definition to version-specific mixins, where the Python 3-only mixin will never get imported on Python 2.In any case, we can get rid of this compat hack with SDK 2.0.