aboutsummaryrefslogtreecommitdiff
path: root/src/main.rs
diff options
context:
space:
mode:
Diffstat (limited to 'src/main.rs')
-rw-r--r--src/main.rs50
1 files changed, 44 insertions, 6 deletions
diff --git a/src/main.rs b/src/main.rs
index 50975c66..d5985bac 100644
--- a/src/main.rs
+++ b/src/main.rs
@@ -16,6 +16,7 @@ extern crate diesel;
#[macro_use]
extern crate diesel_migrations;
+use job_scheduler::{JobScheduler, Job};
use std::{
fs::create_dir_all,
panic,
@@ -23,6 +24,7 @@ use std::{
process::{exit, Command},
str::FromStr,
thread,
+ time::Duration,
};
#[macro_use]
@@ -56,7 +58,9 @@ fn main() {
create_icon_cache_folder();
- launch_rocket(extra_debug);
+ let pool = create_db_pool();
+ schedule_jobs(pool.clone());
+ launch_rocket(pool, extra_debug); // Blocks until program termination.
}
const HELP: &str = "\
@@ -301,17 +305,17 @@ fn check_web_vault() {
}
}
-fn launch_rocket(extra_debug: bool) {
- let pool = match util::retry_db(db::DbPool::from_config, CONFIG.db_connection_retries()) {
+fn create_db_pool() -> db::DbPool {
+ match util::retry_db(db::DbPool::from_config, CONFIG.db_connection_retries()) {
Ok(p) => p,
Err(e) => {
error!("Error creating database pool: {:?}", e);
exit(1);
}
- };
-
- api::start_send_deletion_scheduler(pool.clone());
+ }
+}
+fn launch_rocket(pool: db::DbPool, extra_debug: bool) {
let basepath = &CONFIG.domain_path();
// If adding more paths here, consider also adding them to
@@ -334,3 +338,37 @@ fn launch_rocket(extra_debug: bool) {
// The launch will restore the original logging level
error!("Launch error {:#?}", result);
}
+
+fn schedule_jobs(pool: db::DbPool) {
+ if CONFIG.job_poll_interval_ms() == 0 {
+ info!("Job scheduler disabled.");
+ return;
+ }
+ thread::Builder::new().name("job-scheduler".to_string()).spawn(move || {
+ let mut sched = JobScheduler::new();
+
+ // Purge sends that are past their deletion date.
+ if !CONFIG.send_purge_schedule().is_empty() {
+ sched.add(Job::new(CONFIG.send_purge_schedule().parse().unwrap(), || {
+ api::purge_sends(pool.clone());
+ }));
+ }
+
+ // Purge trashed items that are old enough to be auto-deleted.
+ if !CONFIG.trash_purge_schedule().is_empty() {
+ sched.add(Job::new(CONFIG.trash_purge_schedule().parse().unwrap(), || {
+ api::purge_trashed_ciphers(pool.clone());
+ }));
+ }
+
+ // Periodically check for jobs to run. We probably won't need any
+ // jobs that run more often than once a minute, so a default poll
+ // interval of 30 seconds should be sufficient. Users who want to
+ // schedule jobs to run more frequently for some reason can reduce
+ // the poll interval accordingly.
+ loop {
+ sched.tick();
+ thread::sleep(Duration::from_millis(CONFIG.job_poll_interval_ms()));
+ }
+ }).expect("Error spawning job scheduler thread");
+}