]> Untitled Git - lemmy.git/blobdiff - src/scheduled_tasks.rs
Adding job to drop phantom ccnew indexes. Fixes #2431 (#2432)
[lemmy.git] / src / scheduled_tasks.rs
index c3514d63ed5fee9f11fa355d753dad648af2379d..df54868fb5f8ce1e96a97a45b8621a32cf9a5935 100644 (file)
@@ -2,10 +2,10 @@
 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> {
@@ -13,13 +13,16 @@ 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()?;
@@ -91,3 +94,23 @@ fn active_counts(conn: &PgConnection) {
 
   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");
+}