Skip to content

Commit

Permalink
Merge pull request #467 from Nukesor/better-pid-error-messages
Browse files Browse the repository at this point in the history
Better pid error messages
  • Loading branch information
Nukesor authored Oct 5, 2023
2 parents 96b5fef + e79818e commit 94ecaaf
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 10 deletions.
3 changes: 2 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),

### Fixed

- Support parameter parsing for signal names with capslock (`SIGINT`) and short name (`INT`|`int`). [#455](https://github.com/Nukesor/pueue/issues/455).
- Support parameter parsing for signal names with capslock (`SIGINT`) and short name (`INT`|`int`). [#455](https://github.com/Nukesor/pueue/issues/455)
- Better error messages for pid related I/O errors. [#466](https://github.com/Nukesor/pueue/issues/466)

### Changed

Expand Down
15 changes: 7 additions & 8 deletions pueue/src/daemon/pid.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,11 @@ use pueue_lib::process_helper::process_exists;
/// Read a PID file and throw an error, if another daemon instance is still running.
fn check_for_running_daemon(pid_path: &Path) -> Result<()> {
info!("Placing pid file at {pid_path:?}");
let mut file =
File::open(pid_path).map_err(|err| Error::IoError("opening pid file".to_string(), err))?;
let mut file = File::open(pid_path)
.map_err(|err| Error::IoPathError(pid_path.to_path_buf(), "opening pid file", err))?;
let mut pid = String::new();
file.read_to_string(&mut pid)
.map_err(|err| Error::IoError("reading pid file".to_string(), err))?;
.map_err(|err| Error::IoPathError(pid_path.to_path_buf(), "reading pid file", err))?;

let pid: u32 = pid
.parse()
Expand All @@ -40,18 +40,17 @@ pub fn create_pid_file(pid_path: &Path) -> Result<()> {
check_for_running_daemon(pid_path)?;
}
let mut file = File::create(pid_path)
.map_err(|err| Error::IoError("creating pid file".to_string(), err))?;
.map_err(|err| Error::IoPathError(pid_path.to_path_buf(), "creating pid file", err))?;

file.write_all(std::process::id().to_string().as_bytes())
.map_err(|err| Error::IoError("writing pid file".to_string(), err))?;
.map_err(|err| Error::IoPathError(pid_path.to_path_buf(), "writing pid file", err))?;

Ok(())
}

/// Remove the daemon's pid file.
/// Errors if it doesn't exist or cannot be deleted.
pub fn cleanup_pid_file(pid_path: &Path) -> Result<()> {
pub fn cleanup_pid_file(pid_path: &Path) -> Result<(), Error> {
std::fs::remove_file(pid_path)
.map_err(|err| Error::IoError("removing pid file".to_string(), err))?;
Ok(())
.map_err(|err| Error::IoPathError(pid_path.to_path_buf(), "removing pid file", err))
}
2 changes: 1 addition & 1 deletion pueue/src/daemon/task_handler/callback.rs
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ impl TaskHandler {
// Format and insert start and end times.
let print_time = |time: Option<DateTime<Local>>| {
time.map(|time| time.timestamp().to_string())
.unwrap_or_else(String::new)
.unwrap_or_default()
};
parameters.insert("start", print_time(task.start));
parameters.insert("end", print_time(task.end));
Expand Down

0 comments on commit 94ecaaf

Please sign in to comment.