11 个版本 (6 个重大更新)
0.7.0 | 2022 年 3 月 31 日 |
---|---|
0.6.0 | 2021 年 6 月 25 日 |
0.5.0 | 2021 年 3 月 24 日 |
0.4.2 | 2020 年 10 月 26 日 |
0.3.0 | 2020 年 3 月 11 日 |
#604 in 异步
每月 623 次下载
11KB
220 行
actix-daemon-utils
actix 守护进程工具。
功能
- 通过信号优雅地停止(挂起、中断、退出或终止)
- 循环守护进程(looper 或 delayer)
待办事项
- 在 #actix::main 中运行
示例
use actix_daemon_utils::{
actix::{
prelude::*,
System,
},
graceful_stop::{GracefulStop},
looper::{Looper, Task},
};
use std::{
sync::mpsc,
thread,
time::Duration,
};
struct MyActor { msg: String, seconds: u64 }
impl Actor for MyActor {
type Context = Context<Self>;
}
impl Handler<Task> for MyActor {
type Result = Option<std::time::Duration>;
fn handle(&mut self, _msg: Task, _ctx: &mut Self::Context) -> Self::Result {
println!("{}", self.msg);
Some(Duration::from_secs(self.seconds))
}
}
// Note. #[actix::main] don't work. I don't know how to deal with.
fn main() {
let (tx, rx) = mpsc::channel::<()>();
let sys = System::new();
let graceful_stop = GracefulStop::new_with_sender(tx);
sys.block_on( async {
let actor1 = MyActor { msg: "x".to_string(), seconds: 1 }.start();
let actor2 = MyActor { msg: "y".to_string(), seconds: 3 }.start();
let looper1 = Looper::new(actor1.recipient(), graceful_stop.clone_system_terminator()).start();
let looper2 = Looper::new(actor2.recipient(), graceful_stop.clone_system_terminator()).start();
graceful_stop
.subscribe(looper1.recipient())
.subscribe(looper2.recipient())
.start();
});
let sys2 = System::current();
thread::spawn(move || {
rx.recv().unwrap();
println!("ended");
sys2.stop();
});
let _ = sys.run();
println!("main terminated");
}
依赖项
~4–16MB
~152K SLoC