From f4c53359eeca8b15e4761341eef3161e58b6ce4e Mon Sep 17 00:00:00 2001 From: Nordine Bittich Date: Thu, 29 Aug 2024 21:54:34 +0200 Subject: [PATCH] add daemon feature --- README.md | 5 ++++- adana-shell/src/args.rs | 12 ++++++++++++ adana-shell/src/main.rs | 15 ++++++++++++--- 3 files changed, 28 insertions(+), 4 deletions(-) diff --git a/README.md b/README.md index c3a3067..2282a19 100644 --- a/README.md +++ b/README.md @@ -713,7 +713,7 @@ fact(10) You can dynamically load a script written in adana in the repl. Assuming you've cloned the repo and you use docker, here's an example of how to do it. -Note that the extension can be anything. +Note that the extension must be `.adana`. - map the example directory as a docker volume: @@ -884,6 +884,9 @@ RUST_LOG=adana=debug adana # using file adana -sp /path/to/script.adana +# using file with --daemon feature +adana -sp /path/to/script.adana -d + # using code adana -e 1+1 ``` diff --git a/adana-shell/src/args.rs b/adana-shell/src/args.rs index 096af1e..0e680a1 100644 --- a/adana-shell/src/args.rs +++ b/adana-shell/src/args.rs @@ -3,6 +3,7 @@ use anyhow::Context; pub enum Argument { InMemory, Execute(String), + Daemon, ScriptPath(String), DbPath(String), NoFallbackInMemory, @@ -55,6 +56,17 @@ pub fn parse_args( ); arguments.push(Argument::InMemory); } + "--daemon" | "-d" => { + anyhow::ensure!( + !arguments.iter().any(|a| matches!(a, Argument::InMemory)), + "daemon should be specified only once!" + ); + anyhow::ensure!( + arguments.iter().any(|a| matches!(a, Argument::ScriptPath(_))), + "script path must be specified first when having the daemon feature on! " + ); + arguments.push(Argument::Daemon); + } "--no-fallback" | "-nofb" => { anyhow::ensure!( !arguments diff --git a/adana-shell/src/main.rs b/adana-shell/src/main.rs index f7f366e..20f87d3 100644 --- a/adana-shell/src/main.rs +++ b/adana-shell/src/main.rs @@ -12,6 +12,7 @@ use nu_ansi_term::Color::LightBlue; use nu_ansi_term::Style; use rustyline::error::ReadlineError; use std::collections::BTreeMap; +use std::time::Duration; use std::{ borrow::Cow, path::{Path, PathBuf}, @@ -95,6 +96,7 @@ fn main() -> anyhow::Result<()> { None } }); + let is_daemon = args.iter().any(|a| matches!(a, Argument::Daemon)); let mut direct_execution_script = args.iter().find_map(|a| { if let Argument::Execute(script) = a { @@ -104,6 +106,10 @@ fn main() -> anyhow::Result<()> { } }); + ctrlc::set_handler(|| { + debug!("catch CTRL-C! DO NOT REMOVE this. receive ctrl+c signal 2"); + std::process::exit(0); + })?; let script = if let Some(script_path) = script_path { let pb = PathBuf::from(&script_path); if !pb.exists() { @@ -132,6 +138,7 @@ fn main() -> anyhow::Result<()> { } else { direct_execution_script.take() }; + if let Some(script) = script { let mut script_context = BTreeMap::new(); @@ -147,12 +154,14 @@ fn main() -> anyhow::Result<()> { Ok(calc) => println!("{calc}"), Err(calc_err) => eprintln!("Error: {calc_err:?}"), } + if is_daemon { + loop { + std::thread::sleep(Duration::from_millis(50)); + } + } return Ok(()); } - ctrlc::set_handler(|| { - debug!("catch CTRL-C! DO NOT REMOVE this. receive ctrl+c signal 2") - })?; clear_terminal(); println!("{PKG_NAME} v{VERSION} (rust version: {RUST_VERSION})"); println!("shared lib path: {path_to_shared_lib:?}");