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 d797c6c commit 182e6fa
Show file tree
Hide file tree
Showing 16 changed files with 161 additions and 112 deletions.
23 changes: 19 additions & 4 deletions docs/cli/commands.json
Original file line number Diff line number Diff line change
Expand Up @@ -415,7 +415,7 @@
"full_cmd": [
"start"
],
"usage": "start [ID]...",
"usage": "start [-f --force] [ID]...",
"subcommands": {},
"args": [
{
Expand Down Expand Up @@ -444,6 +444,20 @@
"required": true,
"hide": false
}
},
{
"name": "force",
"usage": "-f --force",
"help": "Stop the daemon if it is already running",
"help_first_line": "Stop the daemon if it is already running",
"short": [
"f"
],
"long": [
"force"
],
"hide": false,
"global": false
}
],
"mounts": [],
Expand Down Expand Up @@ -485,15 +499,16 @@
"full_cmd": [
"stop"
],
"usage": "stop <ID>",
"usage": "stop [ID]...",
"subcommands": {},
"args": [
{
"name": "ID",
"usage": "<ID>",
"usage": "[ID]...",
"help": "The name of the daemon to stop",
"help_first_line": "The name of the daemon to stop",
"required": true,
"required": false,
"var": true,
"hide": false
}
],
Expand Down
4 changes: 2 additions & 2 deletions docs/cli/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,9 @@
- [`pitchfork list [--hide-header]`](/cli/list.md)
- [`pitchfork logs [-n <N>] [-t --tail] [ID]...`](/cli/logs.md)
- [`pitchfork run [-f --force] <ID> [RUN]...`](/cli/run.md)
- [`pitchfork start [ID]...`](/cli/start.md)
- [`pitchfork start [-f --force] [ID]...`](/cli/start.md)
- [`pitchfork status <ID>`](/cli/status.md)
- [`pitchfork stop <ID>`](/cli/stop.md)
- [`pitchfork stop [ID]...`](/cli/stop.md)
- [`pitchfork supervisor <SUBCOMMAND>`](/cli/supervisor.md)
- [`pitchfork supervisor run [-f --force]`](/cli/supervisor/run.md)
- [`pitchfork supervisor start [-f --force]`](/cli/supervisor/start.md)
Expand Down
8 changes: 7 additions & 1 deletion docs/cli/start.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# `pitchfork start`

- **Usage**: `pitchfork start [ID]...`
- **Usage**: `pitchfork start [-f --force] [ID]...`
- **Aliases**: `s`

Starts a daemon from a pitchfork.toml file
Expand All @@ -10,3 +10,9 @@ Starts a daemon from a pitchfork.toml file
### `[ID]...`

ID of the daemon(s) in pitchfork.toml to start

## Flags

### `-f --force`

Stop the daemon if it is already running
4 changes: 2 additions & 2 deletions docs/cli/stop.md
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
# `pitchfork stop`

- **Usage**: `pitchfork stop <ID>`
- **Usage**: `pitchfork stop [ID]...`
- **Aliases**: `kill`

Sends a stop signal to a daemon

## Arguments

### `<ID>`
### `[ID]...`

The name of the daemon to stop
4 changes: 3 additions & 1 deletion pitchfork.toml
Original file line number Diff line number Diff line change
@@ -1 +1,3 @@
[daemons]
[daemons.docs]
run = "mise run docs"
auto = ["start"]
3 changes: 2 additions & 1 deletion pitchfork.usage.kdl
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@ cmd "start" help="Starts a daemon from a pitchfork.toml file" {
flag "--shell-pid" hide=true {
arg "<SHELL_PID>"
}
flag "-f --force" help="Stop the daemon if it is already running"
arg "[ID]..." help="ID of the daemon(s) in pitchfork.toml to start" var=true
}
cmd "status" help="Display the status of a daemon" {
Expand All @@ -75,7 +76,7 @@ cmd "status" help="Display the status of a daemon" {
}
cmd "stop" help="Sends a stop signal to a daemon" {
alias "kill"
arg "<ID>" help="The name of the daemon to stop"
arg "[ID]..." help="The name of the daemon to stop" var=true
}
cmd "supervisor" subcommand_required=true help="Start, stop, and check the status of the pitchfork supervisor daemon" {
cmd "run" help="Runs the internal pitchfork daemon in the foreground" {
Expand Down
16 changes: 1 addition & 15 deletions src/cli/disable.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
use crate::ipc::client::IpcClient;
use crate::ipc::{IpcRequest, IpcResponse};
use crate::Result;

/// Prevent a daemon from restarting
Expand All @@ -13,20 +12,7 @@ pub struct Disable {
impl Disable {
pub async fn run(&self) -> Result<()> {
let ipc = IpcClient::connect().await?;
let rsp = ipc
.request(IpcRequest::Disable {
id: self.id.clone(),
})
.await?;
match rsp {
IpcResponse::Yes => {
info!("disabled daemon {}", self.id);
}
IpcResponse::No => {
info!("daemon {} already disabled", self.id);
}
rsp => unreachable!("unexpected response: {rsp:?}"),
}
ipc.disable(self.id.clone()).await?;
Ok(())
}
}
16 changes: 1 addition & 15 deletions src/cli/enable.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
use crate::ipc::client::IpcClient;
use crate::ipc::{IpcRequest, IpcResponse};
use crate::Result;

/// Allow a daemon to start
Expand All @@ -13,20 +12,7 @@ pub struct Enable {
impl Enable {
pub async fn run(&self) -> Result<()> {
let ipc = IpcClient::connect().await?;
let rsp = ipc
.request(IpcRequest::Enable {
id: self.id.clone(),
})
.await?;
match rsp {
IpcResponse::Yes => {
info!("enabled daemon {}", self.id);
}
IpcResponse::No => {
info!("daemon {} already enabled", self.id);
}
rsp => unreachable!("unexpected response: {rsp:?}"),
}
ipc.enable(self.id.clone()).await?;
Ok(())
}
}
54 changes: 8 additions & 46 deletions src/cli/run.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use crate::daemon::RunOptions;
use crate::ipc::client::IpcClient;
use crate::ipc::{IpcRequest, IpcResponse};
use crate::Result;
use miette::bail;

Expand All @@ -24,51 +24,13 @@ impl Run {

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

if self.force {
let rsp = ipc
.request(IpcRequest::Stop {
id: self.id.clone(),
})
.await?;
match rsp {
IpcResponse::Ok => {
info!("stopped daemon {}", self.id);
}
IpcResponse::DaemonAlreadyStopped => {
info!("daemon {} already stopped", self.id);
}
rsp => unreachable!("unexpected response: {rsp:?}"),
}
}

let rsp = ipc
.request(IpcRequest::Run {
id: self.id.clone(),
cmd: self.run.clone(),
})
.await?;
match rsp {
IpcResponse::DaemonAlreadyRunning => {
if self.force {
bail!("failed to stop daemon {}", self.id);
} else {
info!("daemon {} already running", self.id);
}
}
IpcResponse::DaemonStart { daemon } => {
info!(
"started daemon {} with pid {}",
daemon.id,
daemon.pid.unwrap()
);
}
IpcResponse::DaemonFailed { error } => {
bail!("Failed to start daemon {}: {}", self.id, error);
}
msg => {
debug!("ignoring message: {:?}", msg);
}
}
ipc.run(RunOptions {
id: self.id.clone(),
cmd: self.run.clone(),
shell_pid: None,
force: self.force,
})
.await?;
Ok(())
}
}
18 changes: 14 additions & 4 deletions src/cli/start.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
use crate::daemon::RunOptions;
use crate::ipc::client::IpcClient;
use crate::ipc::IpcRequest;
use crate::pitchfork_toml::PitchforkToml;
use crate::Result;
use miette::IntoDiagnostic;
use miette::{ensure, IntoDiagnostic};

/// Starts a daemon from a pitchfork.toml file
#[derive(Debug, clap::Args)]
Expand All @@ -12,18 +12,28 @@ pub struct Start {
id: Vec<String>,
#[clap(long, hide = true)]
shell_pid: Option<u32>,
/// Stop the daemon if it is already running
#[clap(short, long)]
force: bool,
}

impl Start {
pub async fn run(&self) -> Result<()> {
ensure!(
!self.id.is_empty(),
"At least one daemon ID must be provided"
);
let pt = PitchforkToml::all_merged();
let ipc = IpcClient::connect().await?;
for id in &self.id {
let daemon = pt.daemons.get(id);
if let Some(daemon) = daemon {
ipc.request(IpcRequest::Run {
let cmd = shell_words::split(&daemon.run).into_diagnostic()?;
ipc.run(RunOptions {
id: id.clone(),
cmd: shell_words::split(&daemon.run.clone()).into_diagnostic()?,
cmd,
shell_pid: self.shell_pid,
force: self.force,
})
.await?;
} else {
Expand Down
21 changes: 14 additions & 7 deletions src/cli/stop.rs
Original file line number Diff line number Diff line change
@@ -1,26 +1,33 @@
use crate::cli::supervisor::kill_or_stop;
use crate::state_file::StateFile;
use crate::Result;
use miette::ensure;

/// Sends a stop signal to a daemon
#[derive(Debug, clap::Args)]
#[clap(visible_alias = "kill", verbatim_doc_comment)]
pub struct Stop {
/// The name of the daemon to stop
id: String,
id: Vec<String>,
}

impl Stop {
pub async fn run(&self) -> Result<()> {
ensure!(
!self.id.is_empty(),
"You must provide at least one daemon to stop"
);
let pid_file = StateFile::get();
if let Some(d) = pid_file.daemons.get(&self.id) {
if let Some(pid) = d.pid {
info!("stopping {} with pid {}", self.id, pid);
kill_or_stop(pid, true).await?;
return Ok(());
for id in &self.id {
if let Some(d) = pid_file.daemons.get(id) {
if let Some(pid) = d.pid {
info!("stopping {} with pid {}", id, pid);
kill_or_stop(pid, true).await?;
continue;
}
}
warn!("{} is not running", id);
}
warn!("{} is not running", self.id);
Ok(())
}
}
9 changes: 9 additions & 0 deletions src/daemon.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,18 @@ pub struct Daemon {
pub id: String,
pub title: Option<String>,
pub pid: Option<u32>,
pub shell_pid: Option<u32>,
pub status: DaemonStatus,
}

#[derive(Clone, Debug, serde::Serialize, serde::Deserialize)]
pub struct RunOptions {
pub id: String,
pub cmd: Vec<String>,
pub force: bool,
pub shell_pid: Option<u32>,
}

impl Display for Daemon {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(f, "{}", self.id)
Expand Down
Loading

0 comments on commit 182e6fa

Please sign in to comment.