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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
use std::cell::RefCell;
use std::rc::Rc;
use futures::channel::mpsc;
use futures::future::BoxFuture;
use futures::{FutureExt, SinkExt, StreamExt};
use ya_runtime_api::server::*;
use crate::common::IntoVec;
use crate::ProcessId;
#[derive(Clone, Debug)]
pub enum EventKind {
Process(ProcessStatus),
Runtime(RuntimeStatus),
}
impl From<ProcessStatus> for EventKind {
fn from(status: ProcessStatus) -> Self {
Self::Process(status)
}
}
impl From<RuntimeStatus> for EventKind {
fn from(status: RuntimeStatus) -> Self {
Self::Runtime(status)
}
}
impl From<RuntimeStatusKind> for EventKind {
fn from(kind: RuntimeStatusKind) -> Self {
Self::Runtime(RuntimeStatus { kind: Some(kind) })
}
}
#[derive(Clone)]
pub struct EventEmitter {
tx_process: mpsc::Sender<ProcessStatus>,
tx_runtime: mpsc::Sender<RuntimeStatus>,
}
impl EventEmitter {
pub fn spawn(emitter: impl RuntimeHandler + 'static) -> Self {
let (tx_p, rx_p) = mpsc::channel(1);
let (tx_r, rx_r) = mpsc::channel(1);
let e_p = Rc::new(RefCell::new(emitter));
let e_r = e_p.clone();
tokio::task::spawn_local(
rx_p.for_each(move |status| e_p.borrow().on_process_status(status)),
);
tokio::task::spawn_local(
rx_r.for_each(move |status| e_r.borrow().on_runtime_status(status)),
);
Self {
tx_process: tx_p,
tx_runtime: tx_r,
}
}
}
impl EventEmitter {
pub fn command_started(&mut self, process_id: ProcessId) -> BoxFuture<()> {
self.emit(ProcessStatus {
pid: process_id,
running: true,
return_code: 0,
stdout: Default::default(),
stderr: Default::default(),
})
}
pub fn command_stopped(&mut self, process_id: ProcessId, return_code: i32) -> BoxFuture<()> {
self.emit(ProcessStatus {
pid: process_id,
running: false,
return_code,
stdout: Default::default(),
stderr: Default::default(),
})
}
pub fn command_stdout(
&mut self,
process_id: ProcessId,
stdout: impl IntoVec<u8>,
) -> BoxFuture<()> {
self.emit(ProcessStatus {
pid: process_id,
running: true,
return_code: 0,
stdout: stdout.into_vec(),
stderr: Default::default(),
})
}
pub fn command_stderr(
&mut self,
process_id: ProcessId,
stderr: impl IntoVec<u8>,
) -> BoxFuture<()> {
self.emit(ProcessStatus {
pid: process_id,
running: true,
return_code: 0,
stdout: Default::default(),
stderr: stderr.into_vec(),
})
}
pub fn state(&mut self, state: RuntimeState) -> BoxFuture<()> {
self.emit(RuntimeStatusKind::State(state))
}
pub fn counter(&mut self, counter: RuntimeCounter) -> BoxFuture<()> {
self.emit(RuntimeStatusKind::Counter(counter))
}
pub fn emit(&mut self, event: impl Into<EventKind>) -> BoxFuture<()> {
match event.into() {
EventKind::Process(status) => self.tx_process.send(status).then(|_| async {}).boxed(),
EventKind::Runtime(status) => self.tx_runtime.send(status).then(|_| async {}).boxed(),
}
}
}