Skip to content

setup shutdown hooks on ServiceLifecycle::startAndWait #69

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Aug 9, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 15 additions & 13 deletions Sources/Lifecycle/Lifecycle.swift
Original file line number Diff line number Diff line change
Expand Up @@ -105,25 +105,14 @@ public struct ServiceLifecycle {
/// - parameters:
/// - callback: The handler which is called after the start operation completes. The parameter will be `nil` on success and contain the `Error` otherwise.
public func start(_ callback: @escaping (Error?) -> Void) {
self.configuration.shutdownSignal?.forEach { signal in
self.lifecycle.log("setting up shutdown hook on \(signal)")
let signalSource = ServiceLifecycle.trap(signal: signal, handler: { signal in
self.lifecycle.log("intercepted signal: \(signal)")
self.shutdown()
})
self.lifecycle.shutdownGroup.notify(queue: .global()) {
signalSource.cancel()
}
}
self.setupShutdownHook()
self.lifecycle.start(on: self.configuration.callbackQueue, callback)
}

/// Starts the provided `LifecycleItem` array and waits (blocking) until a shutdown `Signal` is captured or `shutdown` is called on another thread.
/// Startup is performed in the order of items provided.
///
/// - parameters:
/// - configuration: Defines lifecycle `Configuration`
public func startAndWait() throws {
self.setupShutdownHook()
try self.lifecycle.startAndWait(on: self.configuration.callbackQueue)
}

Expand All @@ -140,6 +129,19 @@ public struct ServiceLifecycle {
public func wait() {
self.lifecycle.wait()
}

private func setupShutdownHook() {
self.configuration.shutdownSignal?.forEach { signal in
self.lifecycle.log("setting up shutdown hook on \(signal)")
let signalSource = ServiceLifecycle.trap(signal: signal, handler: { signal in
self.lifecycle.log("intercepted signal: \(signal)")
self.shutdown()
})
self.lifecycle.shutdownGroup.notify(queue: .global()) {
signalSource.cancel()
}
}
}
}

extension ServiceLifecycle {
Expand Down
1 change: 1 addition & 0 deletions Tests/LifecycleTests/ServiceLifecycleTests+XCTest.swift
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ extension ServiceLifecycleTests {
("testStartThenShutdown", testStartThenShutdown),
("testShutdownWithSignal", testShutdownWithSignal),
("testStartAndWait", testStartAndWait),
("testStartAndWaitShutdownWithSignal", testStartAndWaitShutdownWithSignal),
("testBadStartAndWait", testBadStartAndWait),
("testNesting", testNesting),
("testNesting2", testNesting2),
Expand Down
49 changes: 49 additions & 0 deletions Tests/LifecycleTests/ServiceLifecycleTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,55 @@ final class ServiceLifecycleTests: XCTestCase {
XCTAssertEqual(item.state, .shutdown, "expected item to be shutdown")
}

func testStartAndWaitShutdownWithSignal() {
if ProcessInfo.processInfo.environment["SKIP_SIGNAL_TEST"].flatMap(Bool.init) ?? false {
print("skipping testStartAndWaitShutdownWithSignal")
Copy link
Member

Choose a reason for hiding this comment

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

return
}

class Item: LifecycleTask {
private let semaphore: DispatchSemaphore
var state = State.idle

init(_ semaphore: DispatchSemaphore) {
self.semaphore = semaphore
}

var label: String {
return "\(self)"
}

func start(_ callback: (Error?) -> Void) {
self.state = .started
self.semaphore.signal()
callback(nil)
}

func shutdown(_ callback: (Error?) -> Void) {
self.state = .shutdown
callback(nil)
}

enum State {
case idle
case started
case shutdown
}
}

let signal = ServiceLifecycle.Signal.ALRM
let lifecycle = ServiceLifecycle(configuration: .init(shutdownSignal: [signal]))
let semaphore = DispatchSemaphore(value: 0)
DispatchQueue(label: "test").asyncAfter(deadline: .now() + 0.1) {
semaphore.wait()
kill(getpid(), signal.rawValue)
}
let item = Item(semaphore)
lifecycle.register(item)
XCTAssertNoThrow(try lifecycle.startAndWait())
XCTAssertEqual(item.state, .shutdown, "expected item to be shutdown")
}

func testBadStartAndWait() {
class BadItem: LifecycleTask {
var label: String {
Expand Down
2 changes: 1 addition & 1 deletion docker/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ RUN apt-get update && apt-get install -y lsof dnsutils netcat-openbsd net-tools

# ruby and jazzy for docs generation
RUN apt-get update && apt-get install -y ruby ruby-dev libsqlite3-dev
RUN gem install jazzy --no-ri --no-rdoc
RUN if [ "${ubuntu_version}" != "xenial" ] ; then gem install jazzy --no-ri --no-rdoc ; fi
Copy link
Contributor Author

Choose a reason for hiding this comment

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

unrelated jazzy issue on ubuntu 16.04, but required for CI to pass


# tools
RUN mkdir -p $HOME/.tools
Expand Down
4 changes: 3 additions & 1 deletion docker/docker-compose.1804.50.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ services:

test:
image: swift-service-lifecycle:18.04-5.0

environment:
- SKIP_SIGNAL_TEST=true

shell:
image: swift-service-lifecycle:18.04-5.0
2 changes: 2 additions & 0 deletions docker/docker-compose.1804.52.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ services:

test:
image: swift-service-lifecycle:18.04-5.2
environment:
- SKIP_SIGNAL_TEST=true

shell:
image: swift-service-lifecycle:18.04-5.2
2 changes: 2 additions & 0 deletions docker/docker-compose.1804.53.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ services:

test:
image: swift-service-lifecycle:18.04-5.3
environment:
- SKIP_SIGNAL_TEST=true

shell:
image: swift-service-lifecycle:18.04-5.3