clean code
This commit is contained in:
parent
8a0fd00fb9
commit
b8e1afc679
@ -28,46 +28,32 @@ impl Job {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn set_status(&mut self, status: RunnerStatus) {
|
fn deploy_megaport(&mut self) {
|
||||||
self.status = status;
|
self.set_status(RunnerStatus::Running);
|
||||||
|
println!(
|
||||||
|
"[job({} - {:?} - {:?})] deploying megaport...",
|
||||||
|
self.id, self.action, self.status
|
||||||
|
);
|
||||||
|
|
||||||
|
let wait = time::Duration::from_millis(5);
|
||||||
|
thread::sleep(wait);
|
||||||
|
|
||||||
|
self.set_status(RunnerStatus::Success);
|
||||||
|
println!(
|
||||||
|
"[job({} - {:?} - {:?})] megaport deployed",
|
||||||
|
self.id, self.action, self.status
|
||||||
|
);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Runner for Job {
|
impl Runner for Job {
|
||||||
fn run(&mut self) {
|
fn run(&mut self) {
|
||||||
self.set_status(RunnerStatus::Running);
|
|
||||||
|
|
||||||
match self.action {
|
match self.action {
|
||||||
JobAction::MegaportDeploy => {
|
JobAction::MegaportDeploy => self.deploy_megaport(),
|
||||||
println!(
|
JobAction::MegaportUndeploy => todo!(),
|
||||||
"[job({} - {:?} - {:?})] deploying megaport...",
|
JobAction::MegaportCheckDeploy => todo!(),
|
||||||
self.id, self.action, self.status
|
JobAction::MegaportCheckUndeploy => todo!(),
|
||||||
);
|
|
||||||
}
|
}
|
||||||
JobAction::MegaportUndeploy => {
|
|
||||||
println!(
|
|
||||||
"[job({} - {:?} - {:?})] undeploying megaport...",
|
|
||||||
self.id, self.action, self.status
|
|
||||||
);
|
|
||||||
}
|
|
||||||
JobAction::MegaportCheckDeploy => {
|
|
||||||
println!(
|
|
||||||
"[job({} - {:?} - {:?})] checking megaport deployement...",
|
|
||||||
self.id, self.action, self.status
|
|
||||||
);
|
|
||||||
}
|
|
||||||
JobAction::MegaportCheckUndeploy => {
|
|
||||||
println!(
|
|
||||||
"[job({} - {:?} - {:?})] checking megaport undeployement...",
|
|
||||||
self.id, self.action, self.status
|
|
||||||
);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
let wait = time::Duration::from_millis(1);
|
|
||||||
thread::sleep(wait);
|
|
||||||
|
|
||||||
self.set_status(RunnerStatus::Success);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
fn set_status(&mut self, status: RunnerStatus) {
|
fn set_status(&mut self, status: RunnerStatus) {
|
||||||
|
|||||||
@ -18,7 +18,7 @@ impl<T> Queue<T> {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Copy, Clone, PartialEq)]
|
#[derive(Debug, Copy, Clone, PartialEq)]
|
||||||
#[allow(dead_code)]
|
#[allow(dead_code)]
|
||||||
pub enum WorkerStatus {
|
pub enum WorkerStatus {
|
||||||
Pending,
|
Pending,
|
||||||
@ -27,12 +27,16 @@ pub enum WorkerStatus {
|
|||||||
}
|
}
|
||||||
|
|
||||||
struct Worker {
|
struct Worker {
|
||||||
|
id: u32,
|
||||||
|
manager: String,
|
||||||
status: WorkerStatus,
|
status: WorkerStatus,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Worker {
|
impl Worker {
|
||||||
fn new() -> Self {
|
fn new(id: u32, manager: String) -> Self {
|
||||||
Self {
|
Self {
|
||||||
|
id: id,
|
||||||
|
manager: manager,
|
||||||
status: WorkerStatus::Pending,
|
status: WorkerStatus::Pending,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -64,12 +68,11 @@ impl<T: Runner + std::marker::Send + 'static> Manager<T> {
|
|||||||
|
|
||||||
pub fn launch_workers(&mut self, nb_workers: u32) {
|
pub fn launch_workers(&mut self, nb_workers: u32) {
|
||||||
for i in 0..nb_workers {
|
for i in 0..nb_workers {
|
||||||
let shared_worker = Arc::new(Mutex::new(Worker::new()));
|
let shared_worker = Arc::new(Mutex::new(Worker::new(i, self.name.clone())));
|
||||||
self.workers.push(shared_worker.clone());
|
self.workers.push(shared_worker.clone());
|
||||||
|
|
||||||
let worker = shared_worker.clone();
|
let worker = shared_worker.clone();
|
||||||
let queue = self.queue.clone();
|
let queue = self.queue.clone();
|
||||||
let name = self.name.clone();
|
|
||||||
|
|
||||||
thread::spawn(move || loop {
|
thread::spawn(move || loop {
|
||||||
let mut guard = queue.content.lock().unwrap();
|
let mut guard = queue.content.lock().unwrap();
|
||||||
@ -84,17 +87,9 @@ impl<T: Runner + std::marker::Send + 'static> Manager<T> {
|
|||||||
|
|
||||||
drop(guard);
|
drop(guard);
|
||||||
|
|
||||||
let mut guard = worker.lock().unwrap();
|
Manager::<T>::set_worker_status(&worker, WorkerStatus::Running);
|
||||||
guard.set_status(WorkerStatus::Running);
|
|
||||||
drop(guard);
|
|
||||||
|
|
||||||
println!("[worker({} - {})] launching job...", name, i);
|
|
||||||
runner.run();
|
runner.run();
|
||||||
println!("[worker({} - {})] job done", name, i);
|
Manager::<T>::set_worker_status(&worker, WorkerStatus::Pending);
|
||||||
|
|
||||||
let mut guard = worker.lock().unwrap();
|
|
||||||
guard.set_status(WorkerStatus::Pending);
|
|
||||||
drop(guard);
|
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -114,4 +109,14 @@ impl<T: Runner + std::marker::Send + 'static> Manager<T> {
|
|||||||
}
|
}
|
||||||
true
|
true
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fn set_worker_status(worker: &Arc<Mutex<Worker>>, status: WorkerStatus) {
|
||||||
|
let mut guard = worker.lock().unwrap();
|
||||||
|
guard.set_status(status);
|
||||||
|
|
||||||
|
println!(
|
||||||
|
"[worker({} - {})] status: {:?}",
|
||||||
|
guard.manager, guard.id, guard.status
|
||||||
|
);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user