feat: Write a pidfile for people to use when communicating to us

This commit is contained in:
Jeremy Wall 2024-06-04 19:53:19 -04:00
parent 76828b7e57
commit ece2e6ad7a
2 changed files with 35 additions and 6 deletions

View File

@ -20,6 +20,7 @@ Arguments:
Options: Options:
-e, --err-path <STDERR_PATH> Path to write stderr to -e, --err-path <STDERR_PATH> Path to write stderr to
-o, --out-path <STDOUT_PATH> Path to write stdout to -o, --out-path <STDOUT_PATH> Path to write stdout to
-p, --pid-file <PID_FILE> Path to the place to write a pidfile to
--sig <ROTATED_SIGNAL> Signal notifiying that the file paths have been rotated [default: sighup] [possible values: sighup, sigusr1, sigusr2] --sig <ROTATED_SIGNAL> Signal notifiying that the file paths have been rotated [default: sighup] [possible values: sighup, sigusr1, sigusr2]
-h, --help Print help -h, --help Print help
-V, --version Print version -V, --version Print version
@ -28,5 +29,5 @@ Options:
## Example ## Example
```sh ```sh
clio --err-path /var/log/app.err.log --out-path /var/log/app.out.log --sig sighup -- app --flag1 --flag2 clio --err-path /var/log/app.err.log --out-path /var/log/app.out.log --sig sighup --pid-file app.pid -- app --flag1 --flag2
``` ```

View File

@ -34,12 +34,26 @@ struct Args {
stderr_path: PathBuf, stderr_path: PathBuf,
#[arg(short = 'o', long = "out-path", help = "Path to write stdout to")] #[arg(short = 'o', long = "out-path", help = "Path to write stdout to")]
stdout_path: PathBuf, stdout_path: PathBuf,
#[arg(
short = 'p',
long = "pid-file",
help = "Path to the place to write a pidfile to"
)]
pid_file: Option<PathBuf>,
#[arg(long = "sig", value_enum, help="Signal notifiying that the file paths have been rotated", default_value_t = HandledSignals::SIGHUP)] #[arg(long = "sig", value_enum, help="Signal notifiying that the file paths have been rotated", default_value_t = HandledSignals::SIGHUP)]
rotated_signal: HandledSignals, rotated_signal: HandledSignals,
#[arg(last = true, help="Command to run")] #[arg(last = true, help = "Command to run")]
cmd: Vec<String>, cmd: Vec<String>,
} }
async fn write_pid_file(p: &PathBuf) -> anyhow::Result<()> {
let mut pid_file = File::options().create(true).truncate(true).open(p).await?;
let id = std::process::id().to_string();
pid_file.write(id.as_bytes()).await?;
pid_file.sync_all().await?;
Ok(())
}
#[tokio::main] #[tokio::main]
async fn main() -> anyhow::Result<ExitCode> { async fn main() -> anyhow::Result<ExitCode> {
let args = Args::parse(); let args = Args::parse();
@ -61,16 +75,30 @@ async fn main() -> anyhow::Result<ExitCode> {
.stderr(Stdio::piped()) .stderr(Stdio::piped())
.spawn()?; .spawn()?;
let mut stdout_reader = child let mut stdout_reader = child
.stdout.take() .stdout
.take()
.expect("no valid stdout from command available"); .expect("no valid stdout from command available");
let mut stdout_buffer = [0; 8 * 1024]; let mut stdout_buffer = [0; 8 * 1024];
let mut stderr_reader = child let mut stderr_reader = child
.stderr.take() .stderr
.take()
.expect("no valid stderr from command available"); .expect("no valid stderr from command available");
let mut stderr_buffer = [0; 8 * 1024]; let mut stderr_buffer = [0; 8 * 1024];
let mut stderr_writer = File::options().append(true).create(true).open(stderr_path).await?; let mut stderr_writer = File::options()
let mut stdout_writer = File::options().append(true).create(true).open(stdout_path).await?; .append(true)
.create(true)
.open(stderr_path)
.await?;
let mut stdout_writer = File::options()
.append(true)
.create(true)
.open(stdout_path)
.await?;
// TODO(jwall): Write our pidfile somehwere
if let Some(p) = args.pid_file {
write_pid_file(&p).await?
}
// TODO(jwall): Forward all other signals to the running process. // TODO(jwall): Forward all other signals to the running process.
loop { loop {
// NOTE(zaphar): Each select block will run exclusively of the other blocks using a // NOTE(zaphar): Each select block will run exclusively of the other blocks using a