• Stars
    star
    395
  • Rank 109,040 (Top 3 %)
  • Language
    Swift
  • License
    Apache License 2.0
  • Created over 4 years ago
  • Updated about 2 months ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

Cleanly startup and shutdown server application, freeing resources in order before exiting.

Swift Service Lifecycle

Swift Service Lifecycle provides a basic mechanism to cleanly start up and shut down the application, freeing resources in order before exiting. It also provides a Signal-based shutdown hook, to shut down on signals like TERM or INT.

Swift 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. The library codes 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.

This is the beginning of a community-driven open-source project actively seeking contributions, be it code, documentation, or ideas. What Swift Service Lifecycle provides today is covered in the API docs, but it will continue to evolve with community input.

Getting started

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, Swift Service Lifecycle is a great idea. Below you will find all you need to know to get started.

Adding the dependency

To add a dependency on the package, declare it in your Package.swift:

.package(url: "https://github.com/swift-server/swift-service-lifecycle.git", from: "2.0.0"),

and to your application target, add ServiceLifecycle to your dependencies:

.product(name: "ServiceLifecycle", package: "swift-service-lifecycle")

Example Package.swift file with ServiceLifecycle as a dependency:

// swift-tools-version:5.9
import PackageDescription

let package = Package(
    name: "my-application",
    dependencies: [
        .package(url: "https://github.com/swift-server/swift-service-lifecycle.git", from: "2.3.0"),
    ],
    targets: [
        .target(name: "MyApplication", dependencies: [
            .product(name: "ServiceLifecycle", package: "swift-service-lifecycle")
        ]),
        .testTarget(name: "MyApplicationTests", dependencies: [
            .target(name: "MyApplication"),
        ]),
    ]
)

Using ServiceLifecycle

Below is a short usage example however you can find detailed documentation on how to use ServiceLifecycle over here.

ServiceLifecycle consists of two main building blocks. First, the Service protocol and secondly the ServiceGroup. As a library or application developer you should model your long-running work as services that implement the Service protocol. The protocol only requires a single func run() async throws method to be implemented. Afterwards, in your application you can use the ServiceGroup to orchestrate multiple services. The group will spawn a child task for each service and call the respective run method in the child task. Furthermore, the group will setup signal listeners for the configured signals and trigger a graceful shutdown on each service.

actor FooService: Service {
    func run() async throws {
        print("FooService starting")
        try await Task.sleep(for: .seconds(10))
        print("FooService done")
    }
}

@main
struct Application {
    static func main() async throws {
        let service1 = FooService()
        let service2 = FooService()
        
        let serviceGroup = ServiceGroup(
            services: [service1, service2],
            configuration: .init(gracefulShutdownSignals: [.sigterm]),
            logger: logger
        )
        try await serviceGroup.run()
    }
}

Security

Please see SECURITY.md for details on the security process.

More Repositories

1

swift-aws-lambda-runtime

Swift implementation of AWS Lambda Runtime
Swift
1,132
star
2

async-http-client

HTTP client library built on SwiftNIO
Swift
917
star
3

http

โš ๏ธ Historical HTTP API - please use https://github.com/swift-server/async-http-client instead
Swift
703
star
4

vscode-swift

Visual Studio Code Extension for Swift
TypeScript
622
star
5

work-group

โš ๏ธ Historical workgroup organisation
352
star
6

swiftly

A Swift toolchain installer and manager, written in Swift.
Swift
337
star
7

swift-backtrace

๐Ÿ’ฅ Backtraces for Swift on Linux and Windows
C
295
star
8

guides

Guides for building, debugging and deploying Swift Server applications
258
star
9

sswg

Swift Server Working Group (SSWG)
181
star
10

swift-prometheus

Prometheus client library for Swift
Swift
144
star
11

RediStack

Non-blocking, event-driven Swift client for Redis.
Swift
142
star
12

swift-webauthn

A Swift library for implementing the WebAuthn spec
Swift
127
star
13

swift-openapi-vapor

Vapor Bindings for the OpenAPI Generator
Swift
79
star
14

swift-kafka-client

Swift
77
star
15

swift-devcontainer-template

Visual Studio Code Development Container for Swift
Shell
54
star
16

swift-openapi-async-http-client

AsyncHTTPClient transport for Swift OpenAPI Generator.
Swift
54
star
17

security

โš ๏ธ Historical TLS API - please use SwiftNIO instead
Swift
49
star
18

swift-aws-lambda-events

Swift implementation of AWS Lambda Events
Swift
48
star
19

swift-openapi-hummingbird

Hummingbird transport for OpenAPI generator
Shell
27
star
20

swift-memcache-gsoc

Swift
15
star
21

sswg-collection

Dockerfile
11
star
22

swift-openapi-lambda

An AWS Lambda transport for Swift OpenAPI
Swift
8
star
23

swift-service-cache

caches for swift services
6
star
24

swift-etcd-client-gsoc

Swift
5
star