使用rs-svc
实现rust程序的优雅退出.
引用依赖
在Cargo.toml
中添加rs-svc和anyhow库:
[package]
name = "svctest"
version = "0.1.0"
edition = "2021"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
[dependencies]
rs-svc="0.1"
anyhow = "1.0"
实现接口
在自己的服务中实现Service
接口:
use rs_svc::svc::service::Service;
struct App;
impl Service for App {
fn init(&self) -> anyhow::Result<()> {
println!("init");
Ok(())
}
fn start(&self) -> anyhow::Result<()> {
println!("start");
Ok(())
}
fn stop(&self) -> anyhow::Result<()> {
println!("stop");
Ok(())
}
}
fn main() {
let app = App;
rs_svc::svc::service::run(&app).unwrap()
}
控制台输出: