1 // Scheduler, and trait for .seconds(), .minutes(), etc.
2 use clokwerk::{Scheduler, TimeUnits};
3 // Import week days and WeekDay
4 use diesel::{sql_query, PgConnection, RunQueryDsl};
5 use lemmy_db_schema::{source::activity::Activity, utils::DbPool};
6 use lemmy_utils::error::LemmyError;
7 use std::{thread, time::Duration};
10 /// Schedules various cleanup tasks for lemmy in a background thread
11 pub fn setup(pool: DbPool) -> Result<(), LemmyError> {
12 let mut scheduler = Scheduler::new();
14 let conn = pool.get()?;
16 update_banned_when_expired(&conn);
18 // On startup, reindex the tables non-concurrently
19 // TODO remove this for now, since it slows down startup a lot on lemmy.ml
20 reindex_aggregates_tables(&conn, true);
21 scheduler.every(1.hour()).run(move || {
23 update_banned_when_expired(&conn);
24 reindex_aggregates_tables(&conn, true);
25 drop_ccnew_indexes(&conn);
28 let conn = pool.get()?;
29 clear_old_activities(&conn);
30 scheduler.every(1.weeks()).run(move || {
31 clear_old_activities(&conn);
34 // Manually run the scheduler in an event loop
36 scheduler.run_pending();
37 thread::sleep(Duration::from_millis(1000));
41 /// Reindex the aggregates tables every one hour
42 /// This is necessary because hot_rank is actually a mutable function:
43 /// https://dba.stackexchange.com/questions/284052/how-to-create-an-index-based-on-a-time-based-function-in-postgres?noredirect=1#comment555727_284052
44 fn reindex_aggregates_tables(conn: &PgConnection, concurrently: bool) {
48 "community_aggregates",
50 reindex_table(conn, table_name, concurrently);
54 fn reindex_table(conn: &PgConnection, table_name: &str, concurrently: bool) {
55 let concurrently_str = if concurrently { "concurrently" } else { "" };
56 info!("Reindexing table {} {} ...", concurrently_str, table_name);
57 let query = format!("reindex table {} {}", concurrently_str, table_name);
58 sql_query(query).execute(conn).expect("reindex table");
62 /// Clear old activities (this table gets very large)
63 fn clear_old_activities(conn: &PgConnection) {
64 info!("Clearing old activities...");
65 Activity::delete_olds(conn).expect("clear old activities");
69 /// Re-calculate the site and community active counts every 12 hours
70 fn active_counts(conn: &PgConnection) {
71 info!("Updating active site and community aggregates ...");
77 ("6 months", "half_year"),
81 let update_site_stmt = format!(
82 "update site_aggregates set users_active_{} = (select * from site_aggregates_activity('{}'))",
85 sql_query(update_site_stmt)
87 .expect("update site stats");
89 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);
90 sql_query(update_community_stmt)
92 .expect("update community stats");
98 /// Set banned to false after ban expires
99 fn update_banned_when_expired(conn: &PgConnection) {
100 info!("Updating banned column if it expires ...");
101 let update_ban_expires_stmt =
102 "update person set banned = false where banned = true and ban_expires < now()";
103 sql_query(update_ban_expires_stmt)
105 .expect("update banned when expires");
108 /// Drops the phantom CCNEW indexes created by postgres
109 /// https://github.com/LemmyNet/lemmy/issues/2431
110 fn drop_ccnew_indexes(conn: &PgConnection) {
111 info!("Dropping phantom ccnew indexes...");
112 let drop_stmt = "select drop_ccnew_indexes()";
115 .expect("drop ccnew indexes");