From 10c886111e304a53be24b29194f2127d91121929 Mon Sep 17 00:00:00 2001 From: Alex Auvolat Date: Tue, 21 Jun 2022 17:18:16 +0200 Subject: Rename things, garage worker list cmd --- src/util/background/mod.rs | 25 +++++++++++++++++++++++-- 1 file changed, 23 insertions(+), 2 deletions(-) (limited to 'src/util/background/mod.rs') diff --git a/src/util/background/mod.rs b/src/util/background/mod.rs index c06e2225..92090a1a 100644 --- a/src/util/background/mod.rs +++ b/src/util/background/mod.rs @@ -4,9 +4,12 @@ pub mod job_worker; pub mod worker; use core::future::Future; + +use std::collections::HashMap; use std::pin::Pin; use std::sync::Arc; +use serde::{Deserialize, Serialize}; use tokio::sync::{mpsc, watch, Mutex}; use crate::error::Error; @@ -20,6 +23,14 @@ pub(crate) type Job = Pin + Send>>; pub struct BackgroundRunner { send_job: mpsc::UnboundedSender<(Job, bool)>, send_worker: mpsc::UnboundedSender>, + worker_info: Arc>>, +} + +#[derive(Clone, Serialize, Deserialize, Debug)] +pub struct WorkerInfo { + pub name: String, + pub info: Option, + pub status: WorkerStatus, } impl BackgroundRunner { @@ -30,8 +41,13 @@ impl BackgroundRunner { ) -> (Arc, tokio::task::JoinHandle<()>) { let (send_worker, worker_out) = mpsc::unbounded_channel::>(); - let await_all_done = - tokio::spawn(async move { WorkerProcessor::new(worker_out, stop_signal).run().await }); + let worker_info = Arc::new(std::sync::Mutex::new(HashMap::new())); + let mut worker_processor = + WorkerProcessor::new(worker_out, stop_signal, worker_info.clone()); + + let await_all_done = tokio::spawn(async move { + worker_processor.run().await; + }); let (send_job, queue_out) = mpsc::unbounded_channel(); let queue_out = Arc::new(Mutex::new(queue_out)); @@ -52,10 +68,15 @@ impl BackgroundRunner { let bgrunner = Arc::new(Self { send_job, send_worker, + worker_info, }); (bgrunner, await_all_done) } + pub fn get_worker_info(&self) -> HashMap { + self.worker_info.lock().unwrap().clone() + } + /// Spawn a task to be run in background pub fn spawn(&self, job: T) where -- cgit v1.2.3