Skip to content
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ To add a dependency on the package, declare it in your `Package.swift`:
and to your application target, add `Lifecycle` to your dependencies:

```swift
.target(name: "MyApplication", dependencies: ["Lifecycle"]),
.target(name: "MyApplication", dependencies: [.product(name: "Lifecycle", package: "swift-service-lifecycle")]),
```

### Defining the lifecycle
Expand Down
16 changes: 13 additions & 3 deletions Sources/Lifecycle/Lifecycle.swift
Original file line number Diff line number Diff line change
Expand Up @@ -177,11 +177,13 @@ extension ServiceLifecycle {
/// - signal: The signal to trap.
/// - handler: closure to invoke when the signal is captured.
/// - returns: a `DispatchSourceSignal` for the given trap. The source must be cancelled by the caller.
public static func trap(signal sig: Signal, handler: @escaping (Signal) -> Void, on queue: DispatchQueue = .global()) -> DispatchSourceSignal {
public static func trap(signal sig: Signal, handler: @escaping (Signal) -> Void, on queue: DispatchQueue = .global(), cancelAfterTrap: Bool = true) -> DispatchSourceSignal {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I prefer we make cancelAfterTrap default to true and change the internal call to set it to false, so the API is nicer to use from a user point of view

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Definitely agree, +1. Only reason was reflex to not affect possible other users of API. Will fix.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I changed default in 8d379dc - missed mentioning it in commit message.

let signalSource = DispatchSource.makeSignalSource(signal: sig.rawValue, queue: queue)
signal(sig.rawValue, SIG_IGN)
signalSource.setEventHandler(handler: {
signalSource.cancel()
if (cancelAfterTrap) {
signalSource.cancel()
}
handler(sig)
})
signalSource.resume()
Expand All @@ -194,6 +196,10 @@ extension ServiceLifecycle {

public static let TERM = Signal(rawValue: SIGTERM)
public static let INT = Signal(rawValue: SIGINT)
public static let USR1 = Signal(rawValue: SIGUSR1)
public static let USR2 = Signal(rawValue: SIGUSR2)
public static let HUP = Signal(rawValue: SIGHUP)

// for testing
internal static let ALRM = Signal(rawValue: SIGALRM)

Expand All @@ -203,7 +209,11 @@ extension ServiceLifecycle {
case Signal.TERM: result += "TERM, "
case Signal.INT: result += "INT, "
case Signal.ALRM: result += "ALRM, "
default: () // ok to ignore
case Signal.USR1: result += "USR1, "
case Signal.USR2: result += "USR2, "
case Signal.HUP: result += "HUP, "

default: () // ok to ignore
}
result += "rawValue: \(self.rawValue))"
return result
Expand Down