rust服务控制之使用rs-svc(rust svc)实现优雅退出

使用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()
}

控制台输出:

posted on 2024-06-07 18:07  北溟有鱼。  阅读(10)  评论(0编辑  收藏  举报