]> 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 5c406ff6d2913017285dae8ea25a878ab1f2112c..df54868fb5f8ce1e96a97a45b8621a32cf9a5935 100644 (file)
@@ -2,27 +2,30 @@
 use clokwerk::{Scheduler, TimeUnits};
 // Import week days and WeekDay
 use diesel::{sql_query, PgConnection, RunQueryDsl};
-use lemmy_db_queries::{source::activity::Activity_, DbPool};
-use lemmy_db_schema::source::activity::Activity;
-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) {
+pub fn setup(pool: DbPool) -> Result<(), LemmyError> {
   let mut scheduler = Scheduler::new();
 
-  let conn = pool.get().unwrap();
+  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().unwrap();
+  let conn = pool.get()?;
   clear_old_activities(&conn);
   scheduler.every(1.weeks()).run(move || {
     clear_old_activities(&conn);
@@ -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");
+}