Skip to main content

flare_core_runtime/
lib.rs

1//! Service runtime and lifecycle orchestration for Flare services.
2//!
3//! `flare-core-runtime` manages service tasks, dependency ordering, health
4//! checks, shutdown signals, plugin hooks, metrics collection, middleware, and
5//! state tracking. It is transport-neutral and is used by HTTP, gRPC, MQ, and
6//! custom workers.
7//!
8//! # 核心特性
9//!
10//! - Unified service startup for HTTP, gRPC, MQ consumers, and custom tasks.
11//! - Graceful shutdown with multiple signal sources and dependency ordering.
12//! - Service orchestration with task dependencies, health checks, and state
13//!   tracking.
14//! - Extensible plugin hooks, middleware chains, and custom adapters.
15//!
16//! # Architecture
17//!
18//! `flare-core-runtime` provides transport-neutral traits, configuration, and
19//! orchestration primitives. Concrete adapters live in sibling crates:
20//!
21//! - `flare-core-transport` implements HTTP and gRPC adapters.
22//! - `flare-core-messaging` implements MQ consumer adapters.
23//!
24//! # Example
25//!
26//! ```rust,no_run
27//! use flare_core_runtime::{ServiceRuntime, Task, TaskResult};
28//! use std::pin::Pin;
29//! use std::future::Future;
30//!
31//! // Define a custom task.
32//! struct MyTask {
33//!     name: String,
34//! }
35//!
36//! impl Task for MyTask {
37//!     fn name(&self) -> &str {
38//!         &self.name
39//!     }
40//!
41//!     fn run(
42//!         self: Box<Self>,
43//!         shutdown_rx: tokio::sync::oneshot::Receiver<()>,
44//!     ) -> Pin<Box<dyn Future<Output = TaskResult> + Send>> {
45//!         Box::pin(async move {
46//!             // Task logic.
47//!             Ok(())
48//!         })
49//!     }
50//! }
51//!
52//! #[tokio::main]
53//! async fn main() -> anyhow::Result<()> {
54//!     // Create the runtime.
55//!     let runtime = ServiceRuntime::new("my-service")
56//!         .add_task(Box::new(MyTask { name: "task-1".to_string() }));
57//!
58//!     // Run until shutdown.
59//!     runtime.run().await?;
60//!     Ok(())
61//! }
62//! ```
63
64// Module declarations.
65pub mod config;
66pub mod error;
67pub mod health;
68pub mod metrics;
69pub mod middleware;
70pub mod plugin;
71pub mod registry;
72pub mod runtime;
73pub mod signal;
74pub mod state;
75pub mod task;
76pub mod utils;
77
78// Re-exports
79pub use config::RuntimeConfig;
80pub use error::{
81    HealthError, MetricsError, MiddlewareError, PluginError, RegistryError, RuntimeError,
82};
83pub use health::{HealthCheck, HealthCheckResult, HealthChecker};
84pub use metrics::MetricsCollector;
85pub use middleware::{Middleware, MiddlewareChain};
86pub use plugin::{Plugin, PluginContext, PluginManager};
87pub use registry::{ServiceInfo, ServiceRegistry};
88pub use runtime::{HealthFailureAction, ServiceRuntime};
89pub use signal::{
90    ChannelSignal, CompositeSignal, CtrlCSignal, ShutdownSignal, UnixSignal, UnixSignalKind,
91};
92pub use state::{RuntimeEvent, StateEvent, StateTracker, TaskStateInfo};
93pub use task::{SpawnTask, Task, TaskManager, TaskResult, TaskState};
94pub use utils::topological_sort;