xan_actor/
types.rs

1#[derive(Debug)]
2/// A message that can be sent to a worker thread.
3/// > You don't need to implement this trait.
4/// > It is a wrapper for rmp_serde encoded data that you sent.
5/// This message contains the data to be processed and an result channel.
6/// The result channel is used to send the result back to the sender.
7pub struct Message {
8    inner: Vec<u8>,
9    result_tx: Option<tokio::sync::oneshot::Sender<Vec<u8>>>,
10}
11impl Clone for Message {
12    fn clone(&self) -> Self {
13        Self {
14            inner: self.inner.clone(),
15            result_tx: None,
16        }
17    }
18}
19
20impl Message {
21    pub fn new(inner: Vec<u8>, result_tx: Option<tokio::sync::oneshot::Sender<Vec<u8>>>) -> Self {
22        Self { inner, result_tx }
23    }
24
25    pub fn inner(&self) -> &Vec<u8> {
26        &self.inner
27    }
28
29    pub fn result_tx(&mut self) -> Option<tokio::sync::oneshot::Sender<Vec<u8>>> {
30        let tx = self.result_tx.take();
31        self.result_tx = None;
32        tx
33    }
34}
35
36#[derive(Debug, Clone)]
37/// A specification for a job that can be executed by a worker thread.
38/// The `max_iter` is the maximum number of iterations the job will be executed.
39/// If `max_iter` is `None`, the job will be executed indefinitely.
40/// The `interval` is the time between two iterations of the job.
41/// If the `interval` is `None`, the job will be executed only once.
42/// The `start_at` is the time when the job will start executing.
43/// If the `start_at` is in the past, the job will start executing immediately.
44pub struct JobSpec {
45    max_iter: Option<usize>,
46    interval: Option<std::time::Duration>,
47    start_at: std::time::SystemTime,
48}
49
50impl JobSpec {
51    pub fn new(
52        max_iter: Option<usize>,
53        interval: Option<std::time::Duration>,
54        start_at: std::time::SystemTime,
55    ) -> Self {
56        if let None = interval {
57            Self {
58                max_iter: Some(1),
59                interval,
60                start_at,
61            }
62        } else {
63            Self {
64                max_iter,
65                interval,
66                start_at,
67            }
68        }
69    }
70
71    pub fn max_iter(&self) -> Option<usize> {
72        self.max_iter
73    }
74
75    pub fn start_at(&self) -> std::time::SystemTime {
76        self.start_at
77    }
78
79    pub fn interval(&self) -> Option<std::time::Duration> {
80        self.interval
81    }
82}
83
84impl Default for JobSpec {
85    fn default() -> Self {
86        Self {
87            max_iter: Some(1),
88            interval: None,
89            start_at: std::time::SystemTime::now(),
90        }
91    }
92}