]> Untitled Git - lemmy.git/blob - src/scheduled_tasks.rs
Fixing clippy
[lemmy.git] / src / scheduled_tasks.rs
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, DbPool};
6 use lemmy_utils::LemmyError;
7 use log::info;
8 use std::{thread, time::Duration};
9
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();
13
14   let conn = pool.get()?;
15   active_counts(&conn);
16
17   // On startup, reindex the tables non-concurrently
18   // TODO remove this for now, since it slows down startup a lot on lemmy.ml
19   reindex_aggregates_tables(&conn, true);
20   scheduler.every(1.hour()).run(move || {
21     active_counts(&conn);
22     reindex_aggregates_tables(&conn, true);
23   });
24
25   let conn = pool.get()?;
26   clear_old_activities(&conn);
27   scheduler.every(1.weeks()).run(move || {
28     clear_old_activities(&conn);
29   });
30
31   // Manually run the scheduler in an event loop
32   loop {
33     scheduler.run_pending();
34     thread::sleep(Duration::from_millis(1000));
35   }
36 }
37
38 /// Reindex the aggregates tables every one hour
39 /// This is necessary because hot_rank is actually a mutable function:
40 /// https://dba.stackexchange.com/questions/284052/how-to-create-an-index-based-on-a-time-based-function-in-postgres?noredirect=1#comment555727_284052
41 fn reindex_aggregates_tables(conn: &PgConnection, concurrently: bool) {
42   for table_name in &[
43     "post_aggregates",
44     "comment_aggregates",
45     "community_aggregates",
46   ] {
47     reindex_table(conn, table_name, concurrently);
48   }
49 }
50
51 fn reindex_table(conn: &PgConnection, table_name: &str, concurrently: bool) {
52   let concurrently_str = if concurrently { "concurrently" } else { "" };
53   info!("Reindexing table {} {} ...", concurrently_str, table_name);
54   let query = format!("reindex table {} {}", concurrently_str, table_name);
55   sql_query(query).execute(conn).expect("reindex table");
56   info!("Done.");
57 }
58
59 /// Clear old activities (this table gets very large)
60 fn clear_old_activities(conn: &PgConnection) {
61   info!("Clearing old activities...");
62   Activity::delete_olds(conn).expect("clear old activities");
63   info!("Done.");
64 }
65
66 /// Re-calculate the site and community active counts every 12 hours
67 fn active_counts(conn: &PgConnection) {
68   info!("Updating active site and community aggregates ...");
69
70   let intervals = vec![
71     ("1 day", "day"),
72     ("1 week", "week"),
73     ("1 month", "month"),
74     ("6 months", "half_year"),
75   ];
76
77   for i in &intervals {
78     let update_site_stmt = format!(
79       "update site_aggregates set users_active_{} = (select * from site_aggregates_activity('{}'))",
80       i.1, i.0
81     );
82     sql_query(update_site_stmt)
83       .execute(conn)
84       .expect("update site stats");
85
86     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);
87     sql_query(update_community_stmt)
88       .execute(conn)
89       .expect("update community stats");
90   }
91
92   info!("Done.");
93 }