swift-server/swift-service-lifecycle

Crash on start if no tasks have been registered

fabianfett opened this issue · 3 comments

If we use ServiceLifecycle without registering any tasks, a precondition fails in ComponentLifecycle._start.

        precondition(tasks.count > 0, "invalid number of tasks, must be > 0")

I think this case should be handled and should not lead to a crash. The ServiceLifecycle may be used in tests in which no tasks are registered, but the class, that is tested, still expects to be initialized with a ServiceLifecycle.

Totally agree. Imagine an application where all the startup and shutdown tasks are provided by configurable plugins. A fresh boilerplate doesn't contain any tasks, but we do need to use ServiceLifecycle to handle the lifecycle of plugins.

I would prefer logging to preconditioning:

if tasks.count == 0 {
    logger.notice("No task provided.")
}
ktoso commented

Makes sense to me, could you send in a PR? :)