onechatsocial_database/tasks/
mod.rs

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
//! Semi-important background task management

use crate::Database;

use async_std::task;
use std::time::Instant;

const WORKER_COUNT: usize = 5;

pub mod ack;
pub mod last_message_id;
pub mod process_embeds;
pub mod web_push;

/// Spawn background workers
pub async fn start_workers(db: Database, authifier_db: authifier::Database) {
    for _ in 0..WORKER_COUNT {
        task::spawn(ack::worker(db.clone()));
        task::spawn(last_message_id::worker(db.clone()));
        task::spawn(process_embeds::worker(db.clone()));
        task::spawn(web_push::worker(authifier_db.clone()));
    }
}

/// Task with additional information on when it should run
pub struct DelayedTask<T> {
    pub data: T,
    last_updated: Instant,
    first_seen: Instant,
}

/// Commit to database every 30 seconds if the task is particularly active.
static EXPIRE_CONSTANT: u64 = 30;

/// Otherwise, commit to database after 5 seconds.
static SAVE_CONSTANT: u64 = 5;

impl<T> DelayedTask<T> {
    /// Create a new delayed task
    pub fn new(data: T) -> Self {
        DelayedTask {
            data,
            last_updated: Instant::now(),
            first_seen: Instant::now(),
        }
    }

    /// Push a task further back in time
    pub fn delay(&mut self) {
        self.last_updated = Instant::now()
    }

    /// Check if a task should run yet
    pub fn should_run(&self) -> bool {
        self.first_seen.elapsed().as_secs() > EXPIRE_CONSTANT
            || self.last_updated.elapsed().as_secs() > SAVE_CONSTANT
    }
}