use clokwerk::{Scheduler, TimeUnits};
// Import week days and WeekDay
use diesel::{sql_query, PgConnection, RunQueryDsl};
-use lemmy_db_schema::{source::activity::Activity, DbPool};
-use lemmy_utils::LemmyError;
-use log::info;
+use lemmy_db_schema::{source::activity::Activity, utils::DbPool};
+use lemmy_utils::error::LemmyError;
use std::{thread, time::Duration};
+use tracing::info;
/// Schedules various cleanup tasks for lemmy in a background thread
pub fn setup(pool: DbPool) -> Result<(), LemmyError> {
let conn = pool.get()?;
active_counts(&conn);
+ update_banned_when_expired(&conn);
// On startup, reindex the tables non-concurrently
// TODO remove this for now, since it slows down startup a lot on lemmy.ml
reindex_aggregates_tables(&conn, true);
scheduler.every(1.hour()).run(move || {
active_counts(&conn);
+ update_banned_when_expired(&conn);
reindex_aggregates_tables(&conn, true);
+ drop_ccnew_indexes(&conn);
});
let conn = pool.get()?;
info!("Done.");
}
+
+/// Set banned to false after ban expires
+fn update_banned_when_expired(conn: &PgConnection) {
+ info!("Updating banned column if it expires ...");
+ let update_ban_expires_stmt =
+ "update person set banned = false where banned = true and ban_expires < now()";
+ sql_query(update_ban_expires_stmt)
+ .execute(conn)
+ .expect("update banned when expires");
+}
+
+/// Drops the phantom CCNEW indexes created by postgres
+/// https://github.com/LemmyNet/lemmy/issues/2431
+fn drop_ccnew_indexes(conn: &PgConnection) {
+ info!("Dropping phantom ccnew indexes...");
+ let drop_stmt = "select drop_ccnew_indexes()";
+ sql_query(drop_stmt)
+ .execute(conn)
+ .expect("drop ccnew indexes");
+}