update Runner.run signature + adapt message enum
This commit is contained in:
parent
a50b6c82ba
commit
35161d1c61
@ -1,5 +1,6 @@
|
||||
mod database;
|
||||
mod error;
|
||||
mod message;
|
||||
mod model;
|
||||
mod worker;
|
||||
|
||||
|
||||
@ -1,9 +1,9 @@
|
||||
#[derive(Debug, Clone)]
|
||||
pub enum Message {
|
||||
Command(MessageBody),
|
||||
QueueStop,
|
||||
CheckDeploy(MessageBody),
|
||||
}
|
||||
|
||||
#[allow(dead_code)]
|
||||
#[derive(Debug, Clone)]
|
||||
pub struct MessageBody {
|
||||
id: u32,
|
||||
@ -11,11 +11,11 @@ pub struct MessageBody {
|
||||
}
|
||||
|
||||
impl Message {
|
||||
pub fn command(id: u32, content: &str) -> Message {
|
||||
pub fn check_deploy(id: u32, content: &str) -> Self {
|
||||
let body = MessageBody {
|
||||
id: id,
|
||||
content: content.to_string(),
|
||||
};
|
||||
Message::Command(body)
|
||||
Message::CheckDeploy(body)
|
||||
}
|
||||
}
|
||||
|
||||
@ -2,6 +2,7 @@ use std::{thread, time};
|
||||
|
||||
use super::{Runner, RunnerStatus, Storer};
|
||||
use crate::error::StorerError;
|
||||
use crate::message::Message;
|
||||
|
||||
#[derive(Debug)]
|
||||
#[allow(dead_code)]
|
||||
@ -28,7 +29,7 @@ impl Job {
|
||||
}
|
||||
}
|
||||
|
||||
fn deploy_megaport(&mut self) {
|
||||
fn deploy_megaport(&mut self) -> Result<Message, String> {
|
||||
self.set_status(RunnerStatus::Running);
|
||||
println!(
|
||||
"[job({} - {:?} - {:?})] deploying megaport...",
|
||||
@ -43,6 +44,8 @@ impl Job {
|
||||
"[job({} - {:?} - {:?})] megaport deployed",
|
||||
self.id, self.action, self.status
|
||||
);
|
||||
|
||||
Ok(Message::check_deploy(self.id.try_into().unwrap(), ""))
|
||||
}
|
||||
|
||||
fn set_status(&mut self, status: RunnerStatus) {
|
||||
@ -51,7 +54,7 @@ impl Job {
|
||||
}
|
||||
|
||||
impl Runner for Job {
|
||||
fn run(&mut self) {
|
||||
fn run(&mut self) -> Result<Message, String> {
|
||||
match self.action {
|
||||
JobAction::MegaportDeploy => self.deploy_megaport(),
|
||||
JobAction::MegaportUndeploy => todo!(),
|
||||
|
||||
@ -1,4 +1,5 @@
|
||||
use crate::error::StorerError;
|
||||
use crate::message::Message;
|
||||
|
||||
#[derive(Debug)]
|
||||
#[allow(dead_code)]
|
||||
@ -10,7 +11,7 @@ pub enum RunnerStatus {
|
||||
}
|
||||
|
||||
pub trait Runner {
|
||||
fn run(&mut self);
|
||||
fn run(&mut self) -> Result<Message, String>;
|
||||
}
|
||||
|
||||
pub trait Storer {
|
||||
|
||||
@ -2,6 +2,7 @@ use std::collections::VecDeque;
|
||||
use std::sync::{Arc, Condvar, Mutex};
|
||||
use std::thread;
|
||||
|
||||
use crate::message::Message;
|
||||
use crate::model::*;
|
||||
|
||||
pub struct Queue<T> {
|
||||
@ -88,7 +89,10 @@ impl<T: Runner + std::marker::Send + 'static> Manager<T> {
|
||||
drop(guard);
|
||||
|
||||
Manager::<T>::set_worker_status(&worker, WorkerStatus::Running);
|
||||
runner.run();
|
||||
match runner.run() {
|
||||
Ok(m) => Manager::<T>::manage_message(m),
|
||||
Err(_e) => todo!(),
|
||||
}
|
||||
Manager::<T>::set_worker_status(&worker, WorkerStatus::Pending);
|
||||
});
|
||||
}
|
||||
@ -110,6 +114,14 @@ impl<T: Runner + std::marker::Send + 'static> Manager<T> {
|
||||
true
|
||||
}
|
||||
|
||||
fn manage_message(message: Message) {
|
||||
match message {
|
||||
Message::CheckDeploy(body) => {
|
||||
println!("{:?}", body)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fn set_worker_status(worker: &Arc<Mutex<Worker>>, status: WorkerStatus) {
|
||||
let mut guard = worker.lock().unwrap();
|
||||
guard.set_status(status);
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user