From: Dessalines Date: Tue, 20 Jun 2023 09:33:03 +0000 (-0400) Subject: Move connection creation into scheduler. (#3120) X-Git-Url: http://these/git/readmes/%7Bthis.imageSrc%7D?a=commitdiff_plain;h=981c1115671f93d905676dbfc4d223a35c9d9e0f;p=lemmy.git Move connection creation into scheduler. (#3120) * Move connection creation into scheduler. - #3076 * Fix clippy. --- 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();