-
Notifications
You must be signed in to change notification settings - Fork 17
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
Implement BackgroundService and new Actor class #564
Conversation
4692189
to
8db7fca
Compare
This comment was marked as outdated.
This comment was marked as outdated.
In your commit message,
|
8db7fca
to
b0020c0
Compare
b0020c0
to
0673319
Compare
Updated with the suggestions by @Marenz |
0673319
to
9a31a82
Compare
9a31a82
to
10bc894
Compare
Other than those small things, design wise I didn't notice any problems with the code. I would leave a more detailed review once the draft part is over. |
1d28683
to
ee5ecb1
Compare
OK, now this has all actor classes migrated. I'm pretty happy about the results. Next step are:
And in a follow-up PR:
|
68458ec
to
4d3af38
Compare
Signed-off-by: Leandro Lucarella <luca-frequenz@llucax.com>
When using an union, if we want to use a method present only in one of the union types, then we need to assert for the type, so it is better to just make it generic to the type is always the specific type. Signed-off-by: Leandro Lucarella <luca-frequenz@llucax.com>
We'll need to start actors in this function in the future, so we need it to be async to be able to `await actor.start()`. Signed-off-by: Leandro Lucarella <luca-frequenz@llucax.com>
Signed-off-by: Leandro Lucarella <luca-frequenz@llucax.com>
Signed-off-by: Leandro Lucarella <luca-frequenz@llucax.com>
Signed-off-by: Leandro Lucarella <luca-frequenz@llucax.com>
Signed-off-by: Leandro Lucarella <luca-frequenz@llucax.com>
This improves actors debuggeability. Signed-off-by: Leandro Lucarella <luca-frequenz@llucax.com>
Signed-off-by: Leandro Lucarella <luca-frequenz@llucax.com>
Signed-off-by: Leandro Lucarella <luca-frequenz@llucax.com>
This gives the moving window a common interface and removes some bookkeeping from the implementation, as well as making tests more correct (when using it as an async context manager). Signed-off-by: Leandro Lucarella <luca-frequenz@llucax.com>
Now that actors are stopped properly, we can reestablish the restart limit after it was changed. Signed-off-by: Leandro Lucarella <luca-frequenz@llucax.com>
A new utility `actor_restart_limit` context manager is added to be able to temporarily change the actor's restart limit, and it is now used in the `disable_actor_auto_restart` fixture. Signed-off-by: Leandro Lucarella <luca-frequenz@llucax.com>
This is useful to test that actors are being properly restarted. Signed-off-by: Leandro Lucarella <luca-frequenz@llucax.com>
In particular we test that the new restart rules work as expected, and we also check the logs produced to have extra certainty that what we expect to happen is really happening. The restart tests are done using the `run()` function because we expect to move the restart logic there, so the tests are "forward-compatible". Signed-off-by: Leandro Lucarella <luca-frequenz@llucax.com>
When a `BackgroundService.wait()` is used, it should also propage `CancelledError`s, as is it not always normal that a background service is cancelled unless `stop()` is called, which uses `cancel()` to stop the tasks. When `stop()` is used, we properly filter out all `CancelledError`s too, as in that case we triggered the cancellation, so it is normal. Signed-off-by: Leandro Lucarella <luca-frequenz@llucax.com>
We verify that cancelled actors are reported as such by the `run()` function and that the actor is not automatically restarted. Signed-off-by: Leandro Lucarella <luca-frequenz@llucax.com>
Add some tests for the `BackgroundService` class. The tests were written looking at the coverage to avoid duplicating a lot of tests that are already performed in the `Actor` tests. Signed-off-by: Leandro Lucarella <luca-frequenz@llucax.com>
This is so they don't show up in the generated documentation. Signed-off-by: Leandro Lucarella <luca-frequenz@llucax.com>
Signed-off-by: Leandro Lucarella <luca-frequenz@llucax.com>
ace41e1
to
cf12100
Compare
Enabling auto-merge |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Awesome, everybody get ready to refactor everything now!
I think you also have to click a button, looks like you just typed the message. :D |
It seems like GitHub copilot is not there yet... |
BackgroundService
is a new abstract base class can be used to write other classes that runs one or more tasks in the background. It provides a consistent API to start and stop these services and also takes care of the handling of the background tasks. It can also work as anasync
context manager, giving the service a deterministic lifetime and guaranteed cleanup.The new
Actor
class brings quite a few new improvements over the old@actor
decorator. These are the main differences:start()
needs to be called to start an actor.run()
to_run()
, as it is not intended to be run externally.name
(useful for debugging/logging purposes) andloop
(if the actor should run in a loop different from the currently running loop).Exception
is raised by_run()
. It will not be restarted if the_run()
method finishes normally. If an unhandledBaseException
is raised instead, it will be re-raised. For normal cancellation the_run()
method should handleasyncio.CancelledError
if the cancellation shouldn't be propagated (this is the same as with the decorator)._stop()
method is public (stop()
) and willcancel()
andawait
for the task to finish, catching theasyncio.CancelledError
.join()
method is renamed towait()
, but they can also be awaited directly (await actor
).async
context managers.The base actors (
ConfigManagingActor
,ComponentMetricsResamplingActor
,DataSourcingActor
,PowerDistributingActor
) now inherit from the newActor
class, as well as theMovingWindow
.Fixes #240, fixes #45, fixes #196.