diff options
Diffstat (limited to 'src/importer.rs')
-rw-r--r-- | src/importer.rs | 48 |
1 files changed, 48 insertions, 0 deletions
diff --git a/src/importer.rs b/src/importer.rs new file mode 100644 index 0000000..8513b54 --- /dev/null +++ b/src/importer.rs @@ -0,0 +1,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 +} |