I have a few unrelated services. Currently these are deployed within a single (HttpApp) server, but this may change in future. Some of these services require long-lived resources, like Slick JDBC backends, whose life span should exceed a single request and that should be closed upon server shutdown.
Creating these resources prior to server startup and passing them into the route factory methods of the services feels wrong, since it leaks lots of knowledge about the service requirements into the server.
Currently I’m using something like
case class RouteWithShutdown(route: Route, shutdown: ShutdownCallback)
to return from the service factory methods. The routes are aggregated/path-prefixed/sealed into a single server route, and the shutdown callbacks are bundled and kept to be called in the #postServerShutdown() callback.
Is there any better pattern for this?
Bonus question: If this approach is ok, what would you use for aggregating the shutdown callbacks to ensure that they’re all called and that exceptions during these calls are kept around for later reporting?
Thanks and best regards,