pitchfork-cli 2.13.1

Daemons with DX
Documentation
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
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
use crate::daemon::{Daemon, RunOptions};
use crate::daemon_id::DaemonId;
use crate::error::IpcError;
use crate::ipc::batch::RunResult;
use crate::ipc::{IpcRequest, IpcResponse, deserialize, fs_name, serialize};
use crate::settings::settings;
use crate::{Result, supervisor};
use exponential_backoff::Backoff;
use interprocess::local_socket::tokio::{RecvHalf, SendHalf};
use interprocess::local_socket::traits::tokio::Stream;
use miette::Context;
use std::path::PathBuf;
use std::time::Duration;
use tokio::io::{AsyncBufReadExt, AsyncWriteExt, BufReader};
use tokio::sync::Mutex;
use uuid::Uuid;

pub struct IpcClient {
    _id: String,
    recv: Mutex<BufReader<RecvHalf>>,
    send: Mutex<SendHalf>,
}

impl IpcClient {
    pub async fn connect(autostart: bool) -> Result<Self> {
        if autostart {
            supervisor::start_if_not_running()?;
        }
        let id = Uuid::new_v4().to_string();
        let client = Self::connect_(&id, "main").await?;
        trace!("Connected to IPC socket");
        let client_version = env!("CARGO_PKG_VERSION").to_string();

        // Try ConnectV2 first (supervisor that knows about it will return ConnectOk with its version).
        // If the supervisor is older and doesn't recognize ConnectV2, it will return Error,
        // and we fall back to the legacy Connect handshake.
        let rsp = client
            .request(IpcRequest::ConnectV2 {
                version: client_version.clone(),
            })
            .await?;
        match rsp {
            IpcResponse::ConnectOk {
                version: supervisor_version,
            } => {
                if supervisor_version != client_version {
                    warn!(
                        "CLI version {client_version} differs from supervisor version {supervisor_version}. \
                         Restart the supervisor with: pitchfork supervisor start --force"
                    );
                }
            }
            IpcResponse::Error(_) => {
                // Old supervisor doesn't recognize ConnectV2 — fall back to legacy Connect
                debug!("Supervisor did not recognize ConnectV2, falling back to legacy Connect");
                let rsp = client.request(IpcRequest::Connect).await?;
                if !rsp.is_ok() {
                    return Err(IpcError::UnexpectedResponse {
                        expected: "Ok".to_string(),
                        actual: format!("{rsp:?}"),
                    }
                    .into());
                }
                warn!(
                    "Supervisor is running an older version. \
                     Restart the supervisor with: pitchfork supervisor start --force"
                );
            }
            _ => {
                return Err(IpcError::UnexpectedResponse {
                    expected: "ConnectOk or Error".to_string(),
                    actual: format!("{rsp:?}"),
                }
                .into());
            }
        }
        debug!("Connected to IPC main");
        Ok(client)
    }

    async fn connect_(id: &str, name: &str) -> Result<Self> {
        let s = settings();
        let connect_attempts = u32::try_from(s.ipc.connect_attempts).unwrap_or_else(|_| {
            warn!(
                "ipc.connect_attempts value {} is out of range (0-{}), clamping to 5",
                s.ipc.connect_attempts,
                u32::MAX
            );
            5
        });
        let connect_attempts = if connect_attempts == 0 {
            warn!("ipc.connect_attempts is 0; defaulting to 1");
            1
        } else {
            connect_attempts
        };
        let connect_min_delay = s.ipc_connect_min_delay();
        let connect_max_delay = s.ipc_connect_max_delay();

        // Compute timeout from backoff parameters: sum the worst-case delays
        // for each attempt (exponential backoff capped at connect_max_delay),
        // plus a 1s buffer for connection overhead.
        let connect_timeout = {
            let mut total = Duration::from_secs(1); // buffer
            let mut delay = connect_min_delay;
            for _ in 0..connect_attempts {
                total += delay;
                delay = (delay * 2).min(connect_max_delay);
            }
            total
        };

        tokio::time::timeout(connect_timeout, async {
            for duration in Backoff::new(connect_attempts, connect_min_delay, connect_max_delay) {
                match interprocess::local_socket::tokio::Stream::connect(fs_name(name)?).await {
                    Ok(conn) => {
                        let (recv, send) = conn.split();
                        let recv = BufReader::new(recv);

                        return Ok(Self {
                            _id: id.to_string(),
                            recv: Mutex::new(recv),
                            send: Mutex::new(send),
                        });
                    }
                    Err(err) => {
                        if let Some(duration) = duration {
                            debug!(
                                "Failed to connect to IPC socket: {err:?}, retrying in {duration:?}"
                            );
                            tokio::time::sleep(duration).await;
                            continue;
                        } else {
                            return Err(IpcError::ConnectionFailed {
                                attempts: connect_attempts,
                                source: Some(err),
                                help:
                                    "ensure the supervisor is running with: pitchfork supervisor start"
                                        .to_string(),
                            }
                            .into());
                        }
                    }
                }
            }
            Err(IpcError::ConnectionFailed {
                attempts: connect_attempts,
                source: None,
                help: "ensure the supervisor is running with: pitchfork supervisor start"
                    .to_string(),
            }
            .into())
        })
        .await
        .unwrap_or_else(|_| {
            Err(IpcError::ConnectionFailed {
                attempts: connect_attempts,
                source: None,
                help: format!(
                    "connection timed out after {connect_timeout:?}; ensure the supervisor is running with: pitchfork supervisor start"
                ),
            }
            .into())
        })
    }

    pub async fn send(&self, msg: IpcRequest) -> Result<()> {
        let mut msg = serialize(&msg)?;
        if msg.contains(&0) {
            return Err(IpcError::InvalidMessage {
                reason: "message contains null byte".to_string(),
            }
            .into());
        }
        msg.push(0);
        let mut send = self.send.lock().await;
        send.write_all(&msg)
            .await
            .map_err(|e| IpcError::SendFailed { source: e })?;
        Ok(())
    }

    async fn read(&self, timeout: Duration) -> Result<IpcResponse> {
        let mut recv = self.recv.lock().await;
        let mut bytes = Vec::new();
        match tokio::time::timeout(timeout, recv.read_until(0, &mut bytes)).await {
            Ok(Ok(_)) => {}
            Ok(Err(err)) => {
                return Err(IpcError::ReadFailed { source: err }.into());
            }
            Err(_) => {
                return Err(IpcError::Timeout {
                    seconds: timeout.as_secs(),
                }
                .into());
            }
        }
        if bytes.is_empty() {
            return Err(IpcError::ConnectionClosed.into());
        }
        deserialize(&bytes).wrap_err("failed to deserialize IPC response")
    }

    pub(crate) async fn request(&self, msg: IpcRequest) -> Result<IpcResponse> {
        self.request_with_timeout(msg, settings().ipc_request_timeout())
            .await
    }

    pub(crate) fn unexpected_response(expected: &str, actual: &IpcResponse) -> IpcError {
        IpcError::UnexpectedResponse {
            expected: expected.to_string(),
            actual: format!("{actual:?}"),
        }
    }

    pub(crate) async fn request_with_timeout(
        &self,
        msg: IpcRequest,
        timeout: Duration,
    ) -> Result<IpcResponse> {
        self.send(msg).await?;
        self.read(timeout).await
    }

    // =========================================================================
    // Low-level IPC operations
    // =========================================================================

    pub async fn enable(&self, id: DaemonId) -> Result<bool> {
        let id_str = id.qualified();
        let rsp = self.request(IpcRequest::Enable { id: id.clone() }).await?;
        match rsp {
            IpcResponse::Yes => {
                info!("Enabled daemon {id_str}");
                Ok(true)
            }
            IpcResponse::No => {
                info!("Daemon {id_str} already enabled");
                Ok(false)
            }
            IpcResponse::Error(error) => Err(miette::miette!(error)),
            rsp => Err(Self::unexpected_response("Yes or No", &rsp).into()),
        }
    }

    pub async fn disable(&self, id: DaemonId) -> Result<bool> {
        let id_str = id.qualified();
        let rsp = self.request(IpcRequest::Disable { id: id.clone() }).await?;
        match rsp {
            IpcResponse::Yes => {
                info!("Disabled daemon {id_str}");
                Ok(true)
            }
            IpcResponse::No => {
                info!("Daemon {id_str} already disabled");
                Ok(false)
            }
            IpcResponse::Error(error) => Err(miette::miette!(error)),
            rsp => Err(Self::unexpected_response("Yes or No", &rsp).into()),
        }
    }

    /// Run a single daemon with the given options (low-level operation)
    pub async fn run(&self, opts: RunOptions) -> Result<RunResult> {
        let start_time = chrono::Local::now();
        // Use longer timeout for daemon start - ready_delay can be up to 60s+
        let timeout = Duration::from_secs(opts.ready_delay.unwrap_or(3) + 60);
        let rsp = self
            .request_with_timeout(IpcRequest::Run(opts.clone()), timeout)
            .await?;

        match rsp {
            IpcResponse::DaemonStart { daemon } => {
                debug!("Started {}", daemon.id);
                Ok(RunResult {
                    started: true,
                    exit_code: None,
                    start_time,
                    resolved_ports: daemon.resolved_port.clone(),
                    error_message: None,
                })
            }
            IpcResponse::DaemonReady { daemon } => {
                debug!("Started {}", daemon.id);
                Ok(RunResult {
                    started: true,
                    exit_code: None,
                    start_time,
                    resolved_ports: daemon.resolved_port.clone(),
                    error_message: None,
                })
            }
            IpcResponse::DaemonFailedWithCode { exit_code } => {
                let code = exit_code.unwrap_or(1);
                Ok(RunResult {
                    started: false,
                    exit_code: Some(code),
                    start_time,
                    resolved_ports: Vec::new(),
                    error_message: Some(format!(
                        "Daemon {} failed with exit code {}",
                        opts.id, code
                    )),
                })
            }
            IpcResponse::DaemonAlreadyRunning => {
                warn!("Daemon {} already running", opts.id);
                Ok(RunResult {
                    started: false,
                    exit_code: None,
                    start_time,
                    resolved_ports: Vec::new(),
                    error_message: None,
                })
            }
            IpcResponse::DaemonFailed { error } => Ok(RunResult {
                started: false,
                exit_code: Some(1),
                start_time,
                resolved_ports: Vec::new(),
                error_message: Some(format!("Failed to start daemon {}: {}", opts.id, error)),
            }),
            IpcResponse::PortConflict { port, process, pid } => Ok(RunResult {
                started: false,
                exit_code: Some(1),
                start_time,
                resolved_ports: Vec::new(),
                error_message: Some(format!(
                    "Failed to start daemon {}: port {} is already in use by process '{}' (PID: {})",
                    opts.id, port, process, pid
                )),
            }),
            IpcResponse::NoAvailablePort {
                start_port,
                attempts,
            } => Ok(RunResult {
                started: false,
                exit_code: Some(1),
                start_time,
                resolved_ports: Vec::new(),
                error_message: Some(format!(
                    "Failed to start daemon {}: could not find an available port after {} attempts starting from {}",
                    opts.id, attempts, start_port
                )),
            }),
            rsp => Err(Self::unexpected_response("DaemonStart or DaemonReady", &rsp).into()),
        }
    }

    pub async fn active_daemons(&self) -> Result<Vec<Daemon>> {
        let rsp = self.request(IpcRequest::GetActiveDaemons).await?;
        match rsp {
            IpcResponse::ActiveDaemons(daemons) => Ok(daemons),
            rsp => Err(Self::unexpected_response("ActiveDaemons", &rsp).into()),
        }
    }

    pub async fn update_shell_dir(&self, shell_pid: u32, dir: PathBuf) -> Result<()> {
        let rsp = self
            .request(IpcRequest::UpdateShellDir {
                shell_pid,
                dir: dir.clone(),
            })
            .await?;
        match rsp {
            IpcResponse::Ok => {
                trace!("updated shell dir for pid {shell_pid} to {}", dir.display());
            }
            rsp => return Err(Self::unexpected_response("Ok", &rsp).into()),
        }
        Ok(())
    }

    pub async fn clean(&self) -> Result<()> {
        let rsp = self.request(IpcRequest::Clean).await?;
        match rsp {
            IpcResponse::Ok => {
                info!("Cleaned up stopped/failed daemons");
            }
            rsp => return Err(Self::unexpected_response("Ok", &rsp).into()),
        }
        Ok(())
    }

    pub async fn get_disabled_daemons(&self) -> Result<Vec<DaemonId>> {
        let rsp = self.request(IpcRequest::GetDisabledDaemons).await?;
        match rsp {
            IpcResponse::DisabledDaemons(daemons) => Ok(daemons),
            rsp => Err(Self::unexpected_response("DisabledDaemons", &rsp).into()),
        }
    }

    pub async fn get_notifications(&self) -> Result<Vec<(log::LevelFilter, String)>> {
        let rsp = self.request(IpcRequest::GetNotifications).await?;
        match rsp {
            IpcResponse::Notifications(notifications) => Ok(notifications),
            rsp => Err(Self::unexpected_response("Notifications", &rsp).into()),
        }
    }

    /// Notify the supervisor that the slug registry has changed.
    ///
    /// The supervisor will re-read slugs and update mDNS records.  This is a
    /// best-effort notification — if the supervisor is not running, the call
    /// silently succeeds (mDNS is not needed without a running supervisor).
    pub async fn sync_mdns(&self) -> Result<()> {
        let rsp = self.request(IpcRequest::SyncMdns).await?;
        match rsp {
            IpcResponse::MdnsSynced => Ok(()),
            IpcResponse::Error(e) => {
                // Old supervisor doesn't recognize SyncMdns — not an error.
                info!("mDNS sync skipped: {e}");
                Ok(())
            }
            rsp => Err(Self::unexpected_response("MdnsSynced", &rsp).into()),
        }
    }

    /// Notify the supervisor that settings have changed.
    ///
    /// The supervisor will reload settings from config files. This is a
    /// best-effort notification — if the supervisor is not running, the call
    /// silently succeeds (settings will be fresh on next supervisor start).
    pub async fn reload_config(&self) -> Result<()> {
        match self.request(IpcRequest::ReloadConfig).await {
            Ok(IpcResponse::ConfigReloaded) => Ok(()),
            Ok(IpcResponse::Error(e)) => {
                debug!("config reload skipped: {e}");
                Ok(())
            }
            Ok(rsp) => Err(Self::unexpected_response("ConfigReloaded", &rsp).into()),
            Err(err) => {
                debug!("config reload skipped: {err:?}");
                Ok(())
            }
        }
    }

    /// Stop a single daemon (low-level operation)
    pub async fn stop(&self, id: DaemonId) -> Result<bool> {
        let id_str = id.qualified();
        let rsp = self.request(IpcRequest::Stop { id: id.clone() }).await?;
        match rsp {
            IpcResponse::Ok => {
                info!("Stopped daemon {id_str}");
                Ok(true)
            }
            IpcResponse::DaemonNotRunning => {
                warn!("Daemon {id_str} is not running");
                Ok(false)
            }
            IpcResponse::DaemonNotFound => {
                warn!("Daemon {id_str} not found");
                Ok(false)
            }
            IpcResponse::DaemonWasNotRunning => {
                warn!("Daemon {id_str} was not running (process may have exited unexpectedly)");
                Ok(false)
            }
            IpcResponse::DaemonStopFailed { error } => {
                error!("Failed to stop daemon {id_str}: {error}");
                Err(crate::error::DaemonError::StopFailed {
                    id: id_str.clone(),
                    error,
                }
                .into())
            }
            rsp => Err(Self::unexpected_response(
                "Ok, DaemonNotRunning, DaemonNotFound, DaemonWasNotRunning, or DaemonStopFailed",
                &rsp,
            )
            .into()),
        }
    }
}