Commit 79776f0
authored
The name `BackgroundService` is a bit too verbose now in the context of
an asyncio library. Services are usually already understood as running
in the background, so the `Background` prefix is redundant and makes the
class name too long.
The `Service` class is made an abstract base class intended for end
users that just want to use services, and a new `ServiceBase` class is
added for developers that want to implement services. This way, the
documentation can be separated between the two use cases and the
`Service` class can be used as a generic type for services. We also
avoid leaking implementation details to services users.
The `ServiceBase` class also have a new method `create_task` that is
used to create tasks in the service, and a new `TaskCreator` protocol is
added to allow overriding how tasks are created (for example, using a
custom `loop` or a `TaskGroup`). `create_task` automatically adds the
task to the internal set, so hopefully users won't forget so easily to
register tasks.
The `create_task` method also have a new `log_exceptions` parameter that
allows to log exceptions raised by tasks. This is useful because
services tasks are supposed to run forever, and there are no many
opportunities to await for their completion appropriately and handle the
failure. With this at least we make sure we will get a log message when
a task raises an exception, so we can at least know what happened and
they just silently disappear.
Fixes #8, improves #9.
2 files changed
+306
-70
lines changed
0 commit comments