Prevent notifications from interfering with ctrl-c register

This commit is contained in:
kpcyrd
2020-06-23 04:55:16 +02:00
parent da0e6aa482
commit 63f226f68f
5 changed files with 53 additions and 10 deletions

View File

@@ -0,0 +1,10 @@
-- Description: TODO your description here
-- Version: 0.1.0
-- License: GPL-3.0
-- Source: subdomains
function run(arg)
db_update('subdomain', arg, {
resolvable=true,
})
end

View File

@@ -0,0 +1,13 @@
-- Description: TODO your description here
-- Version: 0.1.0
-- License: GPL-3.0
-- Source: domains
function run(arg)
for i=1, 20 do
db_add('subdomain', {
domain_id=arg['id'],
value=http_mksession() .. '.' .. arg['value'],
})
end
end

View File

@@ -67,14 +67,20 @@ fn print_summary(module: &Module, sent: usize, errors: usize) {
}
fn send(args: SendArgs, rl: &mut Shell) -> Result<()> {
rl.signal_register().catch_ctrl();
notify::run_router(rl, &mut term::Term, args.dry_run, &args.topic, &args.notification)?;
rl.signal_register().reset_ctrlc();
Ok(())
}
fn exec(args: ExecArgs, rl: &mut Shell) -> Result<()> {
let module = rl.library().get(&args.module)?.clone();
let options = Opt::collect(&args.options);
rl.signal_register().catch_ctrl();
let errors = notify::exec(rl, &module, options, args.verbose, &args.notification)?;
rl.signal_register().reset_ctrlc();
print_summary(&module, 1, errors);
Ok(())
}

View File

@@ -98,15 +98,15 @@ fn apply_rule<T>(name: &str, filter: &[T], value: &str, cmp: fn(&T, &str) -> boo
}
impl NotificationConfig {
fn matches(&self, workspace: &str, topic: &str) -> bool {
debug!("testing notification with rules");
fn matches(&self, name: &str, workspace: &str, topic: &str) -> bool {
debug!("Testing notification with rules: {:?}", name);
if !apply_rule("workspace", &self.workspaces, workspace, |filter, value| filter == value) {
return false;
}
if !apply_rule("topic", &self.topics, topic, |filter, value| filter.matches(value)) {
return false;
}
debug!("notification matches this config");
debug!("Notification matches this config");
true
}
}
@@ -121,6 +121,9 @@ fn prepare_arg(notification: &Notification) -> Result<(serde_json::Value, Option
}
pub fn exec(rl: &mut Shell, module: &Module, options: HashMap<String, String>, verbose: u64, notification: &Notification) -> Result<usize> {
let module_name = module.canonical();
debug!("Setting up notification execution with {:?}", module_name);
if *module.source() != Some(Source::Notifications) {
bail!("Module doesn't take notifications as source");
}
@@ -138,9 +141,9 @@ pub fn exec(rl: &mut Shell, module: &Module, options: HashMap<String, String>, v
prepare_keyring(rl.keyring_mut(), &module, &params)?;
let args = vec![prepare_arg(&notification)?];
rl.signal_register().catch_ctrl();
debug!("Executing notification module {:?}", module_name);
let errors = worker::spawn(rl, &module, args, &params, rl.config().network.proxy.clone(), options);
rl.signal_register().reset_ctrlc();
debug!("Notification module {:?} exited with {:?} errors", module_name, errors);
Ok(errors)
}
@@ -148,29 +151,37 @@ pub fn exec(rl: &mut Shell, module: &Module, options: HashMap<String, String>, v
pub fn run_router<T: SpinLogger>(rl: &mut Shell, spinner: &mut T, dry_run: bool, topic: &str, notification: &Notification) -> Result<()> {
let configs = rl.config().notifications.clone();
debug!("Running notification router");
for (name, config) in configs {
if config.matches(rl.workspace(), topic) {
if rl.signal_register().ctrlc_received() {
debug!("Exiting notification router due to ctrl-c");
break;
}
if config.matches(&name, rl.workspace(), topic) {
let module = rl.library().get(&config.script)?.clone();
if dry_run {
spinner.success(&format!("Executed {} {:?} (dry-run)", module.canonical(), name));
spinner.success(&format!("Executed {} for {:?} (dry-run)", module.canonical(), name));
} else {
let options = options::Opt::collect(&config.options);
match exec(rl, &module, options, 0, notification) {
Ok(0) => {
let msg = format!("Executed {} {:?}", module.canonical(), name);
let msg = format!("Executed {} for {:?}", module.canonical(), name);
spinner.success(&msg);
},
Ok(errors) => {
let msg = format!("Executed {} {:?} ({} errors)", module.canonical(), name, errors);
let msg = format!("Executed {} for {:?} ({} errors)", module.canonical(), name, errors);
spinner.error(&msg);
},
Err(err) => {
spinner.error(&format!("Fatal {} {:?}: {}", module.canonical(), name, err));
spinner.error(&format!("Fatal {} for {:?}: {}", module.canonical(), name, err));
},
}
}
}
}
debug!("Notification router finished");
Ok(())
}

View File

@@ -453,6 +453,7 @@ pub fn spawn(rl: &mut Shell, module: &Module, args: Vec<(serde_json::Value, Opti
let pool = ThreadPool::new(params.threads);
let mut expected = 0;
debug!("Preparing to spawn scripts for {:?} structs", args.len());
for (arg, pretty_arg, blobs) in args {
let name = match pretty_arg {
Some(pretty_arg) => format!("{:?}", pretty_arg),
@@ -465,9 +466,11 @@ pub fn spawn(rl: &mut Shell, module: &Module, args: Vec<(serde_json::Value, Opti
let options = options.clone();
let signal_register = rl.signal_register().clone();
pool.execute(move || {
debug!("Thread pool job became active");
let tx = EventSender::new(name, tx);
if signal_register.ctrlc_received() {
debug!("Thread pool job exits due to ctrl-c");
tx.send(Event2::Exit(ExitEvent::Ok));
return;
}