summaryrefslogtreecommitdiff
path: root/src/importer.rs
blob: 8513b5416e94e718be95a9918238a27db2fbbb4c (plain)
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
use crate::models;
use std::sync::mpsc::channel;
use std::sync::mpsc::Receiver;
use std::sync::mpsc::Sender;
use std::sync::Arc;
use std::sync::Mutex;
use threadpool::ThreadPool;

pub const WORKERS: usize = 10;

#[derive(Debug, Clone)]
pub enum Command {
    ImportStravaUser(models::User),
    Quit,
}

fn handle_command(pool: ThreadPool,
                  command: Command) {
    info!("handle {:?}", command);
}

fn receive_commands(pool: ThreadPool,
                    rx: Arc<Mutex<Receiver<Command>>>) {
    info!("receive_commands");
    match (move || -> Result<(), Box<dyn std::error::Error>> {
        let rx = rx.lock().expect("channel");
        let mut command = rx.recv()?;
        loop {
            info!("got command: {:?}", command);
            let pool0 = pool.clone();
            pool.execute(|| handle_command(pool0, command));
            command = rx.recv()?;
        }})() {
        Ok(()) => (),
        Err(e) => {
            error!("receive_commands: {:?}", e);
            ()
        }
    }
}

pub fn run(pool: ThreadPool) -> Sender<Command> {
    let (tx, rx0) = channel();
    let rx = Arc::new(Mutex::new(rx0));
    let pool0 = pool.clone();
    pool.execute(move || receive_commands(pool0, rx));
    tx
}