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
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
//! IPC request handling and dispatch
//!
//! Handles incoming IPC requests from CLI clients and routes them to the appropriate handlers.
use super::{SUPERVISOR, Supervisor};
use crate::Result;
use crate::ipc::server::IpcServer;
use crate::ipc::{IpcRequest, IpcResponse};
use miette::IntoDiagnostic;
const VERSION: &str = env!("CARGO_PKG_VERSION");
/// Dedupe the supervisor-side version-mismatch warning. Parallel batch clients
/// open one dedicated connection per daemon, each performing its own handshake,
/// which would otherwise log the warning once per connection. Keyed by client
/// version so a different mismatched client still gets logged.
fn version_mismatch_should_warn(client_version: &str) -> bool {
static WARNED: std::sync::Mutex<Option<String>> = std::sync::Mutex::new(None);
let mut warned = WARNED.lock().unwrap_or_else(|e| e.into_inner());
if warned.as_deref() == Some(client_version) {
false
} else {
*warned = Some(client_version.to_string());
true
}
}
impl Supervisor {
/// Main IPC connection watch loop - reads and dispatches requests
pub(crate) async fn conn_watch(&self, mut ipc: IpcServer) -> ! {
loop {
let (msg, send) = match ipc.read().await {
Ok(msg) => msg,
Err(e) => {
error!("failed to accept connection: {e:?}");
continue;
}
};
debug!("received message: {msg:?}");
tokio::spawn(async move {
let rsp = SUPERVISOR
.handle_ipc(msg)
.await
.unwrap_or_else(|err| IpcResponse::Error(err.to_string()));
if let Err(err) = send.send(rsp).await {
debug!("failed to send message: {err:?}");
}
});
}
}
/// Handle a single IPC request and return the appropriate response
pub(crate) async fn handle_ipc(&self, req: IpcRequest) -> Result<IpcResponse> {
let rsp = match req {
IpcRequest::Invalid { error } => {
warn!("Invalid IPC request: {error}");
return Ok(IpcResponse::Error(format!("Invalid request: {error}")));
}
IpcRequest::Connect => {
debug!("received connect message (legacy, no version info)");
self.restore_own_record().await;
IpcResponse::Ok
}
IpcRequest::ConnectV2 {
version: client_version,
} => {
debug!("received connect message (client version: {client_version})");
// A client may have connected because the state file does not
// show this supervisor (see `start_if_not_running`); put the
// record back before it goes looking again, e.g. for
// `supervisor stop`.
self.restore_own_record().await;
if client_version != VERSION && version_mismatch_should_warn(&client_version) {
warn!(
"Client version {client_version} differs from supervisor version {VERSION}. \
Restart the supervisor with: pitchfork supervisor start --force"
);
}
IpcResponse::ConnectOk {
version: VERSION.to_string(),
}
}
IpcRequest::Stop { id } => {
// id is already DaemonId, no validation needed
self.stop(&id).await?
}
IpcRequest::Run(opts) => {
// opts.id is already DaemonId, no validation needed
self.run(opts).await?
}
IpcRequest::Enable { id } => {
// id is already DaemonId, no validation needed
if self.enable(&id).await? {
IpcResponse::Yes
} else {
IpcResponse::No
}
}
IpcRequest::Disable { id } => {
// id is already DaemonId, no validation needed
if self.disable(&id).await? {
IpcResponse::Yes
} else {
IpcResponse::No
}
}
IpcRequest::GetActiveDaemons => {
let daemons = self.active_daemons().await;
IpcResponse::ActiveDaemons(daemons)
}
IpcRequest::GetNotifications => {
let notifications = self.get_notifications().await;
IpcResponse::Notifications(notifications)
}
IpcRequest::UpdateShellDir { shell_pid, dir } => {
let prev = self.get_shell_dir(shell_pid).await;
{
// Seen by any idle stop still deciding, or registered
// after it has finished.
let _admission = super::idle::admit_shell().await;
self.set_shell_dir(shell_pid, dir.clone()).await?;
}
// Cancel any pending autostops for daemons in the new directory
self.cancel_pending_autostops_for_dir(&dir).await;
if let Some(prev) = prev {
self.leave_dir(&prev).await?;
}
self.refresh().await?;
IpcResponse::Ok
}
IpcRequest::SinkOutputLine {
id,
token,
fires_hook,
line,
} => {
super::log_sink::deliver_reported_line(&id, token, fires_hook, line).await;
IpcResponse::Ok
}
IpcRequest::Clean => {
self.clean().await?;
IpcResponse::Ok
}
IpcRequest::CleanFiltered {
namespaces,
daemons,
prune,
} => {
let count = self.clean_filtered(&namespaces, &daemons, prune).await?;
IpcResponse::Cleaned { count }
}
IpcRequest::GetDisabledDaemons => {
let disabled = self.state_file.lock().await.disabled.clone();
IpcResponse::DisabledDaemons(disabled.into_iter().collect())
}
IpcRequest::SyncMdns => {
self.sync_mdns().await;
IpcResponse::MdnsSynced
}
IpcRequest::ReloadConfig => {
tokio::task::spawn_blocking(|| {
crate::pitchfork_toml::invalidate_config_cache();
crate::settings::reload_settings();
crate::logger::apply_settings();
})
.await
.into_diagnostic()?;
IpcResponse::ConfigReloaded
}
IpcRequest::ProjectEnter { pid, dir } => {
debug!("handling project enter pid {pid} dir {}", dir.display());
let prev = {
let _admission = super::idle::admit_shell().await;
self.enter_project_session(pid, dir.clone()).await?
};
self.cancel_pending_autostops_for_dir(&dir).await;
// When re-entering (prev.is_some()), the new session keeps the
// directory active, so leave_dir would be a no-op. Skip it to
// avoid unnecessary autostop evaluation.
let _ = prev;
self.refresh().await?;
IpcResponse::Ok
}
IpcRequest::ProjectLeave { pid, dir } => {
debug!("handling project leave pid {pid} dir {}", dir.display());
if let Some(left_dir) = self.leave_project_session(pid, &dir).await? {
debug!(
"project leave removed session pid {pid}, evaluating {left_dir:?} for autostop"
);
self.leave_dir(&left_dir).await?;
} else {
debug!(
"project leave: session pid {pid} dir {} not found",
dir.display()
);
}
self.refresh().await?;
IpcResponse::Ok
}
IpcRequest::GetProjectSessions => {
let sessions = self.get_project_sessions_info().await;
IpcResponse::ProjectSessions(sessions)
}
IpcRequest::GetWebUrl => IpcResponse::WebUrl {
url: crate::web::url(),
},
IpcRequest::ClaimDaemons { ids } => {
self.claim_daemons(&ids).await;
IpcResponse::Ok
}
};
// Ensure state is flushed to disk before returning the response
// so that CLI commands reading StateFile::get() see fresh data.
self.flush_state().await;
Ok(rsp)
}
}