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:
MedzikUser 2022-08-17 22:05:27 +02:00
parent 67fb1a0a3c
commit d31e0fff2f
No known key found for this signature in database
GPG Key ID: A5FAC1E185C112DB
2 changed files with 15 additions and 3 deletions

View File

@ -1,7 +1,8 @@
use std::{fs, path::Path, sync::Arc};
use async_std::task;
use libloading::{Library, Symbol};
use tracing::{info, trace};
use tracing::{info, span, trace, Level};
use crate::{
commands,
@ -30,6 +31,10 @@ pub fn loader(plugins_dir: &str) -> anyhow::Result<PluginsManagerType> {
let path = plugin_path?.path();
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);
// 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))
}

View File

@ -90,15 +90,16 @@ async fn process(client: Client) -> anyhow::Result<()> {
// to block a command return error in the `onCommand` event
if let Some((_i, cmd)) = command {
// run `onCommand` events
if let Ok(_) = client
if client
.run_events(
EventType::OnCommand,
EventData::Command(cmd.name().to_string()),
)
.await
.is_ok()
{
// execute command
cmd.execute(&client, args).await?;
cmd.execute(client, args).await?;
}
} else {
client.send("unknown command")?;