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
use std::{
io,
sync::{Arc, Mutex},
};
use tokio::sync::oneshot;
use super::command::CommandStopped;
pub enum KillCommandReason<T> {
OtherCommandExited(Arc<CommandStopped<T, T>>),
MainProcessGotSignal,
}
impl<T> Clone for KillCommandReason<T> {
fn clone(&self) -> Self {
match self {
Self::OtherCommandExited(arc) => Self::OtherCommandExited(arc.clone()),
Self::MainProcessGotSignal => Self::MainProcessGotSignal,
}
}
}
pub(super) type KillSender<T> = oneshot::Sender<KillCommandReason<T>>;
#[derive(Clone)]
pub struct CommandKiller<T>(Arc<Mutex<Option<KillSender<T>>>>);
impl<T> CommandKiller<T> {
pub(super) fn new(kill_sender: KillSender<T>) -> Self {
Self(Arc::new(Mutex::new(Some(kill_sender))))
}
pub fn kill(&self, reason: KillCommandReason<T>) -> KillResult {
let mut kill_sender = self.0.lock().unwrap();
let kill_sender = kill_sender.take();
if let Some(kill_sender) = kill_sender {
match kill_sender.send(reason) {
Ok(_) => KillResult::SentSuccess,
Err(_) => KillResult::AlreadyExited,
}
} else {
KillResult::AlreadySent
}
}
}
pub enum KillJoinHandleFinalStatus<T> {
SenderDisconnected,
Killed(KillCommandReason<T>),
FailedToKill {
reason: KillCommandReason<T>,
error: io::Error,
},
AlreadyExited(CommandAlreadyExitedKind<T>),
UnexpectedAlreadyKilled,
}
pub enum CommandAlreadyExitedKind<T> {
SelfExited,
ProcessExited(KillCommandReason<T>),
}
pub enum KillResult {
SentSuccess,
AlreadySent,
AlreadyExited,
}