mirror of
https://github.com/MedzikUser/servers
synced 2024-08-14 23:57:48 +00:00
fix(plugin): execute on_load
function
Now function `on_load` will be executed if the plugin is loads. Added span to the logger on TRACE level in plugin loader. Fixed clippy warning from previous commit.
This commit is contained in:
parent
67fb1a0a3c
commit
d31e0fff2f
2 changed files with 15 additions and 3 deletions
|
@ -1,7 +1,8 @@
|
||||||
use std::{fs, path::Path, sync::Arc};
|
use std::{fs, path::Path, sync::Arc};
|
||||||
|
|
||||||
|
use async_std::task;
|
||||||
use libloading::{Library, Symbol};
|
use libloading::{Library, Symbol};
|
||||||
use tracing::{info, trace};
|
use tracing::{info, span, trace, Level};
|
||||||
|
|
||||||
use crate::{
|
use crate::{
|
||||||
commands,
|
commands,
|
||||||
|
@ -30,6 +31,10 @@ pub fn loader(plugins_dir: &str) -> anyhow::Result<PluginsManagerType> {
|
||||||
let path = plugin_path?.path();
|
let path = plugin_path?.path();
|
||||||
let path_str = path.to_str().unwrap();
|
let path_str = path.to_str().unwrap();
|
||||||
|
|
||||||
|
// add span to logger
|
||||||
|
let span = span!(Level::TRACE, "", plugin_path = path_str);
|
||||||
|
let _enter = span.enter();
|
||||||
|
|
||||||
info!("Loading plugin {}", path_str);
|
info!("Loading plugin {}", path_str);
|
||||||
|
|
||||||
// loading library from .so is unsafe
|
// loading library from .so is unsafe
|
||||||
|
@ -48,5 +53,11 @@ pub fn loader(plugins_dir: &str) -> anyhow::Result<PluginsManagerType> {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
for plugin in plugins_manager.plugins.iter() {
|
||||||
|
// execute the `on_load` function from the plugin
|
||||||
|
task::block_on(async { plugin.on_load().await });
|
||||||
|
info!("Loaded plugin {}.", plugin.name());
|
||||||
|
}
|
||||||
|
|
||||||
Ok(Arc::new(plugins_manager))
|
Ok(Arc::new(plugins_manager))
|
||||||
}
|
}
|
||||||
|
|
|
@ -90,15 +90,16 @@ async fn process(client: Client) -> anyhow::Result<()> {
|
||||||
// to block a command return error in the `onCommand` event
|
// to block a command return error in the `onCommand` event
|
||||||
if let Some((_i, cmd)) = command {
|
if let Some((_i, cmd)) = command {
|
||||||
// run `onCommand` events
|
// run `onCommand` events
|
||||||
if let Ok(_) = client
|
if client
|
||||||
.run_events(
|
.run_events(
|
||||||
EventType::OnCommand,
|
EventType::OnCommand,
|
||||||
EventData::Command(cmd.name().to_string()),
|
EventData::Command(cmd.name().to_string()),
|
||||||
)
|
)
|
||||||
.await
|
.await
|
||||||
|
.is_ok()
|
||||||
{
|
{
|
||||||
// execute command
|
// execute command
|
||||||
cmd.execute(&client, args).await?;
|
cmd.execute(client, args).await?;
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
client.send("unknown command")?;
|
client.send("unknown command")?;
|
||||||
|
|
Loading…
Reference in a new issue