mirror of
https://github.com/LemmyNet/lemmy
synced 2024-11-10 15:04:14 +00:00
Move connection creation into scheduler. (#3120)
* Move connection creation into scheduler. - #3076 * Fix clippy.
This commit is contained in:
parent
56de68c77a
commit
981c111567
1 changed files with 23 additions and 15 deletions
|
@ -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();
|
||||
|
|
Loading…
Reference in a new issue