Skip to content

Commit

Permalink
wip
Browse files Browse the repository at this point in the history
  • Loading branch information
jdx committed Dec 12, 2024
1 parent 2b65582 commit 086d69a
Show file tree
Hide file tree
Showing 9 changed files with 50 additions and 13 deletions.
22 changes: 19 additions & 3 deletions src/cli/cd.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
use crate::ipc::client::IpcClient;
use crate::pitchfork_toml::{PitchforkToml, PitchforkTomlAuto};
use crate::{env, Result};
use duct::cmd;
use itertools::Itertools;
use miette::IntoDiagnostic;
use std::collections::HashSet;

#[derive(Debug, clap::Args)]
#[clap(hide = true, verbatim_doc_comment)]
Expand All @@ -28,10 +30,24 @@ impl Cd {
"--shell-pid".into(),
self.shell_pid.to_string(),
];
for id in to_start {
args.push(id);

if let Ok(ipc) = IpcClient::connect(false).await {
let active_daemons: HashSet<String> = ipc
.active_daemons()
.await?
.into_iter()
.map(|d| d.id)
.collect();
for id in &to_start {
if active_daemons.contains(id) {
continue;
}
args.push(id.clone());
}
if args.len() > 3 {
cmd(&*env::PITCHFORK_BIN, args).run().into_diagnostic()?;
}
}
cmd(&*env::PITCHFORK_BIN, args).run().into_diagnostic()?;
Ok(())
}
}
2 changes: 1 addition & 1 deletion src/cli/disable.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ pub struct Disable {

impl Disable {
pub async fn run(&self) -> Result<()> {
let ipc = IpcClient::connect().await?;
let ipc = IpcClient::connect(false).await?;
ipc.disable(self.id.clone()).await?;
Ok(())
}
Expand Down
2 changes: 1 addition & 1 deletion src/cli/enable.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ pub struct Enable {

impl Enable {
pub async fn run(&self) -> Result<()> {
let ipc = IpcClient::connect().await?;
let ipc = IpcClient::connect(false).await?;
ipc.enable(self.id.clone()).await?;
Ok(())
}
Expand Down
2 changes: 1 addition & 1 deletion src/cli/run.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ impl Run {
bail!("No command provided");
}

let ipc = IpcClient::connect().await?;
let ipc = IpcClient::connect(true).await?;

ipc.run(RunOptions {
id: self.id.clone(),
Expand Down
13 changes: 12 additions & 1 deletion src/cli/start.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ use crate::ipc::client::IpcClient;
use crate::pitchfork_toml::PitchforkToml;
use crate::Result;
use miette::{ensure, IntoDiagnostic};
use std::collections::HashSet;

/// Starts a daemon from a pitchfork.toml file
#[derive(Debug, clap::Args)]
Expand All @@ -24,8 +25,18 @@ impl Start {
"At least one daemon ID must be provided"
);
let pt = PitchforkToml::all_merged();
let ipc = IpcClient::connect().await?;
let ipc = IpcClient::connect(true).await?;
let active_daemons: HashSet<String> = ipc
.active_daemons()
.await?
.into_iter()
.map(|d| d.id)
.collect();
for id in &self.id {
if !self.force && active_daemons.contains(id) {
trace!("Daemon {} is already running", id);
continue;
}
let daemon = pt.daemons.get(id);
if let Some(daemon) = daemon {
let cmd = shell_words::split(&daemon.run).into_diagnostic()?;
Expand Down
2 changes: 1 addition & 1 deletion src/cli/supervisor/start.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ pub struct Start {

impl Start {
pub async fn run(&self) -> Result<()> {
IpcClient::connect().await?;
IpcClient::connect(true).await?;
println!("Pitchfork daemon is running");

Ok(())
Expand Down
2 changes: 1 addition & 1 deletion src/cli/supervisor/status.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ pub struct Status {}

impl Status {
pub async fn run(&self) -> Result<()> {
IpcClient::connect().await?;
IpcClient::connect(false).await?;
println!("Pitchfork daemon is running");
Ok(())
}
Expand Down
2 changes: 1 addition & 1 deletion src/cli/supervisor/stop.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ impl Stop {
if let Some(d) = pid_file.daemons.get("pitchfork") {
if let Some(pid) = d.pid {
info!("Stopping pitchfork daemon with pid {}", pid);
if !(kill_or_stop(pid, false).await?) {
if kill_or_stop(pid, true).await? {
return Ok(());
}
}
Expand Down
16 changes: 13 additions & 3 deletions src/ipc/client.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use crate::daemon::RunOptions;
use crate::daemon::{Daemon, RunOptions};
use crate::ipc::{deserialize, fs_name, serialize, IpcRequest, IpcResponse};
use crate::{supervisor, Result};
use exponential_backoff::Backoff;
Expand All @@ -21,8 +21,10 @@ const CONNECT_MIN_DELAY: Duration = Duration::from_millis(100);
const CONNECT_MAX_DELAY: Duration = Duration::from_secs(1);

impl IpcClient {
pub async fn connect() -> Result<Self> {
supervisor::start_if_not_running()?;
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");
Expand Down Expand Up @@ -148,4 +150,12 @@ impl IpcClient {
}
Ok(())
}

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

0 comments on commit 086d69a

Please sign in to comment.