Swift.org – Introducing Swift Service Lifecycle

栏目: IT技术 · 发布时间: 4年前

内容简介:It is my pleasure to announce a new open source project for the Swift server ecosystem,Most services have startup and shutdown workflow-logic which is often sensitive to failure and hard to get right. Startup sequences include actions like initializing thr

Introducing Swift Service Lifecycle

Tom Doron

Tom Doron is a member of the Swift Core Team and the Swift Server Work Group. He manages a team working on server-side Swift libraries at Apple.

It is my pleasure to announce a new open source project for the Swift server ecosystem, Swift Service Lifecycle . Service Lifecycle is a Swift package designed to help server applications, also known as services, manage their startup and shutdown sequences.

What is it?

Most services have startup and shutdown workflow-logic which is often sensitive to failure and hard to get right. Startup sequences include actions like initializing thread pools, running data migrations, warming up caches, and other forms of state initialization before taking traffic or accepting events. Shutdown sequences include freeing up resources that hold on to file descriptors or other system resources that may leak if not cleared correctly.

Today, server applications and frameworks must find ways to address the need on their own, which could be error prone. To make things safer and easier, Service Lifecycle codifies this common need in a safe, reusable and framework-agnostic way. It is designed to be integrated with any server framework or directly in a server application’s main .

How does it work?

The recommended way of using this library is creating a ServiceLifecycle instance in your server application’s main , and register LifecycleTasks with it. Upon calling the start function, ServiceLifecycle will start these tasks in the order they were registered.

By default, ServiceLifecycle also registers a Signal handler that traps INT and TERM , which are typical Signal s used in modern deployment platforms to communicate shutdown request. The shutdown sequence begins once the Signal is captured, and the LifecycleTasks are shut down in the reverse order they have been registered in.

Example

// Import the package.
import Lifecycle

// Initialize the `Lifecycle` container.
var lifecycle = ServiceLifecycle()

// Register a resource that should be shut down when the application exits.
//
// In this case, we are registering a SwiftNIO `EventLoopGroup`
// and passing its `syncShutdownGracefully` function to be called on shutdown.
let eventLoopGroup = MultiThreadedEventLoopGroup(numberOfThreads: System.coreCount)
lifecycle.registerShutdown(
    name: "eventLoopGroup",
    .sync(eventLoopGroup.syncShutdownGracefully)
)

// Register another resource that should be started when the application starts
// and shut down when the application exits.
//
// In this case, we are registering a contrived `DatabaseMigrator`
// and passing its `migrate` function to be called on startup
// and `shutdown` function to be called on shutdown.
let migrator = DatabaseMigrator(eventLoopGroup: eventLoopGroup)
lifecycle.register(
    name: "migrator",
    start: .async(migrator.migrate),
    shutdown: .async(migrator.shutdown)
)

// Start the application.
//
// Start handlers passed using the `register` function
// will be called in the order they were registered in.
lifecycle.start() { error in
    // start completion handler
    // if an startup error occurred you can capture it here
    if let error = error {
        logger.error("failed starting \(self) ☠️: \(error)")
    } else {
        logger.info("\(self) started successfully :rocket:")
    }
}

// Wait for the application to exit
//
// This is a blocking operation that typically waits for a `Signal`.
// The `Signal` can be configured at `lifecycle.init`, and defaults to `INT` and `TERM`.
// Shutdown handlers passed using the `register` or `registerShutdown` functions
// will be called in the reverse order they were registered in.
lifecycle.wait()

Getting Involved

If you are interested in Service Lifecycle, come and get involved!

The source is available , and we encourage contributions from the open source community. If you have feedback, questions or would like to discuss the project, please feel free to chat on theSwift forums. If you would like to report bugs, please use the GitHub issue tracker . We look forward to working with you, and helping move the industry forward to a better, safer programming future.


以上所述就是小编给大家介绍的《Swift.org – Introducing Swift Service Lifecycle》,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对 码农网 的支持!

查看所有标签

猜你喜欢:

本站部分资源来源于网络,本站转载出于传递更多信息之目的,版权归原作者或者来源机构所有,如转载稿涉及版权问题,请联系我们

叠加体验:用互联网思维设计商业模式

叠加体验:用互联网思维设计商业模式

穆胜 / 机械工业出版社 / 2014-11 / 39.00

本书在互联网思维改变一切的背景下,详细介绍了如何运用互联网思维重构商业模式,主要包括以下内容:①互联网经济中的商业逻辑(即“互联网思维”),不仅给出了消费方面的逻辑变革,还给出了在生产端的逻辑变革以及“跨界”的逻辑变革。②给出了一个“三层产品体验模型”,厘清了互联网思维,打造完美终端、云端服务和价值群落三层体验,企业可以选择做不同层面的体验组合,这即是选择了不同的市场策略。但是,企业要基业长青,终......一起来看看 《叠加体验:用互联网思维设计商业模式》 这本书的介绍吧!

图片转BASE64编码
图片转BASE64编码

在线图片转Base64编码工具

RGB CMYK 转换工具
RGB CMYK 转换工具

RGB CMYK 互转工具