Skip to content

Commit 67ef842

Browse files
committed
feat: databases now automatically close at the end of the script
1 parent 14fa6ed commit 67ef842

File tree

3 files changed

+41
-14
lines changed

3 files changed

+41
-14
lines changed

Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,7 @@ sha3 = "0.10.8"
9595
base64 = "0.22.1"
9696
bytes = { version = "1.11.1", features = ["serde"] }
9797
chrono = { version = "0.4.43", features = ["serde"] }
98+
markdown = { version = "1.0.0", features = ["serde"] }
9899

99100
# runtime
100101
tokio = { version = "1.49.0", features = [
@@ -123,7 +124,6 @@ sqlx = { version = "0.8.6", features = [
123124
] }
124125
include_dir = "0.7.4"
125126
sqlite-vec = "0.1.6"
126-
markdown = { version = "1.0.0", features = ["serde"] }
127127

128128
[profile.release]
129129
opt-level = 3

src/commands/run.rs

Lines changed: 14 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -49,27 +49,31 @@ pub async fn run_command(
4949
spawn_termination_task();
5050

5151
// Remove the Shebang lines
52-
let user_file = user_file
52+
let mut user_file = user_file
5353
.lines()
5454
.filter(|line| !line.starts_with("#!"))
55-
.collect::<Vec<_>>()
56-
.join("\n");
55+
.collect::<Vec<_>>();
56+
user_file.push("astra_internal__close_all_databases()");
57+
let user_file = user_file.join("\n");
5758

58-
if actual_path_str == "<commandline>" {
59-
if let Err(e) = lua.load(user_file).set_name("<commandline>").exec_async().await {
60-
error!("{}", e);
61-
}
62-
} else if let Some(is_teal) = PathBuf::from(&actual_path_str).extension()
59+
if let Some(is_teal) = PathBuf::from(&actual_path_str).extension()
6360
&& is_teal == "tl"
6461
{
6562
if let Err(e) = crate::components::load_teal(lua).await {
6663
error!("{}", e);
6764
}
6865

69-
if let Err(e) = crate::components::execute_teal_code(lua, &actual_path_str, &user_file).await {
66+
if let Err(e) =
67+
crate::components::execute_teal_code(lua, &actual_path_str, &user_file).await
68+
{
7069
error!("{}", e);
7170
}
72-
} else if let Err(e) = lua.load(user_file).set_name(actual_path_str).exec_async().await {
71+
} else if let Err(e) = lua
72+
.load(user_file)
73+
.set_name(actual_path_str)
74+
.exec_async()
75+
.await
76+
{
7377
error!("{}", e);
7478
}
7579

src/components/global.rs

Lines changed: 26 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
use crate::components::database::DATABASE_POOLS;
12
use mlua::{LuaSerdeExt, UserData};
23

34
pub fn register_to_lua(lua: &mlua::Lua) -> mlua::Result<()> {
@@ -7,6 +8,7 @@ pub fn register_to_lua(lua: &mlua::Lua) -> mlua::Result<()> {
78
pprintln(lua)?;
89
AstraRegex::register_to_lua(lua)?;
910
uuid_v4(lua)?;
11+
close_dbs(lua)?;
1012
// env
1113
getenv(lua)?;
1214
setenv(lua)?;
@@ -18,6 +20,23 @@ pub fn register_to_lua(lua: &mlua::Lua) -> mlua::Result<()> {
1820
Ok(())
1921
}
2022

23+
// At the end of the script, the database files should be closed automatically
24+
pub fn close_dbs(lua: &mlua::Lua) -> mlua::Result<()> {
25+
lua.globals().set(
26+
"astra_internal__close_all_databases",
27+
lua.create_async_function(|_, _: ()| async {
28+
let database_pools = DATABASE_POOLS.lock().await.clone();
29+
for i in database_pools {
30+
match i {
31+
crate::components::database::DatabaseType::Postgres(pool) => pool.close().await,
32+
crate::components::database::DatabaseType::Sqlite(pool) => pool.close().await,
33+
}
34+
}
35+
Ok(())
36+
})?,
37+
)
38+
}
39+
2140
pub fn dotenv_function(lua: &mlua::Lua) -> mlua::Result<()> {
2241
lua.globals().set(
2342
"astra_internal__dotenv_load",
@@ -29,15 +48,19 @@ pub fn dotenv_function(lua: &mlua::Lua) -> mlua::Result<()> {
2948
}
3049

3150
fn lua_print(args: mlua::MultiValue) {
51+
let mut padding = " ";
52+
if args.len() == 1 {
53+
padding = ""
54+
}
3255
for input in args.iter() {
3356
if input.is_string()
3457
&& let Ok(s) = input.to_string()
3558
{
36-
print!("{} ", s);
59+
print!("{}{padding}", s);
3760
} else if input.is_userdata() {
38-
print!("{input:?} ")
61+
print!("{input:?}{padding}")
3962
} else {
40-
print!("{input:#?} ")
63+
print!("{input:#?}{padding}")
4164
}
4265
}
4366
}

0 commit comments

Comments
 (0)