feat: commit plugin error handle

This commit is contained in:
YuKun Liu 2022-10-01 15:17:09 -07:00
parent 762093094f
commit 77393b273e
3 changed files with 71 additions and 16 deletions

31
Cargo.lock generated
View file

@ -772,6 +772,12 @@ dependencies = [
"winapi", "winapi",
] ]
[[package]]
name = "either"
version = "1.8.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "90e5c1c8368803113bf0c9584fc495a58b86dc8a29edbf8fe877d21d9507e797"
[[package]] [[package]]
name = "encode_unicode" name = "encode_unicode"
version = "0.3.6" version = "0.3.6"
@ -1391,6 +1397,15 @@ dependencies = [
"winapi", "winapi",
] ]
[[package]]
name = "itertools"
version = "0.10.5"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "b0fd2260e829bddf4cb6ea802289de2f86d6a7a690192fbe91b3f46e0f2c8473"
dependencies = [
"either",
]
[[package]] [[package]]
name = "itoa" name = "itoa"
version = "1.0.3" version = "1.0.3"
@ -1608,12 +1623,28 @@ dependencies = [
"futures-util", "futures-util",
"lua-src", "lua-src",
"luajit-src", "luajit-src",
"mlua_derive",
"num-traits", "num-traits",
"once_cell", "once_cell",
"pkg-config", "pkg-config",
"rustc-hash", "rustc-hash",
] ]
[[package]]
name = "mlua_derive"
version = "0.8.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "b9214e60d3cf1643013b107330fcd374ccec1e4ba1eef76e7e5da5e8202e71c0"
dependencies = [
"itertools",
"once_cell",
"proc-macro-error",
"proc-macro2",
"quote",
"regex",
"syn",
]
[[package]] [[package]]
name = "native-tls" name = "native-tls"
version = "0.2.10" version = "0.2.10"

View file

@ -61,7 +61,7 @@ proc-macro2 = { version = "1.0", features = ["span-locations"] }
lazy_static = "1.4.0" lazy_static = "1.4.0"
# plugin packages # plugin packages
mlua = { version = "0.8.1", features = ["lua54", "vendored", "async", "send"] } mlua = { version = "0.8.1", features = ["lua54", "vendored", "async", "send", "macros"] }
ctrlc = "3.2.3" ctrlc = "3.2.3"
[[bin]] [[bin]]

View file

@ -4,7 +4,7 @@ use std::{
sync::Mutex, sync::Mutex,
}; };
use mlua::{AsChunk, Lua, Table}; use mlua::{AsChunk, chunk, Lua, Table};
use serde::{Deserialize, Serialize}; use serde::{Deserialize, Serialize};
use serde_json::json; use serde_json::json;
@ -108,8 +108,14 @@ impl PluginManager {
lua.globals().set("manager", manager).unwrap(); lua.globals().set("manager", manager).unwrap();
for (idx, path, info) in init_list { for (idx, path, info) in init_list {
let res = lua.load(&format!("manager[{idx}].on_init()")).exec(); let res = lua
if res.is_ok() { .load(mlua::chunk! {
manager[$idx].on_init()
})
.eval::<bool>();
match res {
Ok(true) => {
// plugin init success, create `dcp.json` file.
let mut file = std::fs::File::create(path).unwrap(); let mut file = std::fs::File::create(path).unwrap();
let value = json!({ let value = json!({
"name": info.name, "name": info.name,
@ -122,6 +128,20 @@ impl PluginManager {
let buffer = buffer.as_bytes(); let buffer = buffer.as_bytes();
file.write_all(buffer).unwrap(); file.write_all(buffer).unwrap();
} }
Ok(false) => {
log::warn!("Plugin init function result is `false`, init failed.");
let _ = lua.load(mlua::chunk! {
table.remove(manager, $idx)
}).exec();
}
Err(e) => {
// plugin init failed
let _ = lua.load(mlua::chunk! {
table.remove(manager, $idx)
}).exec();
log::warn!("Plugin init failed: {e}");
}
}
} }
return Ok(()); return Ok(());
} }
@ -269,4 +289,8 @@ impl PluginManager {
res res
} }
pub fn plugin_status() {
let lua = LUA.lock().unwrap();
}
} }