Skip to content

Commit

Permalink
wip
Browse files Browse the repository at this point in the history
  • Loading branch information
jdx committed Dec 10, 2024
1 parent 95a197b commit db84961
Show file tree
Hide file tree
Showing 13 changed files with 97 additions and 33 deletions.
15 changes: 12 additions & 3 deletions docs/cli/commands.json
Original file line number Diff line number Diff line change
Expand Up @@ -416,13 +416,22 @@
"full_cmd": [
"stop"
],
"usage": "stop",
"usage": "stop <ID>",
"subcommands": {},
"args": [],
"args": [
{
"name": "ID",
"usage": "<ID>",
"help": "The name of the daemon to stop",
"help_first_line": "The name of the daemon to stop",
"required": true,
"hide": false
}
],
"flags": [],
"mounts": [],
"hide": false,
"help": "Kill a running daemon",
"help": "Sends a stop signal to a daemon",
"name": "stop",
"aliases": [
"kill"
Expand Down
2 changes: 1 addition & 1 deletion docs/cli/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
- [`pitchfork run [-f --force] <NAME> [CMD]...`](/cli/run.md)
- [`pitchfork start [NAME]...`](/cli/start.md)
- [`pitchfork status <ID>`](/cli/status.md)
- [`pitchfork stop`](/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
10 changes: 8 additions & 2 deletions docs/cli/stop.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,12 @@
# `pitchfork stop`

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

Kill a running daemon
Sends a stop signal to a daemon

## Arguments

### `<ID>`

The name of the daemon to stop
3 changes: 2 additions & 1 deletion pitchfork.usage.kdl
Original file line number Diff line number Diff line change
Expand Up @@ -65,8 +65,9 @@ cmd "status" help="Display the status of a daemon" {
alias "stat"
arg "<ID>"
}
cmd "stop" help="Kill a running daemon" {
cmd "stop" help="Sends a stop signal to a daemon" {
alias "kill"
arg "<ID>" help="The name of the daemon to stop"
}
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
19 changes: 16 additions & 3 deletions src/cli/list.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,14 +19,27 @@ impl List {
.load_preset(comfy_table::presets::NOTHING)
.set_content_arrangement(ContentArrangement::Dynamic);
if !self.hide_header && console::user_attended() {
table.set_header(vec!["Name", "PID", "Status"]);
table.set_header(vec!["Name", "PID", "Status", ""]);
}

for (id, daemon) in StateFile::get().daemons.iter() {
let sf = StateFile::get();
for (id, daemon) in sf.daemons.iter() {
table.add_row(vec![
Cell::new(id),
Cell::new(daemon.pid.to_string()),
Cell::new(
daemon
.pid
.as_ref()
.map(|p| p.to_string())
.unwrap_or_default(),
),
Cell::new(daemon.status.style()),
Cell::new(
sf.disabled
.contains(id)
.then_some("disabled")
.unwrap_or_default(),
),
]);
}

Expand Down
6 changes: 5 additions & 1 deletion src/cli/run.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,11 @@ impl Run {
loop {
match ipc.read().await? {
IpcMessage::DaemonStart(daemon) => {
info!("Started daemon {} with pid {}", daemon.name, daemon.pid);
info!(
"Started daemon {} with pid {}",
daemon.name,
daemon.pid.unwrap()
);
break;
}
IpcMessage::DaemonFailed { name, error } => {
Expand Down
6 changes: 4 additions & 2 deletions src/cli/status.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,10 @@ impl Status {
let daemon = StateFile::get().daemons.get(&self.id);
if let Some(daemon) = daemon {
println!("Name: {}", self.id);
println!("PID: {}", daemon.pid);
println!("Status: {}", daemon.status);
if let Some(pid) = &daemon.pid {
println!("PID: {pid}");
}
println!("Status: {}", daemon.status.style());
} else {
warn!("Daemon {} not found", self.id);
}
Expand Down
18 changes: 16 additions & 2 deletions src/cli/stop.rs
Original file line number Diff line number Diff line change
@@ -1,12 +1,26 @@
use crate::cli::supervisor::kill_or_stop;
use crate::state_file::StateFile;
use crate::Result;

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

impl Stop {
pub async fn run(&self) -> Result<()> {
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)?;
return Ok(());
}
}
warn!("{} is not running", self.id);
Ok(())
}
}
6 changes: 4 additions & 2 deletions src/cli/supervisor/run.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,10 @@ impl Run {
pub async fn run(&self) -> Result<()> {
let pid_file = StateFile::read(&*env::PITCHFORK_STATE_FILE)?;
if let Some(d) = pid_file.daemons.get("pitchfork") {
if !(kill_or_stop(d.pid, self.force)?) {
return Ok(());
if let Some(pid) = d.pid {
if !(kill_or_stop(pid, self.force)?) {
return Ok(());
}
}
}

Expand Down
10 changes: 6 additions & 4 deletions src/cli/supervisor/start.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,12 @@ pub struct Start {

impl Start {
pub async fn run(&self) -> Result<()> {
let pid_file = StateFile::read(&*env::PITCHFORK_STATE_FILE)?;
if let Some(d) = pid_file.daemons.get("pitchfork") {
if !(kill_or_stop(d.pid, self.force)?) {
return Ok(());
let sf = StateFile::get();
if let Some(d) = sf.daemons.get("pitchfork") {
if let Some(pid) = d.pid {
if !(kill_or_stop(pid, self.force)?) {
return Ok(());
}
}
}

Expand Down
15 changes: 9 additions & 6 deletions src/cli/supervisor/stop.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use crate::cli::supervisor::kill_or_stop;
use crate::state_file::StateFile;
use crate::{env, Result};
use crate::Result;

/// Stops the internal pitchfork daemon running in the background
#[derive(Debug, clap::Args)]
Expand All @@ -9,13 +9,16 @@ pub struct Stop {}

impl Stop {
pub async fn run(&self) -> Result<()> {
let pid_file = StateFile::read(&*env::PITCHFORK_STATE_FILE)?;
let pid_file = StateFile::get();
if let Some(d) = pid_file.daemons.get("pitchfork") {
info!("Stopping pitchfork daemon with pid {}", d.pid);
kill_or_stop(d.pid, true)?;
} else {
warn!("Pitchfork daemon is not running");
if let Some(pid) = d.pid {
info!("Stopping pitchfork daemon with pid {}", pid);
if !(kill_or_stop(pid, false)?) {
return Ok(());
}
}
}
warn!("Pitchfork daemon is not running");
Ok(())
}
}
4 changes: 3 additions & 1 deletion src/state_file.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ pub struct StateFile {
#[derive(Debug, Clone, serde::Serialize, serde::Deserialize)]
pub struct StateFileDaemon {
pub name: String,
pub pid: u32,
pub pid: Option<u32>,
pub status: DaemonStatus,
}

Expand All @@ -27,6 +27,7 @@ pub enum DaemonStatus {
Failed(String),
Waiting,
Running,
Stopped,
}

impl DaemonStatus {
Expand All @@ -36,6 +37,7 @@ impl DaemonStatus {
DaemonStatus::Failed(_) => console::style(s).red().to_string(),
DaemonStatus::Waiting => console::style(s).yellow().to_string(),
DaemonStatus::Running => console::style(s).green().to_string(),
DaemonStatus::Stopped => console::style(s).dim().to_string(),
}
}
}
Expand Down
16 changes: 11 additions & 5 deletions src/supervisor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ impl Supervisor {

let daemon = StateFileDaemon {
name: "pitchfork".into(),
pid,
pid: Some(pid),
status: DaemonStatus::Running,
};
self.state_file.daemons.insert("pitchfork".into(), daemon);
Expand Down Expand Up @@ -103,13 +103,19 @@ impl Supervisor {
self.handle_ipc(msg, send).await
}
Event::Signal => {
info!("received SIGTERM, stopping");
info!("received signal, stopping");
self.close();
exit(0)
}
Event::DaemonStop(daemon) => {
self.active_pids.remove(&daemon.pid);
self.state_file.daemons.remove(&daemon.name);
self.active_pids.remove(&daemon.pid.unwrap());
self.state_file
.daemons
.entry(daemon.name.clone())
.and_modify(|d| {
d.pid = None;
d.status = DaemonStatus::Stopped;
});
self.state_file.write()?;
Ok(())
}
Expand Down Expand Up @@ -186,7 +192,7 @@ impl Supervisor {
info!("started daemon {name} with pid {pid}");
let daemon = StateFileDaemon {
name: name.to_string(),
pid,
pid: Some(pid),
status: DaemonStatus::Running,
};
self.state_file
Expand Down

0 comments on commit db84961

Please sign in to comment.