summaryrefslogtreecommitdiff
path: root/src/importer.rs
diff options
context:
space:
mode:
authorKjetil Orbekk <kjetil.orbekk@gmail.com>2020-02-01 09:10:54 -0500
committerKjetil Orbekk <kjetil.orbekk@gmail.com>2020-02-01 09:10:54 -0500
commitfb9f143f2353dc8c64a18be84c12b53cdad847e7 (patch)
tree5d7d2e5f80aa5aff705e6b9e05753a3005bb3bcf /src/importer.rs
parent0773347daf9dd5b1433884aeabd007f2f605adeb (diff)
Add strava importer threadpool
Diffstat (limited to 'src/importer.rs')
-rw-r--r--src/importer.rs48
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
+}