Skip to content
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

Use ComponentLifecycle in HBApplication #142

Merged
merged 3 commits into from
Oct 1, 2022
Merged
Show file tree
Hide file tree
Changes from 2 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
26 changes: 21 additions & 5 deletions Sources/Hummingbird/Application.swift
Original file line number Diff line number Diff line change
Expand Up @@ -69,13 +69,13 @@ public final class HBApplication: HBExtensible {
/// Initialize new Application
public init(
configuration: HBApplication.Configuration = HBApplication.Configuration(),
eventLoopGroupProvider: NIOEventLoopGroupProvider = .createNew
eventLoopGroupProvider: NIOEventLoopGroupProvider = .createNew,
serviceLifecycleProvider: ServiceLifecycleProvider = .createNew
) {
var logger = Logger(label: configuration.serverName ?? "HummingBird")
logger.logLevel = configuration.logLevel
self.logger = logger

self.lifecycle = ServiceLifecycle(configuration: .init(logger: self.logger))
self.middleware = HBMiddlewareGroup()
self.router = TrieRouter()
self.configuration = configuration
Expand All @@ -95,6 +95,22 @@ public final class HBApplication: HBExtensible {
case .shared(let elg):
self.eventLoopGroup = elg
}

// create lifecycle
let lifecycle: LifecycleTasksContainer
slashmo marked this conversation as resolved.
Show resolved Hide resolved

switch serviceLifecycleProvider {
case .shared(let parentLifecycle):
self.lifecycle = parentLifecycle
let componentLifecycle = ComponentLifecycle(label: self.logger.label, logger: self.logger)
lifecycle = componentLifecycle
self.lifecycle.register(componentLifecycle)
case .createNew:
let serviceLifecycle = ServiceLifecycle(configuration: .init(logger: self.logger))
lifecycle = serviceLifecycle
self.lifecycle = serviceLifecycle
}

self.threadPool = NIOThreadPool(numberOfThreads: configuration.threadPoolSize)
self.threadPool.start()

Expand All @@ -103,17 +119,17 @@ public final class HBApplication: HBExtensible {
self.addEventLoopStorage()

// register application shutdown with lifecycle
self.lifecycle.registerShutdown(
lifecycle.registerShutdown(
label: "Application", .sync(self.shutdownApplication)
)

self.lifecycle.registerShutdown(
lifecycle.registerShutdown(
label: "DateCache", .eventLoopFuture { HBDateCache.shutdownDateCaches(eventLoopGroup: self.eventLoopGroup) }
)

// register server startup and shutdown with lifecycle
if !configuration.noHTTPServer {
self.lifecycle.register(
lifecycle.register(
label: "HTTP Server",
start: .eventLoopFuture { self.server.start(responder: HTTPResponder(application: self)) },
shutdown: .eventLoopFuture(self.server.stop)
Expand Down
24 changes: 24 additions & 0 deletions Sources/Hummingbird/Lifecycle/ServiceLifecycleProvider.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
//===----------------------------------------------------------------------===//
//
// This source file is part of the Hummingbird server framework project
//
// Copyright (c) 2021-2021 the Hummingbird authors
slashmo marked this conversation as resolved.
Show resolved Hide resolved
// Licensed under Apache License v2.0
//
// See LICENSE.txt for license information
// See hummingbird/CONTRIBUTORS.txt for the list of Hummingbird authors
//
// SPDX-License-Identifier: Apache-2.0
//
//===----------------------------------------------------------------------===//

import Lifecycle

public enum ServiceLifecycleProvider {
/// Use a `ServiceLifecycle` provided by the user
/// and run `HBApplication` tasks in a `ComponentLifecycle`.
case shared(ServiceLifecycle)

/// Create a new `ServiceLifecycle`.
case createNew
}