swift-systemd

0.0.4

Simple Swift library to interface with systemd on Linux systems
xtremekforever/swift-systemd

What's New

swift-systemd v0.0.4

2024-01-06T20:17:24Z

Adds simple support for sending the WATCHDOG=1 notification from SystemdNotifier and support for sending it automatically from SystemdService using a configurable interval:

let systemdService = SystemdService(watchdogEnabled: true, watchdogInterval: .seconds(30))

Systemd

A simple Swift library to interface with systemd on Linux.

Installation

Add the following dependency to your Package.swift file:

.package(url: "https://github.com/xtremekforever/swift-systemd.git", from: "0.0.1")

Then, add it to your target dependencies section like this:

.product(name: "Systemd", package: "swift-systemd")

Usage

Add import Systemd to the app use the modules provided by this library.

To see if the app is running under systemd, use the SystemdHelpers interface:

if SystemdHelpers.isSystemdService {
    print("This app is running as a systemd service!")

    // do things like modify logging format (if using swift-log) or whatever else is needed.
}

To send signals to systemd about the state of the app, use the SystemdNotifier interface:

let notifier = SystemdNotifier()

// Call after starting up app (sends READY=1)
notifier.notify(ServiceState.Ready)

// Call before exiting app (sends STOPPING=1)
notifier.notify(ServiceState.Stopping)

Systemd Lifecycle

This repo also contains a separate SystemdLifecycle product that can be used by projects that employ the swift-service-lifecycle library to run and manage application services. It is a simple service that sends the READY=1 and STOPPING=1 signals above from the service run() method.

It can be used by adding the SystemdLifecycle target dependencies section like this:

.product(name: "SystemdLifecycle", package: "swift-systemd")

Then, once the product is imported with import SystemdLifecycle, it can be added to a ServiceGroup:

let serviceGroup = ServiceGroup(
    configuration: .init(
        services: [
            .init(service: SystemdService())
        ]
    )
)
try await serviceGroup.run()

SystemdService does not have any dependencies on other services, so it can be constructed and started at any point in the application's service lifecycle.

Description

  • Swift Tools 5.6.0
View More Packages from this Author

Dependencies

Last updated: Sun Nov 17 2024 05:41:22 GMT-1000 (Hawaii-Aleutian Standard Time)