Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Better pid error messages #467

Merged
merged 1 commit into from
Oct 5, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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