Skip to main content

Crate flare_core_runtime

Crate flare_core_runtime 

Source
Expand description

Service runtime and lifecycle orchestration for Flare services.

flare-core-runtime manages service tasks, dependency ordering, health checks, shutdown signals, plugin hooks, metrics collection, middleware, and state tracking. It is transport-neutral and is used by HTTP, gRPC, MQ, and custom workers.

§核心特性

  • Unified service startup for HTTP, gRPC, MQ consumers, and custom tasks.
  • Graceful shutdown with multiple signal sources and dependency ordering.
  • Service orchestration with task dependencies, health checks, and state tracking.
  • Extensible plugin hooks, middleware chains, and custom adapters.

§Architecture

flare-core-runtime provides transport-neutral traits, configuration, and orchestration primitives. Concrete adapters live in sibling crates:

  • flare-core-transport implements HTTP and gRPC adapters.
  • flare-core-messaging implements MQ consumer adapters.

§Example

use flare_core_runtime::{ServiceRuntime, Task, TaskResult};
use std::pin::Pin;
use std::future::Future;

// Define a custom task.
struct MyTask {
    name: String,
}

impl Task for MyTask {
    fn name(&self) -> &str {
        &self.name
    }

    fn run(
        self: Box<Self>,
        shutdown_rx: tokio::sync::oneshot::Receiver<()>,
    ) -> Pin<Box<dyn Future<Output = TaskResult> + Send>> {
        Box::pin(async move {
            // Task logic.
            Ok(())
        })
    }
}

#[tokio::main]
async fn main() -> anyhow::Result<()> {
    // Create the runtime.
    let runtime = ServiceRuntime::new("my-service")
        .add_task(Box::new(MyTask { name: "task-1".to_string() }));

    // Run until shutdown.
    runtime.run().await?;
    Ok(())
}

Re-exports§

pub use config::RuntimeConfig;
pub use error::HealthError;
pub use error::MetricsError;
pub use error::MiddlewareError;
pub use error::PluginError;
pub use error::RegistryError;
pub use error::RuntimeError;
pub use health::HealthCheck;
pub use health::HealthCheckResult;
pub use health::HealthChecker;
pub use metrics::MetricsCollector;
pub use middleware::Middleware;
pub use middleware::MiddlewareChain;
pub use plugin::Plugin;
pub use plugin::PluginContext;
pub use plugin::PluginManager;
pub use registry::ServiceInfo;
pub use registry::ServiceRegistry;
pub use runtime::HealthFailureAction;
pub use runtime::ServiceRuntime;
pub use signal::ChannelSignal;
pub use signal::CompositeSignal;
pub use signal::CtrlCSignal;
pub use signal::ShutdownSignal;
pub use signal::UnixSignal;
pub use signal::UnixSignalKind;
pub use state::RuntimeEvent;
pub use state::StateEvent;
pub use state::StateTracker;
pub use state::TaskStateInfo;
pub use task::SpawnTask;
pub use task::Task;
pub use task::TaskManager;
pub use task::TaskResult;
pub use task::TaskState;
pub use utils::topological_sort;

Modules§

config
运行时配置模块
error
统一错误类型定义
health
健康检查模块
metrics
指标收集模块
middleware
中间件系统模块
plugin
插件系统模块
registry
服务注册模块
runtime
ServiceRuntime 核心实现
signal
停机信号模块
state
状态追踪模块
task
任务抽象模块
utils
工具函数模块