The developers behind Apple’s Swift language have introduced an open source project for the Swift server ecosystem, called Swift Service Lifecycle, to aid server applications with managing startup and shutdown sequences.
The project is accessible from Github and can cleanly starts up and shuts down an application, freeing resources before exiting. Also provided is a Signal-based shutdown hook to shut down on signals TERM or INT.
The project was designed with the notion that each application has startup and workflow-like logic that often is sensitive to failure and difficult to do correctly. Startup sequences include actions like initializing thread pools, warming up caches, running data migrations, and performing other types of state initialization prior to accepting events or taking traffic. Shutdown sequences free up resources that hold on to file descriptors or other resources that could leak if not correctly cleared.
Swift Service Lifecycle codes this common need for startup and shutdown logic in a safe, reusable way that is non-framework-specific. The recommended way to use the library is to create a ServiceLifecycle instance in an application’s main method and register LifecycleTasks. When calling the start function, ServiceLifecycle will start these tasks in the order registered. ServiceLifecycle also registers a signal handler that traps TERM or INT.