Skip to content

Commit

Permalink
add daemon feature
Browse files Browse the repository at this point in the history
  • Loading branch information
nbittich committed Aug 29, 2024
1 parent 79c047c commit f4c5335
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 4 deletions.
5 changes: 4 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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:

Expand Down Expand Up @@ -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
```
Expand Down
12 changes: 12 additions & 0 deletions adana-shell/src/args.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ use anyhow::Context;
pub enum Argument {
InMemory,
Execute(String),
Daemon,
ScriptPath(String),
DbPath(String),
NoFallbackInMemory,
Expand Down Expand Up @@ -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
Expand Down
15 changes: 12 additions & 3 deletions adana-shell/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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},
Expand Down Expand Up @@ -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 {
Expand All @@ -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() {
Expand Down Expand Up @@ -132,6 +138,7 @@ fn main() -> anyhow::Result<()> {
} else {
direct_execution_script.take()
};

if let Some(script) = script {
let mut script_context = BTreeMap::new();

Expand All @@ -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:?}");
Expand Down

0 comments on commit f4c5335

Please sign in to comment.