Skip to content
This repository has been archived by the owner on Oct 20, 2024. It is now read-only.

Commit

Permalink
fix(plugin): execute on_load function
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
M3DZIK committed Aug 17, 2022
1 parent 67fb1a0 commit d31e0ff
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 3 deletions.
13 changes: 12 additions & 1 deletion src/plugins/load.rs
Original file line number Diff line number Diff line change
@@ -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,
Expand Down Expand Up @@ -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
Expand All @@ -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))
}
5 changes: 3 additions & 2 deletions src/server/run.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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")?;
Expand Down

1 comment on commit d31e0ff

@vercel
Copy link

@vercel vercel bot commented on d31e0ff Aug 17, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.