tibba_runtime/lib.rs
1// Copyright 2026 Tree xie.
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
15//! 进程运行时:运行时状态、运行时资源与运行时编排。
16//!
17//! 三者都描述「正在运行的这个进程」,与请求处理、存储访问无关,故同 crate:
18//!
19//! | 模块 | 内容 | feature |
20//! |------|------|---------|
21//! | `app_state` | [`AppState`]:服务标识、运行标志、并发计数、启动时间 | 始终编译 |
22//! | `ctx` | task-local 请求上下文 [`CTX`] / [`Context`] | 始终编译 |
23//! | `process` | 进程 CPU / 内存 / 文件描述符 / 磁盘读写采样 | `process-info` |
24//! | `hook` | 启动前 / 关闭后钩子:[`register_task`] + [`run_before_tasks`] / [`run_after_tasks`] | 始终编译 |
25//! | `scheduler` | cron 定时任务:[`register_job_task`] + `run_scheduler_jobs` | `scheduler` |
26//!
27//! `hook` 管「启动/关闭各跑一次」,`scheduler` 管「按 cron 周期性反复跑」。
28//! 与 `tibba-job`(Postgres 异步任务队列)的分工:本 crate 是进程内编排,
29//! 不落库、不跨进程重试。
30//!
31//! 两个可选 feature 默认关闭:`sysinfo` 与 `tokio-cron-scheduler` 传递依赖较重,
32//! 只用 `AppState` / `CTX` / 钩子的下游不必为其付出编译代价。
33
34mod app_state;
35mod ctx;
36mod hook;
37#[cfg(feature = "process-info")]
38mod process;
39#[cfg(feature = "scheduler")]
40mod scheduler;
41
42pub use app_state::*;
43pub use ctx::*;
44pub use hook::*;
45#[cfg(feature = "process-info")]
46pub use process::*;
47#[cfg(feature = "scheduler")]
48pub use scheduler::*;
49
50/// 钩子相关日志事件的 tracing target。
51/// 可通过 `RUST_LOG=tibba:hook=info`(或 `debug`)进行过滤。
52pub(crate) const HOOK_LOG_TARGET: &str = "tibba:hook";
53
54/// 调度器相关日志事件的 tracing target。
55/// 可通过 `RUST_LOG=tibba:scheduler=info`(或 `debug`)进行过滤。
56///
57/// 与 `HOOK_LOG_TARGET` 分开:两者虽同 crate,但排查场景不同
58/// (启停问题 vs 定时任务漏跑),合成一个 target 会迫使运维多筛一层。
59#[cfg(feature = "scheduler")]
60pub(crate) const SCHEDULER_LOG_TARGET: &str = "tibba:scheduler";