{"id":13872169,"url":"https://github.com/swift-server/swift-service-lifecycle","last_synced_at":"2025-07-16T01:33:02.003Z","repository":{"id":37742283,"uuid":"246219919","full_name":"swift-server/swift-service-lifecycle","owner":"swift-server","description":"Cleanly startup and shutdown server application, freeing resources in order before exiting.","archived":false,"fork":false,"pushed_at":"2024-09-17T15:39:13.000Z","size":390,"stargazers_count":396,"open_issues_count":6,"forks_count":38,"subscribers_count":24,"default_branch":"main","last_synced_at":"2024-10-30T02:22:59.208Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":"https://swiftpackageindex.com/swift-server/swift-service-lifecycle/documentation/servicelifecycle","language":"Swift","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"apache-2.0","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/swift-server.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":"CONTRIBUTING.md","funding":null,"license":"LICENSE.txt","code_of_conduct":"CODE_OF_CONDUCT.md","threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":"SECURITY.md","support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2020-03-10T05:57:11.000Z","updated_at":"2024-10-24T17:19:20.000Z","dependencies_parsed_at":"2024-02-19T09:30:51.236Z","dependency_job_id":"9e1c5857-e906-48f2-8bfe-ecca99ecdd31","html_url":"https://github.com/swift-server/swift-service-lifecycle","commit_stats":{"total_commits":86,"total_committers":15,"mean_commits":5.733333333333333,"dds":"0.34883720930232553","last_synced_commit":"fbdb075d09b12cb6f57051734a3a2af98f809f00"},"previous_names":[],"tags_count":22,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/swift-server%2Fswift-service-lifecycle","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/swift-server%2Fswift-service-lifecycle/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/swift-server%2Fswift-service-lifecycle/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/swift-server%2Fswift-service-lifecycle/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/swift-server","download_url":"https://codeload.github.com/swift-server/swift-service-lifecycle/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":226090030,"owners_count":17572114,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":[],"created_at":"2024-08-05T23:00:35.600Z","updated_at":"2025-07-16T01:33:01.995Z","avatar_url":"https://github.com/swift-server.png","language":"Swift","funding_links":[],"categories":["Swift"],"sub_categories":[],"readme":"# Swift Service Lifecycle\n\nSwift Service Lifecycle provides a basic mechanism to cleanly start up and shut down an application, freeing resources in-order before exiting.\nIt also provides a `Signal`-based shutdown hook, to shut down on signals like `TERM` or `INT`.\n\nSwift Service Lifecycle was designed with the idea that every application has some startup and shutdown workflow-like-logic which is often sensitive to failure and hard to get right.\nThe library encodes this common need in a safe and reusable way that is non-framework specific, and designed to be integrated with any server framework or directly in an application. Furthermore, it integrates natively with Structured Concurrency.\n\nThis is the beginning of a community-driven open-source project actively seeking [contributions](CONTRIBUTING.md), be it code, documentation, or ideas. What Swift Service Lifecycle provides today is covered in the [API docs](https://swiftpackageindex.com/swift-server/swift-service-lifecycle/main/documentation/servicelifecycle), but it will continue to evolve with community input.\n\n## Getting started\n\nSwift Service Lifecycle should be used if you have a server-side Swift application or a cross-platform (e.g. Linux, macOS) application, and you would like to manage its startup and shutdown lifecycle. Below you will find all you need to know to get started.\n\n### Adding the dependency\n\nTo add a dependency on the package, declare it in your `Package.swift`:\n\n```swift\n.package(url: \"https://github.com/swift-server/swift-service-lifecycle.git\", from: \"2.0.0\"),\n```\n\nand add `ServiceLifecycle` to the dependencies of your application target:\n\n```swift\n.product(name: \"ServiceLifecycle\", package: \"swift-service-lifecycle\")\n```\n\nExample `Package.swift` file with `ServiceLifecycle` as a dependency:\n\n```swift\n// swift-tools-version:6.0\nimport PackageDescription\n\nlet package = Package(\n    name: \"my-application\",\n    dependencies: [\n        .package(url: \"https://github.com/swift-server/swift-service-lifecycle.git\", from: \"2.3.0\"),\n    ],\n    targets: [\n        .target(name: \"MyApplication\", dependencies: [\n            .product(name: \"ServiceLifecycle\", package: \"swift-service-lifecycle\")\n        ]),\n        .testTarget(name: \"MyApplicationTests\", dependencies: [\n            .target(name: \"MyApplication\"),\n        ]),\n    ]\n)\n```\n\n###  Using ServiceLifecycle\n\nYou can find a short usage example below. You can find more detailed\ndocumentation on how to use ServiceLifecycle\n[here](https://swiftpackageindex.com/swift-server/swift-service-lifecycle/main/documentation/servicelifecycle).\n\nServiceLifecycle consists of two main building blocks. The `Service` protocol and the `ServiceGroup`\nactor. As a library or application developer you should model your long-running work as services\nthat implement the `Service` protocol. The protocol only requires the implementation of a single\n`func run() async throws` method. Once implemented, your application you can use the `ServiceGroup`\nto orchestrate multiple services. The group will spawn a child task for each service and call the\nrespective `run` method in the child task. Furthermore, the group will setup signal listeners for\nthe configured signals and trigger a graceful shutdown on each service.\n\n```swift\nimport ServiceLifecycle\nimport Logging\n\n// A service can be implemented by a struct, class or actor. For this example we are using a struct.\nstruct FooService: Service {\n    func run() async throws {\n        print(\"FooService starting\")\n        try await Task.sleep(for: .seconds(10))\n        print(\"FooService done\")\n    }\n}\n\n@main\nstruct Application {\n    static let logger = Logger(label: \"Application\")\n    \n    static func main() async throws {\n        let service1 = FooService()\n        let service2 = FooService()\n        \n        let serviceGroup = ServiceGroup(\n            services: [service1, service2],\n            gracefulShutdownSignals: [.sigterm],\n            logger: logger\n        )\n        \n        try await serviceGroup.run()\n    }\n}\n```\n\n## Security\n\nPlease see [SECURITY.md](SECURITY.md) for details on the security process.\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fswift-server%2Fswift-service-lifecycle","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fswift-server%2Fswift-service-lifecycle","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fswift-server%2Fswift-service-lifecycle/lists"}