Satisfying requested changes

This commit is contained in:
daubaris 2019-11-11 09:15:21 +02:00 committed by Félix Saparelli
parent 7ad8e78a34
commit 9a204241f7
2 changed files with 27 additions and 35 deletions

View File

@ -209,7 +209,7 @@ where
.long("no-shell")) .long("no-shell"))
.arg(Arg::with_name("once").short("1").hidden(true)) .arg(Arg::with_name("once").short("1").hidden(true))
.arg(Arg::with_name("watch-when-idle") .arg(Arg::with_name("watch-when-idle")
.help("Ignores commands while the process is still running") .help("Ignore events while the process is still running")
.short("W") .short("W")
.long("watch-when-idle")); .long("watch-when-idle"));

View File

@ -194,7 +194,7 @@ impl ExecHandler {
let guard = self let guard = self
.child_process .child_process
.read() .read()
.expect("poisoned lock in signal_process"); .expect("poisoned lock in has_running_process");
if let Some(ref _child) = *guard { if let Some(ref _child) = *guard {
return true; return true;
@ -228,54 +228,46 @@ impl Handler for ExecHandler {
// 3. Just send a specified signal to the child, do nothing more // 3. Just send a specified signal to the child, do nothing more
// 4. Make sure the previous run was ended, then run the command again // 4. Make sure the previous run was ended, then run the command again
// //
let scenario = (self.args.restart, self.signal.is_some()); let scenario = (
self.args.restart,
self.signal.is_some(),
self.args.watch_when_idle,
);
let running_process = self.has_running_process(); let running_process = self.has_running_process();
match scenario { match scenario {
// Custom restart behaviour (--restart was given, and --signal specified): // SIGHUP scenario: --signal was given, but --restart was not
// Send specified signal to the child, wait for it to exit, then run the command again // Just send a signal (e.g. SIGHUP) to the child, do nothing more
(true, true) => { (false, true, _) => signal_process(&self.child_process, self.signal, false),
if self.args.watch_when_idle {
if !running_process { // Spawn a process when there are no running processes in the background
self.spawn(ops)?; (_, _, true) => {
} if !running_process {
} else {
signal_process(&self.child_process, self.signal, true);
self.spawn(ops)?; self.spawn(ops)?;
} }
} }
// Custom restart behaviour (--restart was given, and --signal specified):
// Send specified signal to the child, wait for it to exit, then run the command again
(true, true, false) => {
signal_process(&self.child_process, self.signal, true);
self.spawn(ops)?;
}
// Default restart behaviour (--restart was given, but --signal wasn't specified): // Default restart behaviour (--restart was given, but --signal wasn't specified):
// Send SIGTERM to the child, wait for it to exit, then run the command again // Send SIGTERM to the child, wait for it to exit, then run the command again
(true, false) => { (true, false, false) => {
let sigterm = signal::new(Some("SIGTERM".into())); let sigterm = signal::new(Some("SIGTERM".into()));
if self.args.watch_when_idle { signal_process(&self.child_process, sigterm, true);
if !running_process { self.spawn(ops)?;
self.spawn(ops)?;
}
} else {
signal_process(&self.child_process, sigterm, true);
self.spawn(ops)?;
}
} }
// SIGHUP scenario: --signal was given, but --restart was not
// Just send a signal (e.g. SIGHUP) to the child, do nothing more
(false, true) => signal_process(&self.child_process, self.signal, false),
// Default behaviour (neither --signal nor --restart specified): // Default behaviour (neither --signal nor --restart specified):
// Make sure the previous run was ended, then run the command again // Make sure the previous run was ended, then run the command again
(false, false) => { (false, false, false) => {
if self.args.watch_when_idle { signal_process(&self.child_process, None, true);
if !running_process { self.spawn(ops)?;
self.spawn(ops)?;
}
} else {
signal_process(&self.child_process, None, true);
self.spawn(ops)?;
}
} }
} }