Move connection creation into scheduler. (#3120)

* Move connection creation into scheduler.

- #3076

* Fix clippy.
This commit is contained in:
Dessalines 2023-06-20 05:33:03 -04:00 committed by GitHub
parent 56de68c77a
commit 981c111567
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -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();