witchcraft_server/
lib.rs

1// Copyright 2022 Palantir Technologies, Inc.
2//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6//
7// http://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14//! A highly opinionated embedded application server for RESTy APIs.
15//!
16//! # Initialization
17//!
18//! The entrypoint of a Witchcraft server is an initialization function annotated with `#[witchcraft_server::main]`:
19//!
20//! ```ignore
21//! use conjure_error::Error;
22//! use refreshable::Refreshable;
23//! use witchcraft_server::config::install::InstallConfig;
24//! use witchcraft_server::config::runtime::RuntimeConfig;
25//!
26//! #[witchcraft_server::main]
27//! fn main(
28//!     install: InstallConfig,
29//!     runtime: Refreshable<RuntimeConfig>,
30//!     wc: &mut Witchcraft,
31//! ) -> Result<(), Error> {
32//!     wc.api(CustomApiEndpoints::new(CustomApiResource));
33//!
34//!     Ok(())
35//! }
36//! ```
37//!
38//! The function is provided with the server's install and runtime configuration, as well as the [`Witchcraft`] object
39//! which can be used to configure the server. Once the initialization function returns, the server will start.
40//!
41//! ## Note
42//!
43//! The initialization function is expected to return quickly - any long-running work required should happen in the
44//! background.
45//!
46//! # Configuration
47//!
48//! Witchcraft divides configuration into two categories:
49//!
50//! * *Install* - Configuration that is fixed for the lifetime of a service. For example, the port that the server
51//!   listens on is part of the server's install configuration.
52//! * *Runtime* - Configuration that can dynamically update while the service is running. For example, the logging
53//!   verbosity level is part of the server's runtime configuration.
54//!
55//! Configuration is loaded from the `var/conf/install.yml` and `var/conf/runtime.yml` files respectively. The
56//! `runtime.yml` file is automatically checked for updates every few seconds.
57//!
58//! ## Extension
59//!
60//! The configuration files are deserialized into Rust types via the [`serde::Deserialize`] trait. `witchcraft-server`'s
61//! own internal configuration is represented by the [`InstallConfig`] and [`RuntimeConfig`] types. Services that need
62//! their own configuration should embed the Witchcraft configuration within their own using the `#[serde(flatten)]`
63//! annotation and implement the [`AsRef`] trait:
64//!
65//! ```
66//! use serde::Deserialize;
67//! use witchcraft_server::config::install::InstallConfig;
68//!
69//! #[derive(Deserialize)]
70//! #[serde(rename_all = "kebab-case")]
71//! struct MyInstallConfig {
72//!     shave_yaks: bool,
73//!     #[serde(flatten)]
74//!     base: InstallConfig,
75//! }
76//!
77//! impl AsRef<InstallConfig> for MyInstallConfig {
78//!     fn as_ref(&self) -> &InstallConfig {
79//!         &self.base
80//!     }
81//! }
82//! ```
83//!
84//! The service's custom configuration will then sit next to the standard Witchcraft configuration:
85//!
86//! ```yml
87//! product-name: my-service
88//! product-version: 1.0.0
89//! port: 12345
90//! shave-yaks: true
91//! ```
92//!
93//! ## Sensitive values
94//!
95//! The server's configuration deserializer supports two methods to handle sensitive values:
96//!
97//! * `${enc:5BBfGvf90H6bApwfx...}` - inline in an encrypted form using [`serde_encrypted_value`] with the
98//!   key stored in `var/conf/encrypted-config-value.key`.
99//! * `${file:/mnt/secrets/foo}` - as a reference to a file containing the value using [`serde_file_value`].
100//!
101//! ## Refreshable runtime configuration
102//!
103//! The server's runtime configuration is wrapped in the [`Refreshable`] type to allow code to properly handle updates
104//! to the configuration. Depending on the use case, implementations can use the [`Refreshable::get`] to retrieve the
105//! current state of the configuration when needed or the [`Refreshable::subscribe`] method to be notified of changes
106//! to the configuration as they happen. See the documentation of the [`refreshable`] crate for more details.
107//!
108//! # HTTP APIs
109//!
110//! The server supports HTTP endpoints implementing the [`Service`] and [`AsyncService`]
111//! traits. These implementations can be generated from a [Conjure] YML [definition] with the [`conjure-codegen`] crate.
112//!
113//! While we strongly encourage the use of Conjure-generated APIs, some services may need to expose endpoints that can't
114//! be defined in Conjure. The [`conjure_http::conjure_endpoints`] macro can be used to define arbitrary HTTP endpoints.
115//!
116//! API endpoints should normally be registered with the [`Witchcraft::api`] and [`Witchcraft::blocking_api`] methods,
117//! which will place the endpoints under the `/api` route. If necessary, the [`Witchcraft::app`] and
118//! [`Witchcraft::blocking_app`] methods can be used to place the endpoints directly at the root route instead.
119//!
120//! [`Service`]: conjure_http::server::Service
121//! [Conjure]: https://github.com/palantir/conjure
122//! [definition]: https://palantir.github.io/conjure/#/docs/spec/conjure_definitions
123//! [`conjure-codegen`]: https://docs.rs/conjure-codegen
124//!
125//! # HTTP clients
126//!
127//! Remote services are configured in the `service-discovery` section of the runtime configuration, and clients can be
128//! created from the [`ClientFactory`] returned by the [`Witchcraft::client_factory`] method. The clients will
129//! automatically update based on changes to the runtime configuration. See the documentation of the [`conjure_runtime`]
130//! crate for more details.
131//!
132//! # Status endpoints
133//!
134//! The server exposes several "status" endpoints to report various aspects of the server.
135//!
136//! ## Liveness
137//!
138//! The `/status/liveness` endpoint returns a successful response to all requests, indicating that the server is alive.
139//!
140//! ## Readiness
141//!
142//! The `/status/readiness` endpoint returns a response indicating the server's readiness to handle requests to its
143//! endpoints. Deployment infrastructure uses the result of this endpoint to decide if requests should be routed to a
144//! given instance of the service. Custom readiness checks can be added to the server via the [`ReadinessCheckRegistry`]
145//! returned by the [`Witchcraft::readiness_checks`] method. Any long-running initialization logic should happen
146//! asynchronously and use a readiness check to indicate completion.
147//!
148//! ## Health
149//!
150//! The `/status/health` endpoint returns a response indicating the server's overall health. Deployment infrastructure
151//! uses the result of this endpoint to trigger alerts. Custom health checks can be added to the server via the
152//! [`HealthCheckRegistry`] returned by the [`Witchcraft::health_checks`] method. Requests to this endpoint must be
153//! authenticated with the `health-checks.shared-secret` bearer token in runtime configuration.
154//!
155//! The server registers several built-in health checks:
156//!
157//! * `CONFIG_RELOAD` - Reports an error state if the runtime configuration failed to reload properly.
158//! * `ENDPOINT_FIVE_HUNDREDS` - Reports a warning if an endpoint has a high rate of `500 Internal Server Error`
159//!   responses.
160//! * `SERVICE_DEPENDENCY` - Tracks the status of requests made with HTTP clients created via the server's client
161//!   factory, and reports a warning state of requests to a remote service have a high failure rate.
162//! * `PANICS` - Reports a warning if the server has panicked at any point.
163//!
164//! # Diagnostics
165//!
166//! The `/debug/diagnostic/{diagnosticType}` endpoint returns diagnostic information. Requests to this endpoint must be
167//! authenticated with the `diagnostics.debug-shared-secret` bearer token in the runtime configuration.
168//!
169//! Several diagnostic types are defined:
170//!
171//! * `diagnostic.types.v1` - Returns a JSON-encoded list of all valid diagnostic types.
172//! * `rust.heap.status.v1` - Returns detailed statistics about the state of the heap. Requires the `jemalloc` feature
173//!   (enabled by default).
174//! * `rust.heap.profile.v1` - Returns a profile of the source of a sample of live allocations. Use the `jeprof` tool
175//!   to analyze the profile. Requires the `jemalloc` feature (enabled by default).
176//! * `metric.names.v1` - Returns a JSON-encoded list of the names of all metrics registered with the server.
177//! * `rust.thread.dump.v1` - Returns a stack trace of every thread in the process. Only supported when running on
178//!   Linux.
179//!
180//! # Logging
181//!
182//! `witchcraft-server` emits JSON-encoded logs following the [witchcraft-api spec]. By default, logs will be written to
183//! a file in `var/log` corresponding to the type of log message (`service.log`, `request.log`, etc). These files are
184//! automatically rotated and compressed based on a non-configurable policy. If running in a Docker container or if the
185//! `use-console-log` setting is enabled in the install configuration, logs will instead be written to standard out.
186//!
187//! [witchcraft-api spec]: https://github.com/palantir/witchcraft-api
188//!
189//! ## Service
190//!
191//! The service log contains the messages emitted by invocations of the macros in the [`witchcraft_log`] crate. Messages
192//! emitted by the standard Rust [`log`] crate are additionally captured, but code that is written as part of a
193//! Witchcraft service should use [`witchcraft_log`] instead for better integration. See the documentation of that crate
194//! for more details.
195//!
196//! ## Request
197//!
198//! The request log records an entry for each HTTP request processed by the server. Parameters marked marked as safe by
199//! an endpoint's Conjure definition will be included as parameters in the log record.
200//!
201//! ## Trace
202//!
203//! The trace log records [Zipkin]-style trace spans. The server automatically creates spans for each incoming HTTP
204//! request based off of request's propagation metadata. Traces that have not alread had a sampling decision made will
205//! be sampled at the rate specified by the `logging.trace-rate` field in the server's runtime configuration, which
206//! defaults to 0.005%. Server logic can create additional spans with the [`zipkin`] crate. See the documentation of
207//! that crate for more details.
208//!
209//! [Zipkin]: https://zipkin.io/
210//!
211//! ## Metric
212//!
213//! The metric log contains the values of metrics reporting the state of various components of the server. Metrics are
214//! recorded every 30 seconds. Server logic can create additional metrics with the [`MetricRegistry`] returned by the
215//! [`Witchcraft::metrics`] method. See the documentation of the [`witchcraft_metrics`] crate for more details.
216//!
217//! # Metrics
218//!
219//! The server reports a variety of metrics by default:
220//!
221//! ## Thread Pool
222//!
223//! * `server.worker.max` (gauge) - The configured maximum size of the server's thread pool used for requests to
224//!   blocking endpoints.
225//! * `server.worker.active` (gauge) - The number of threads actively processing requests to blocking endpoints.
226//! * `server.worker.utilization-max` (gauge) - `server.worker.active` divided by `server.worker.max`. If this is 1, the
227//!   server will immediately reject calls to blocking endpoints with a `503 Service Unavailable` status code.
228//!
229//! ## Logging
230//!
231//! * `logging.queue (type: <log_type>)` (gauge) - The number of log messages queued for output.
232//!
233//! ## Process
234//!
235//! * `process.heap` (gauge) - The total number of bytes allocated from the heap. Requires the `jemalloc` feature
236//!   (enabled by default).
237//! * `process.heap.active` (gauge) - The total number of bytes in active pages. Requires the `jemalloc` feature
238//!   (enabled by default).
239//! * `process.heap.resident` (gauge) - The total number of bytes in physically resident pages. Requires the `jemalloc` feature
240//!   (enabled by default).
241//! * `process.uptime` (gauge) - The number of microseconds that have elapsed since the server started.
242//! * `process.panics` (counter) - The number of times the server has panicked.
243//! * `process.user-time` (gauge) - The number of microseconds the process has spent running in user-space.
244//! * `process.user-time.norm` (gauge) - `process.user-time` divided by the number of CPU cores.
245//! * `process.system-time` (gauge) - The number of microseconds the process has spent either running in kernel-space
246//!   or in uninterruptable IO wait.
247//! * `process.system-time.norm` (gauge) - `process.system-time` divided by the number of CPU cores.
248//! * `process.blocks-read` (gauge) - The number of filesystem blocks the server has read.
249//! * `process.blocks-written` (gauge) - The number of filesystem blocks the server has written.
250//! * `process.threads` (gauge) - The number of threads in the process.
251//! * `process.filedescriptor` (gauge) - The number of file descriptors held open by the process divided by the maximum
252//!   number of files the server may hold open.
253//!
254//! ## Connection
255//!
256//! * `server.connection.active` (counter) - The number of TCP sockets currently connected to the HTTP server.
257//! * `server.connection.utilization` (gauge) - `server.connection.active` divided by the maximum number of connections
258//!   the server will accept.
259//!
260//! ## TLS
261//!
262//! * `tls.handshake (context: server, protocol: <protocol>, cipher: <cipher>)` (meter) - The rate of TLS handshakes
263//!   completed by the HTTP server.
264//!
265//! ## Server
266//!
267//! * `server.request.active` (counter) - The number of requests being actively processed.
268//! * `server.request.unmatched` (meter) - The rate of `404 Not Found` responses returned by the server.
269//! * `server.response.all` (meter) - The rate of responses returned by the server.
270//! * `server.response.1xx` (meter) - The rate of `1xx` responses returned by the server.
271//! * `server.response.2xx` (meter) - The rate of `2xx` responses returned by the server.
272//! * `server.response.3xx` (meter) - The rate of `3xx` responses returned by the server.
273//! * `server.response.4xx` (meter) - The rate of `4xx` responses returned by the server.
274//! * `server.response.5xx` (meter) - The rate of `5xx` responses returned by the server.
275//! * `server.response.500` (meter) - The rate of `500 Internal Server Error` responses returned by the server.
276//!
277//! ## Endpoints
278//!
279//! * `server.response (service-name: <service_name>, endpoint: <endpoint>)` (timer) - The amount of time required to
280//!   process each request to the endpoint, including sending the entire response body.
281//! * `server.response.error (service-name: <service_name>, endpoint: <endpoint>)` (meter) - The rate of `5xx` errors
282//!   returned for requests to the endpoint.
283//!
284//! ## HTTP clients
285//!
286//! See the documentation of the [`conjure_runtime`] crate for the metrics reported by HTTP clients.
287#![warn(missing_docs)]
288
289use std::env;
290use std::path::Path;
291use std::pin::pin;
292use std::process;
293use std::sync::atomic::{AtomicBool, AtomicUsize, Ordering};
294use std::sync::Arc;
295use std::time::Duration;
296
297use conjure_error::Error;
298use conjure_http::server::{AsyncService, ConjureRuntime};
299use conjure_runtime::{Agent, ClientFactory, HostMetricsRegistry, UserAgent};
300use debug::endpoint::DebugResource;
301use debug::endpoint::DebugServiceEndpoints;
302#[cfg(feature = "jemalloc")]
303use debug::heap_profile::{self, HeapProfileDiagnostic};
304use futures_util::{stream, Stream, StreamExt};
305use refreshable::Refreshable;
306use serde::de::DeserializeOwned;
307use status::StatusResource;
308use status::StatusServiceEndpoints;
309use tokio::runtime::{Handle, Runtime};
310use tokio::signal::unix::{self, SignalKind};
311use tokio::{runtime, select, time};
312use witchcraft_log::{error, fatal, info};
313use witchcraft_metrics::MetricRegistry;
314
315pub use body::{RequestBody, ResponseWriter};
316use config::install::InstallConfig;
317use config::runtime::RuntimeConfig;
318pub use witchcraft::Witchcraft;
319#[doc(inline)]
320pub use witchcraft_server_config as config;
321#[doc(inline)]
322pub use witchcraft_server_macros::main;
323
324use crate::debug::diagnostic_types::DiagnosticTypesDiagnostic;
325#[cfg(feature = "jemalloc")]
326use crate::debug::heap_stats::HeapStatsDiagnostic;
327use crate::debug::metric_names::MetricNamesDiagnostic;
328#[cfg(target_os = "linux")]
329use crate::debug::thread_dump::ThreadDumpDiagnostic;
330use crate::debug::DiagnosticRegistry;
331use crate::health::config_reload::ConfigReloadHealthCheck;
332use crate::health::endpoint_500s::Endpoint500sHealthCheck;
333use crate::health::minidump::MinidumpHealthCheck;
334use crate::health::panics::PanicsHealthCheck;
335use crate::health::service_dependency::ServiceDependencyHealthCheck;
336use crate::health::HealthCheckRegistry;
337use crate::readiness::ReadinessCheckRegistry;
338use crate::server::Listener;
339use crate::shutdown_hooks::ShutdownHooks;
340
341pub mod blocking;
342mod body;
343mod configs;
344pub mod debug;
345mod endpoint;
346pub mod extensions;
347pub mod health;
348pub mod logging;
349mod metrics;
350mod minidump;
351pub mod readiness;
352mod server;
353mod service;
354mod shutdown_hooks;
355mod status;
356pub mod tls;
357mod witchcraft;
358
359#[cfg(feature = "jemalloc")]
360#[global_allocator]
361static ALLOC: tikv_jemallocator::Jemalloc = tikv_jemallocator::Jemalloc;
362
363/// Initializes a Witchcraft server.
364///
365/// `init` is invoked with the parsed install and runtime configs as well as the [`Witchcraft`] context object. It
366/// is expected to return quickly; any long running initialization should be spawned off into the background to run
367/// asynchronously.
368pub fn init<I, R, F>(init: F)
369where
370    I: AsRef<InstallConfig> + DeserializeOwned,
371    R: AsRef<RuntimeConfig> + DeserializeOwned + PartialEq + 'static + Sync + Send,
372    F: FnOnce(I, Refreshable<R, Error>, &mut Witchcraft) -> Result<(), Error>,
373{
374    init_with_configs(init, configs::load_install::<I>, configs::load_runtime::<R>)
375}
376
377/// Initializes a Witchcraft server with custom config loaders.
378///
379/// `init` is invoked with the install and runtime configs from the provided loaders as well as the [`Witchcraft`]
380/// context object. It is expected to return quickly; any long running initialization should be spawned off into
381/// the background to run asynchronously.
382pub fn init_with_configs<I, R, F, LI, LR>(init: F, load_install: LI, load_runtime: LR)
383where
384    I: AsRef<InstallConfig> + DeserializeOwned,
385    R: AsRef<RuntimeConfig> + DeserializeOwned + PartialEq + 'static + Sync + Send,
386    F: FnOnce(I, Refreshable<R, Error>, &mut Witchcraft) -> Result<(), Error>,
387    LI: FnOnce() -> Result<I, Error>,
388    LR: FnOnce(&Handle, &Arc<AtomicBool>) -> Result<Refreshable<R, Error>, Error>,
389{
390    let mut runtime_guard = None;
391
392    let ret = match init_inner(init, load_install, load_runtime, &mut runtime_guard) {
393        Ok(()) => 0,
394        Err(e) => {
395            fatal!("error starting server", error: e);
396            1
397        }
398    };
399    drop(runtime_guard);
400
401    process::exit(ret);
402}
403
404fn init_inner<I, R, F, LI, LR>(
405    init: F,
406    load_install: LI,
407    load_runtime: LR,
408    runtime_guard: &mut Option<RuntimeGuard>,
409) -> Result<(), Error>
410where
411    I: AsRef<InstallConfig> + DeserializeOwned,
412    R: AsRef<RuntimeConfig> + DeserializeOwned + PartialEq + 'static + Sync + Send,
413    F: FnOnce(I, Refreshable<R, Error>, &mut Witchcraft) -> Result<(), Error>,
414    LI: FnOnce() -> Result<I, Error>,
415    LR: FnOnce(&Handle, &Arc<AtomicBool>) -> Result<Refreshable<R, Error>, Error>,
416{
417    logging::early_init();
418
419    let args = env::args_os().collect::<Vec<_>>();
420    if args.len() == 3 && args[1] == "minidump" {
421        return minidump::server(Path::new(&args[2]));
422    }
423
424    let install_config = load_install()?;
425
426    let thread_id = AtomicUsize::new(0);
427    let runtime = runtime::Builder::new_multi_thread()
428        .enable_all()
429        .thread_name_fn(move || format!("runtime-{}", thread_id.fetch_add(1, Ordering::Relaxed)))
430        .worker_threads(install_config.as_ref().server().io_threads())
431        .thread_keep_alive(install_config.as_ref().server().idle_thread_timeout())
432        .build()
433        .map_err(Error::internal_safe)?;
434
435    let handle = runtime.handle().clone();
436    let runtime = runtime_guard.insert(RuntimeGuard {
437        runtime: Some(runtime),
438        logger_shutdown: Some(ShutdownHooks::new()),
439    });
440
441    let runtime_config_ok = Arc::new(AtomicBool::new(true));
442    let runtime_config = load_runtime(&handle, &runtime_config_ok)?;
443
444    let metrics = Arc::new(MetricRegistry::new());
445    let host_metrics = Arc::new(HostMetricsRegistry::new());
446    let health_checks = Arc::new(HealthCheckRegistry::new(&handle));
447    let readiness_checks = Arc::new(ReadinessCheckRegistry::new());
448    let diagnostics = Arc::new(DiagnosticRegistry::new());
449
450    let loggers = handle.block_on(logging::init(
451        &metrics,
452        install_config.as_ref(),
453        &runtime_config.map(|c| c.as_ref().logging().clone()),
454        runtime.logger_shutdown.as_mut().unwrap(),
455    ))?;
456
457    info!("server starting");
458
459    if install_config.as_ref().minidump().enabled() {
460        let minidump_ok = Arc::new(AtomicBool::new(true));
461        let socket_dir = install_config.as_ref().minidump().socket_dir();
462        handle.spawn({
463            let minidump_ok = minidump_ok.clone();
464            let socket_dir = socket_dir.to_owned();
465            async move {
466                let result = minidump::init(&socket_dir).await;
467                minidump_ok.store(result.is_ok(), Ordering::Relaxed);
468                if let Err(e) = result {
469                    error!("error during minidump init", error: e)
470                }
471            }
472        });
473
474        health_checks.register(MinidumpHealthCheck::new(minidump_ok));
475        #[cfg(target_os = "linux")]
476        diagnostics.register(ThreadDumpDiagnostic::new(socket_dir));
477    }
478
479    metrics::init(&metrics);
480
481    health_checks.register(ServiceDependencyHealthCheck::new(&host_metrics));
482    health_checks.register(PanicsHealthCheck::new());
483    health_checks.register(ConfigReloadHealthCheck::new(runtime_config_ok));
484
485    diagnostics.register(MetricNamesDiagnostic::new(&metrics));
486    #[cfg(feature = "jemalloc")]
487    {
488        diagnostics.register(HeapStatsDiagnostic);
489        heap_profile::init(&runtime_config);
490        diagnostics.register(HeapProfileDiagnostic);
491    }
492    diagnostics.register(DiagnosticTypesDiagnostic::new(Arc::downgrade(&diagnostics)));
493
494    let client_factory = ClientFactory::builder()
495        .config(runtime_config.map(|c| c.as_ref().service_discovery().clone()))
496        .user_agent(UserAgent::new(Agent::new(
497            install_config.as_ref().product_name(),
498            install_config.as_ref().product_version(),
499        )))
500        .metrics(metrics.clone())
501        .host_metrics(host_metrics)
502        .blocking_handle(handle.clone());
503
504    let mut witchcraft = Witchcraft {
505        metrics,
506        health_checks,
507        readiness_checks,
508        client_factory,
509        diagnostics: diagnostics.clone(),
510        handle: handle.clone(),
511        install_config: install_config.as_ref().clone(),
512        thread_pool: None,
513        endpoints: vec![],
514        shutdown_hooks: ShutdownHooks::new(),
515        conjure_runtime: Arc::new(ConjureRuntime::new()),
516    };
517
518    let status_endpoints = StatusServiceEndpoints::new(StatusResource::new(
519        &runtime_config,
520        &witchcraft.health_checks,
521        &witchcraft.readiness_checks,
522    ));
523    witchcraft.endpoints(
524        None,
525        status_endpoints.endpoints(&witchcraft.conjure_runtime),
526        false,
527    );
528
529    let debug_endpoints =
530        DebugServiceEndpoints::new(DebugResource::new(&runtime_config, &diagnostics));
531    witchcraft.app(debug_endpoints);
532
533    // server::start clears out the previously-registered endpoints so the existing Witchcraft
534    // is ready to reuse for the main port afterwards.
535    if let Some(management_port) = install_config.as_ref().management_port() {
536        if management_port != install_config.as_ref().port() {
537            handle.block_on(server::start(
538                &mut witchcraft,
539                &loggers,
540                Listener::Management,
541                management_port,
542            ))?;
543        }
544    }
545
546    init(install_config, runtime_config, &mut witchcraft)?;
547
548    witchcraft
549        .health_checks
550        .register(Endpoint500sHealthCheck::new(&witchcraft.endpoints));
551
552    let port = witchcraft.install_config.port();
553    handle.block_on(server::start(
554        &mut witchcraft,
555        &loggers,
556        Listener::Service,
557        port,
558    ))?;
559
560    handle.block_on(shutdown(
561        witchcraft.shutdown_hooks,
562        witchcraft.install_config.server().shutdown_timeout(),
563    ))
564}
565
566async fn shutdown(shutdown_hooks: ShutdownHooks, timeout: Duration) -> Result<(), Error> {
567    let mut signals = pin!(signals()?);
568
569    signals.next().await;
570    info!("server shutting down");
571
572    select! {
573        _ = shutdown_hooks => {}
574        _ = signals.next() => info!("graceful shutdown interrupted by signal"),
575        _ = time::sleep(timeout) => {
576            info!(
577                "graceful shutdown timed out",
578                safe: {
579                    timeout: format_args!("{timeout:?}"),
580                },
581            );
582        }
583    }
584
585    Ok(())
586}
587
588fn signals() -> Result<impl Stream<Item = ()>, Error> {
589    let sigint = signal(SignalKind::interrupt())?;
590    let sigterm = signal(SignalKind::terminate())?;
591    Ok(stream::select(sigint, sigterm))
592}
593
594fn signal(kind: SignalKind) -> Result<impl Stream<Item = ()>, Error> {
595    let mut signal = unix::signal(kind).map_err(Error::internal_safe)?;
596    Ok(stream::poll_fn(move |cx| signal.poll_recv(cx)))
597}
598
599struct RuntimeGuard {
600    runtime: Option<Runtime>,
601    logger_shutdown: Option<ShutdownHooks>,
602}
603
604impl Drop for RuntimeGuard {
605    fn drop(&mut self) {
606        let runtime = self.runtime.take().unwrap();
607        runtime.block_on(self.logger_shutdown.take().unwrap());
608        runtime.shutdown_background()
609    }
610}