Skip to content

Conversation

@passy
Copy link

@passy passy commented Jun 2, 2023

Hi and thank you so much for making this amazingly useful crate.

The error mod seems incredibly useful to treat errors differently, but unless I'm missing something (and that's not at all unlikely given that I'm a bit of a noob), the only way to handle errors right now is by comparing their Display representations.

I would quite like to use the ErrorKind object for that, to do something like this for instance and treat a lock failure as an expected event.

use std::fs::File;
use std::time::Duration;

use daemonize::Daemonize;

fn main() -> Result<(), Box<dyn std::error::Error>> {
    let stdout = File::create("/tmp/daemon.out").unwrap();
    let stderr = File::create("/tmp/daemon.err").unwrap();

    let daemonize = Daemonize::new()
        .pid_file("/tmp/test.pid") // Every method except `new` and `start`
        .chown_pid_file(true) // is optional, see `Daemonize` documentation
        .working_directory("/tmp") // for default behaviour.
        .stdout(stdout) // Redirect stdout to `/tmp/daemon.out`.
        .stderr(stderr) // Redirect stderr to `/tmp/daemon.err`.
        .privileged_action(|| "Executed before drop privileges");

    match daemonize.start() {
        Ok(_) => {
            println!("Success, daemonized");
            std::thread::sleep(Duration::from_secs(60));
            Ok(())
        }
        Err(e) => match e.kind {
            daemonize::ErrorKind::LockPidfile(_) => {
                println!("Daemon is already running");
                Ok(())
            }
            _ => {
                eprintln!("Error: {}", e);
                Err(e.into())
            }
        },
    }
}

Please let me know if that's that's something you would consider or if you'd like to have any changes made.

@passy passy changed the title Make error mod public Make ErrorKind public Jun 2, 2023
@passy passy marked this pull request as ready for review June 2, 2023 14:00
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant