From 981c1115671f93d905676dbfc4d223a35c9d9e0f Mon Sep 17 00:00:00 2001 From: Dessalines Date: Tue, 20 Jun 2023 05:33:03 -0400 Subject: [PATCH] Move connection creation into scheduler. (#3120) * Move connection creation into scheduler. - #3076 * Fix clippy. --- src/scheduled_tasks.rs | 38 +++++++++++++++++++++++--------------- 1 file changed, 23 insertions(+), 15 deletions(-) diff --git a/src/scheduled_tasks.rs b/src/scheduled_tasks.rs index 0f75fdba..aae78b6f 100644 --- a/src/scheduled_tasks.rs +++ b/src/scheduled_tasks.rs @@ -31,35 +31,34 @@ pub fn setup(db_url: String, user_agent: String) -> Result<(), LemmyError> { // Setup the connections let mut scheduler = Scheduler::new(); - let mut conn_1 = PgConnection::establish(&db_url).expect("could not establish connection"); - let mut conn_2 = PgConnection::establish(&db_url).expect("could not establish connection"); - let mut conn_3 = PgConnection::establish(&db_url).expect("could not establish connection"); - let mut conn_4 = PgConnection::establish(&db_url).expect("could not establish connection"); - - // Run on startup - active_counts(&mut conn_1); - update_hot_ranks(&mut conn_1, false); - update_banned_when_expired(&mut conn_1); - clear_old_activities(&mut conn_1); + startup_jobs(&db_url); // Update active counts every hour + let url = db_url.clone(); scheduler.every(CTimeUnits::hour(1)).run(move || { - active_counts(&mut conn_1); - update_banned_when_expired(&mut conn_1); + let mut conn = PgConnection::establish(&url).expect("could not establish connection"); + active_counts(&mut conn); + update_banned_when_expired(&mut conn); }); // Update hot ranks every 5 minutes + let url = db_url.clone(); scheduler.every(CTimeUnits::minutes(5)).run(move || { - update_hot_ranks(&mut conn_2, true); + let mut conn = PgConnection::establish(&url).expect("could not establish connection"); + update_hot_ranks(&mut conn, true); }); // Clear old activities every week + let url = db_url.clone(); scheduler.every(CTimeUnits::weeks(1)).run(move || { - clear_old_activities(&mut conn_3); + let mut conn = PgConnection::establish(&url).expect("could not establish connection"); + clear_old_activities(&mut conn); }); + // Update the Instance Software scheduler.every(CTimeUnits::days(1)).run(move || { - update_instance_software(&mut conn_4, &user_agent); + let mut conn = PgConnection::establish(&db_url).expect("could not establish connection"); + update_instance_software(&mut conn, &user_agent); }); // Manually run the scheduler in an event loop @@ -69,6 +68,15 @@ pub fn setup(db_url: String, user_agent: String) -> Result<(), LemmyError> { } } +/// Run these on server startup +fn startup_jobs(db_url: &str) { + let mut conn = PgConnection::establish(db_url).expect("could not establish connection"); + active_counts(&mut conn); + update_hot_ranks(&mut conn, false); + update_banned_when_expired(&mut conn); + clear_old_activities(&mut conn); +} + /// Update the hot_rank columns for the aggregates tables fn update_hot_ranks(conn: &mut PgConnection, last_week_only: bool) { let mut post_update = diesel::update(post_aggregates::table).into_boxed(); -- 2.44.1