Skip to content

Commit 81047a3

Browse files
authored
Add plugin hooks for init and scheduled tasks (fixes #6488, fixes #6489) (#6493)
* Add plugin hooks for init and scheduled tasks (fixes #6488, fixes #6489) * fix test, add init * clippy * 1 min hook
1 parent 108d78c commit 81047a3

3 files changed

Lines changed: 20 additions & 2 deletions

File tree

crates/api/api_utils/src/plugins.rs

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -187,7 +187,7 @@ pub fn plugin_metadata() -> Vec<PluginMetadata> {
187187
}
188188

189189
#[derive(Clone)]
190-
struct LemmyPlugins {
190+
pub struct LemmyPlugins {
191191
plugins: Vec<LemmyPlugin>,
192192
captcha_plugin: Option<LemmyPlugin>,
193193
}
@@ -240,6 +240,11 @@ impl LemmyPlugin {
240240
.insert("lemmy_version".to_string(), VERSION.to_string());
241241
let builder = move || PluginBuilder::new(manifest.clone()).with_wasi(true).build();
242242
let pool = Pool::new(builder);
243+
if let Some(mut p) = pool.get(GET_PLUGIN_TIMEOUT)?
244+
&& p.function_exists("init")
245+
{
246+
p.call::<_, ()>("init", ())?;
247+
}
243248
Ok(LemmyPlugin {
244249
pool,
245250
filename: filename.unwrap_or(settings.file),
@@ -263,7 +268,7 @@ impl LemmyPlugin {
263268

264269
impl LemmyPlugins {
265270
/// Load and initialize all plugins
266-
fn get_or_init() -> Self {
271+
pub fn get_or_init() -> Self {
267272
static PLUGINS: LazyLock<LemmyPlugins> = LazyLock::new(|| {
268273
let mut plugins: Vec<_> = SETTINGS
269274
.plugins

crates/routes/src/utils/scheduled_tasks.rs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ use diesel_async::{AsyncPgConnection, RunQueryDsl};
1818
use diesel_uplete::uplete;
1919
use lemmy_api_utils::{
2020
context::LemmyContext,
21+
plugins::plugin_hook_after,
2122
send_activity::{ActivityChannel, SendActivityData},
2223
utils::send_webmention,
2324
};
@@ -64,6 +65,11 @@ pub async fn setup(context: Data<LemmyContext>) -> LemmyResult<()> {
6465
// https://github.com/mdsherry/clokwerk/issues/38
6566
let mut scheduler = AsyncScheduler::with_tz(Utc);
6667

68+
// Every 1 minute run plugin hooks
69+
scheduler.every(CTimeUnits::minutes(1)).run(async move || {
70+
plugin_hook_after("scheduled_task_1_min", &());
71+
});
72+
6773
let context_1 = context.clone();
6874
// Every 10 minutes update hot ranks, delete expired captchas and publish scheduled posts
6975
scheduler.every(CTimeUnits::minutes(10)).run(move || {
@@ -78,6 +84,7 @@ pub async fn setup(context: Data<LemmyContext>) -> LemmyResult<()> {
7884
.await
7985
.inspect_err(|e| warn!("Failed to publish scheduled posts: {e}"))
8086
.ok();
87+
plugin_hook_after("scheduled_task_10_mins", &());
8188
}
8289
});
8390

@@ -102,6 +109,7 @@ pub async fn setup(context: Data<LemmyContext>) -> LemmyResult<()> {
102109
.await
103110
.inspect_err(|e| warn!("Failed to delete expired instance bans: {e}"))
104111
.ok();
112+
plugin_hook_after("scheduled_task_1_hour", &());
105113
}
106114
});
107115

@@ -141,6 +149,7 @@ pub async fn setup(context: Data<LemmyContext>) -> LemmyResult<()> {
141149
.await
142150
.inspect_err(|e| warn!("Failed to clear old activities: {e}"))
143151
.ok();
152+
plugin_hook_after("scheduled_task_daily", &());
144153
}
145154
});
146155

crates/server/src/lib.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ use clap::{Parser, Subcommand};
1111
use lemmy_api::sitemap::get_sitemap;
1212
use lemmy_api_utils::{
1313
context::LemmyContext,
14+
plugins::LemmyPlugins,
1415
request::client_builder,
1516
send_activity::ActivityChannel,
1617
utils::local_site_rate_limit_to_rate_limit_config,
@@ -175,6 +176,9 @@ pub async fn start_lemmy_server(args: CmdArgs) -> LemmyResult<()> {
175176
startup_server_handle = Some(create_startup_server()?);
176177
}
177178

179+
// Initialize plugins in background
180+
tokio::task::spawn_blocking(LemmyPlugins::get_or_init);
181+
178182
// Set up the connection pool
179183
let pool = build_db_pool()?;
180184

0 commit comments

Comments
 (0)