11 releases (6 breaking)

0.7.0 Mar 31, 2022
0.6.0 Jun 25, 2021
0.5.0 Mar 24, 2021
0.4.2 Oct 26, 2020
0.3.0 Mar 11, 2020

#565 in Asynchronous

Download history 613/week @ 2023-11-23 413/week @ 2023-11-30 452/week @ 2023-12-07 365/week @ 2023-12-14 765/week @ 2023-12-21 70/week @ 2023-12-28 88/week @ 2024-01-04 608/week @ 2024-01-11 759/week @ 2024-01-18 534/week @ 2024-01-25 491/week @ 2024-02-01 296/week @ 2024-02-08 463/week @ 2024-02-15 402/week @ 2024-02-22 553/week @ 2024-02-29 566/week @ 2024-03-07

2,075 downloads per month

MIT/Apache

11KB
220 lines

actix-daemon-utils

Daemon Utilities by actix.

Documentation

Features

  • Graceful Stop by singals(hangup, interrupt, quit or terminate)
  • Loop daemon(looper or delayer)

TODO

Examples

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");
}

Dependencies

~5–15MB
~154K SLoC