Don't panic when scheduled tasks can't connect to database (#3634)

* Don't panic when scheduled tasks can't connect to database

* Use map & map_err instead of match

* Empty commit for Woodpecker re-run
pull/3666/head
Sander Saarend 2023-07-19 16:00:44 +03:00 committed by GitHub
parent 1f21bdb2f9
commit 77a8e3b897
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 86 additions and 69 deletions

View File

@ -49,30 +49,54 @@ pub fn setup(
// Update active counts every hour // Update active counts every hour
let url = db_url.clone(); let url = db_url.clone();
scheduler.every(CTimeUnits::hour(1)).run(move || { scheduler.every(CTimeUnits::hour(1)).run(move || {
let mut conn = PgConnection::establish(&url).expect("could not establish connection"); PgConnection::establish(&url)
.map(|mut conn| {
active_counts(&mut conn); active_counts(&mut conn);
update_banned_when_expired(&mut conn); update_banned_when_expired(&mut conn);
})
.map_err(|e| {
error!("Failed to establish db connection for active counts update: {e}");
})
.ok();
}); });
// Update hot ranks every 15 minutes // Update hot ranks every 15 minutes
let url = db_url.clone(); let url = db_url.clone();
scheduler.every(CTimeUnits::minutes(15)).run(move || { scheduler.every(CTimeUnits::minutes(15)).run(move || {
let mut conn = PgConnection::establish(&url).expect("could not establish connection"); PgConnection::establish(&url)
.map(|mut conn| {
update_hot_ranks(&mut conn); update_hot_ranks(&mut conn);
})
.map_err(|e| {
error!("Failed to establish db connection for hot ranks update: {e}");
})
.ok();
}); });
// Delete any captcha answers older than ten minutes, every ten minutes // Delete any captcha answers older than ten minutes, every ten minutes
let url = db_url.clone(); let url = db_url.clone();
scheduler.every(CTimeUnits::minutes(10)).run(move || { scheduler.every(CTimeUnits::minutes(10)).run(move || {
let mut conn = PgConnection::establish(&url).expect("could not establish connection"); PgConnection::establish(&url)
.map(|mut conn| {
delete_expired_captcha_answers(&mut conn); delete_expired_captcha_answers(&mut conn);
})
.map_err(|e| {
error!("Failed to establish db connection for captcha cleanup: {e}");
})
.ok();
}); });
// Clear old activities every week // Clear old activities every week
let url = db_url.clone(); let url = db_url.clone();
scheduler.every(CTimeUnits::weeks(1)).run(move || { scheduler.every(CTimeUnits::weeks(1)).run(move || {
let mut conn = PgConnection::establish(&url).expect("could not establish connection"); PgConnection::establish(&url)
.map(|mut conn| {
clear_old_activities(&mut conn); clear_old_activities(&mut conn);
})
.map_err(|e| {
error!("Failed to establish db connection for activity cleanup: {e}");
})
.ok();
}); });
// Remove old rate limit buckets after 1 to 2 hours of inactivity // Remove old rate limit buckets after 1 to 2 hours of inactivity
@ -84,16 +108,28 @@ pub fn setup(
// Overwrite deleted & removed posts and comments every day // Overwrite deleted & removed posts and comments every day
let url = db_url.clone(); let url = db_url.clone();
scheduler.every(CTimeUnits::days(1)).run(move || { scheduler.every(CTimeUnits::days(1)).run(move || {
let mut conn = PgConnection::establish(&url).expect("could not establish connection"); PgConnection::establish(&db_url)
.map(|mut conn| {
overwrite_deleted_posts_and_comments(&mut conn); overwrite_deleted_posts_and_comments(&mut conn);
})
.map_err(|e| {
error!("Failed to establish db connection for deleted content cleanup: {e}");
})
.ok();
}); });
// Update the Instance Software // Update the Instance Software
scheduler.every(CTimeUnits::days(1)).run(move || { scheduler.every(CTimeUnits::days(1)).run(move || {
let mut conn = PgConnection::establish(&db_url).expect("could not establish connection"); PgConnection::establish(&url)
.map(|mut conn| {
update_instance_software(&mut conn, &user_agent) update_instance_software(&mut conn, &user_agent)
.map_err(|e| warn!("Failed to update instance software: {e}")) .map_err(|e| warn!("Failed to update instance software: {e}"))
.ok(); .ok();
})
.map_err(|e| {
error!("Failed to establish db connection for instance software update: {e}");
})
.ok();
}); });
// Manually run the scheduler in an event loop // Manually run the scheduler in an event loop
@ -203,18 +239,15 @@ fn process_hot_ranks_in_batches(
} }
fn delete_expired_captcha_answers(conn: &mut PgConnection) { fn delete_expired_captcha_answers(conn: &mut PgConnection) {
match diesel::delete( diesel::delete(
captcha_answer::table.filter(captcha_answer::published.lt(now - IntervalDsl::minutes(10))), captcha_answer::table.filter(captcha_answer::published.lt(now - IntervalDsl::minutes(10))),
) )
.execute(conn) .execute(conn)
{ .map(|_| {
Ok(_) => {
info!("Done."); info!("Done.");
} })
Err(e) => { .map_err(|e| error!("Failed to clear old captcha answers: {e}"))
error!("Failed to clear old captcha answers: {}", e) .ok();
}
}
} }
/// Clear old activities (this table gets very large) /// Clear old activities (this table gets very large)
@ -222,21 +255,22 @@ fn clear_old_activities(conn: &mut PgConnection) {
info!("Clearing old activities..."); info!("Clearing old activities...");
diesel::delete(sent_activity::table.filter(sent_activity::published.lt(now - 3.months()))) diesel::delete(sent_activity::table.filter(sent_activity::published.lt(now - 3.months())))
.execute(conn) .execute(conn)
.map_err(|e| error!("Failed to clear old sent activities: {}", e)) .map_err(|e| error!("Failed to clear old sent activities: {e}"))
.ok(); .ok();
diesel::delete( diesel::delete(
received_activity::table.filter(received_activity::published.lt(now - 3.months())), received_activity::table.filter(received_activity::published.lt(now - 3.months())),
) )
.execute(conn) .execute(conn)
.map_err(|e| error!("Failed to clear old received activities: {}", e)) .map(|_| info!("Done."))
.map_err(|e| error!("Failed to clear old received activities: {e}"))
.ok(); .ok();
} }
/// overwrite posts and comments 30d after deletion /// overwrite posts and comments 30d after deletion
fn overwrite_deleted_posts_and_comments(conn: &mut PgConnection) { fn overwrite_deleted_posts_and_comments(conn: &mut PgConnection) {
info!("Overwriting deleted posts..."); info!("Overwriting deleted posts...");
match diesel::update( diesel::update(
post::table post::table
.filter(post::deleted.eq(true)) .filter(post::deleted.eq(true))
.filter(post::updated.lt(now.nullable() - 1.months())) .filter(post::updated.lt(now.nullable() - 1.months()))
@ -247,17 +281,14 @@ fn overwrite_deleted_posts_and_comments(conn: &mut PgConnection) {
post::name.eq(DELETED_REPLACEMENT_TEXT), post::name.eq(DELETED_REPLACEMENT_TEXT),
)) ))
.execute(conn) .execute(conn)
{ .map(|_| {
Ok(_) => {
info!("Done."); info!("Done.");
} })
Err(e) => { .map_err(|e| error!("Failed to overwrite deleted posts: {e}"))
error!("Failed to overwrite deleted posts: {}", e) .ok();
}
}
info!("Overwriting deleted comments..."); info!("Overwriting deleted comments...");
match diesel::update( diesel::update(
comment::table comment::table
.filter(comment::deleted.eq(true)) .filter(comment::deleted.eq(true))
.filter(comment::updated.lt(now.nullable() - 1.months())) .filter(comment::updated.lt(now.nullable() - 1.months()))
@ -265,14 +296,11 @@ fn overwrite_deleted_posts_and_comments(conn: &mut PgConnection) {
) )
.set(comment::content.eq(DELETED_REPLACEMENT_TEXT)) .set(comment::content.eq(DELETED_REPLACEMENT_TEXT))
.execute(conn) .execute(conn)
{ .map(|_| {
Ok(_) => {
info!("Done."); info!("Done.");
} })
Err(e) => { .map_err(|e| error!("Failed to overwrite deleted comments: {e}"))
error!("Failed to overwrite deleted comments: {}", e) .ok();
}
}
} }
/// Re-calculate the site and community active counts every 12 hours /// Re-calculate the site and community active counts every 12 hours
@ -291,20 +319,16 @@ fn active_counts(conn: &mut PgConnection) {
"update site_aggregates set users_active_{} = (select * from site_aggregates_activity('{}')) where site_id = 1", "update site_aggregates set users_active_{} = (select * from site_aggregates_activity('{}')) where site_id = 1",
i.1, i.0 i.1, i.0
); );
match sql_query(update_site_stmt).execute(conn) { sql_query(update_site_stmt)
Ok(_) => {} .execute(conn)
Err(e) => { .map_err(|e| error!("Failed to update site stats: {e}"))
error!("Failed to update site stats: {}", e) .ok();
}
}
let update_community_stmt = format!("update community_aggregates ca set users_active_{} = mv.count_ from community_aggregates_activity('{}') mv where ca.community_id = mv.community_id_", i.1, i.0); let update_community_stmt = format!("update community_aggregates ca set users_active_{} = mv.count_ from community_aggregates_activity('{}') mv where ca.community_id = mv.community_id_", i.1, i.0);
match sql_query(update_community_stmt).execute(conn) { sql_query(update_community_stmt)
Ok(_) => {} .execute(conn)
Err(e) => { .map_err(|e| error!("Failed to update community stats: {e}"))
error!("Failed to update community stats: {}", e) .ok();
}
}
} }
info!("Done."); info!("Done.");
@ -314,27 +338,20 @@ fn active_counts(conn: &mut PgConnection) {
fn update_banned_when_expired(conn: &mut PgConnection) { fn update_banned_when_expired(conn: &mut PgConnection) {
info!("Updating banned column if it expires ..."); info!("Updating banned column if it expires ...");
match diesel::update( diesel::update(
person::table person::table
.filter(person::banned.eq(true)) .filter(person::banned.eq(true))
.filter(person::ban_expires.lt(now)), .filter(person::ban_expires.lt(now)),
) )
.set(person::banned.eq(false)) .set(person::banned.eq(false))
.execute(conn) .execute(conn)
{ .map_err(|e| error!("Failed to update person.banned when expires: {e}"))
Ok(_) => {} .ok();
Err(e) => {
error!("Failed to update person.banned when expires: {}", e) diesel::delete(community_person_ban::table.filter(community_person_ban::expires.lt(now)))
}
}
match diesel::delete(community_person_ban::table.filter(community_person_ban::expires.lt(now)))
.execute(conn) .execute(conn)
{ .map_err(|e| error!("Failed to remove community_ban expired rows: {e}"))
Ok(_) => {} .ok();
Err(e) => {
error!("Failed to remove community_ban expired rows: {}", e)
}
}
} }
/// Updates the instance software and version /// Updates the instance software and version