1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
use std::time::{SystemTime, Duration};
use std::sync::mpsc::Sender;
use chrono::{DateTime, Utc};
use config::Config;
use uuid::Uuid;
use crate::protocol::{Message, ActorMsg};
use crate::actor::{ActorRef, Tell};
pub trait Timer {
type Msg: Message;
fn schedule<T>(&self,
initial_delay: Duration,
interval: Duration,
receiver: ActorRef<Self::Msg>,
sender: Option<ActorRef<Self::Msg>>,
msg: T)
-> Uuid
where T: Into<ActorMsg<Self::Msg>>;
fn schedule_once<T>(&self,
delay: Duration,
receiver: ActorRef<Self::Msg>,
sender: Option<ActorRef<Self::Msg>>,
msg: T)
-> Uuid
where T: Into<ActorMsg<Self::Msg>>;
fn schedule_at_time<T>(&self,
time: DateTime<Utc>,
receiver: ActorRef<Self::Msg>,
sender: Option<ActorRef<Self::Msg>>,
msg: T)
-> Uuid
where T: Into<ActorMsg<Self::Msg>>;
fn cancel_schedule(&self, id: Uuid);
}
pub trait TimerFactory {
type Msg: Message;
fn new(config: &Config, debug: bool) -> Sender<Job<Self::Msg>>;
}
pub enum Job<Msg: Message> {
Once(OnceJob<Msg>),
Repeat(RepeatJob<Msg>),
Cancel(Uuid),
}
pub struct OnceJob<Msg: Message> {
pub id: Uuid,
pub send_at: SystemTime,
pub receiver: ActorRef<Msg>,
pub sender: Option<ActorRef<Msg>>,
pub msg: ActorMsg<Msg>,
}
impl<Msg: Message> OnceJob<Msg> {
pub fn send(self) {
let _ = self.receiver.tell(self.msg, self.sender);
}
}
pub struct RepeatJob<Msg: Message> {
pub id: Uuid,
pub send_at: SystemTime,
pub interval: Duration,
pub receiver: ActorRef<Msg>,
pub sender: Option<ActorRef<Msg>>,
pub msg: ActorMsg<Msg>,
}
impl<Msg: Message> RepeatJob<Msg> {
pub fn send(&self) {
let _ = self.receiver.tell(self.msg.clone(), self.sender.clone());
}
}