replace job->message in queue
This commit is contained in:
+40
-32
@@ -1,16 +1,13 @@
|
||||
use std::{thread, time};
|
||||
|
||||
use super::{Runner, RunnerStatus, Storer};
|
||||
use crate::error::{RunnerError, StorerError};
|
||||
use crate::message::Message;
|
||||
use crate::error::{MessageError, RunnerError, StorerError};
|
||||
use crate::message::{Message, Subject};
|
||||
|
||||
#[derive(Debug)]
|
||||
#[allow(dead_code)]
|
||||
#[derive(Debug, Clone, Copy)]
|
||||
pub enum JobAction {
|
||||
MegaportDeploy,
|
||||
MegaportUndeploy,
|
||||
MegaportCheckDeploy,
|
||||
MegaportCheckUndeploy,
|
||||
Deploy,
|
||||
Check,
|
||||
}
|
||||
|
||||
#[derive(Debug)]
|
||||
@@ -29,42 +26,37 @@ impl Job {
|
||||
}
|
||||
}
|
||||
|
||||
fn deploy_megaport(&mut self) -> Result<Message, RunnerError> {
|
||||
fn format_job(&self) -> String {
|
||||
format!("[job({} - {:?} - {:?})]", self.id, self.action, self.status)
|
||||
}
|
||||
|
||||
fn deploy(&mut self) -> Result<Message, RunnerError> {
|
||||
self.set_status(RunnerStatus::Running);
|
||||
println!(
|
||||
"[job({} - {:?} - {:?})] deploying megaport...",
|
||||
self.id, self.action, self.status
|
||||
);
|
||||
println!("{} {}", self.format_job(), "deploying...");
|
||||
|
||||
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
|
||||
);
|
||||
println!("{} {}", self.format_job(), "deploy done");
|
||||
|
||||
Ok(Message::check_deploy(self.id.try_into().unwrap(), ""))
|
||||
Ok(Message::new(
|
||||
self.id.try_into().unwrap(),
|
||||
Subject::new_action(JobAction::Check),
|
||||
))
|
||||
}
|
||||
|
||||
fn check_megaport(&mut self) -> Result<Message, RunnerError> {
|
||||
fn check(&mut self) -> Result<Message, RunnerError> {
|
||||
self.set_status(RunnerStatus::Running);
|
||||
println!(
|
||||
"[job({} - {:?} - {:?})] checking megaport...",
|
||||
self.id, self.action, self.status
|
||||
);
|
||||
println!("{} {}", self.format_job(), "checking...");
|
||||
|
||||
let wait = time::Duration::from_millis(50);
|
||||
thread::sleep(wait);
|
||||
|
||||
self.set_status(RunnerStatus::Success);
|
||||
println!(
|
||||
"[job({} - {:?} - {:?})] checking megaport done",
|
||||
self.id, self.action, self.status
|
||||
);
|
||||
println!("{} {}", self.format_job(), "checking done");
|
||||
|
||||
Ok(Message::check_deploy(self.id.try_into().unwrap(), ""))
|
||||
Ok(Message::empty())
|
||||
}
|
||||
|
||||
fn set_status(&mut self, status: RunnerStatus) {
|
||||
@@ -75,10 +67,26 @@ impl Job {
|
||||
impl Runner for Job {
|
||||
fn run(&mut self) -> Result<Message, RunnerError> {
|
||||
match self.action {
|
||||
JobAction::MegaportDeploy => self.deploy_megaport(),
|
||||
JobAction::MegaportUndeploy => todo!(),
|
||||
JobAction::MegaportCheckDeploy => self.check_megaport(),
|
||||
JobAction::MegaportCheckUndeploy => todo!(),
|
||||
JobAction::Deploy => self.deploy(),
|
||||
JobAction::Check => self.check(),
|
||||
}
|
||||
}
|
||||
|
||||
fn try_from(message: Message) -> Result<Self, MessageError> {
|
||||
let id: i32 = match message.get_body_id() {
|
||||
Some(id) => match id.try_into() {
|
||||
Ok(id) => id,
|
||||
Err(_) => return Err(MessageError::IDParsingError),
|
||||
},
|
||||
None => return Err(MessageError::IDNeededError),
|
||||
};
|
||||
|
||||
match message.get_subject() {
|
||||
Subject::Action(action) => match action {
|
||||
JobAction::Deploy => Ok(Job::new(id, JobAction::Deploy)),
|
||||
JobAction::Check => Ok(Job::new(id, JobAction::Check)),
|
||||
},
|
||||
_ => Err(MessageError::ParseError),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
+7
-2
@@ -1,4 +1,6 @@
|
||||
use crate::error::{RunnerError, StorerError};
|
||||
use std::marker::Send;
|
||||
|
||||
use crate::error::{MessageError, RunnerError, StorerError};
|
||||
use crate::message::Message;
|
||||
|
||||
#[derive(Debug)]
|
||||
@@ -10,8 +12,11 @@ pub enum RunnerStatus {
|
||||
Success,
|
||||
}
|
||||
|
||||
pub trait Runner {
|
||||
pub trait Runner: Send + 'static {
|
||||
fn run(&mut self) -> Result<Message, RunnerError>;
|
||||
fn try_from(message: Message) -> Result<Self, MessageError>
|
||||
where
|
||||
Self: Sized;
|
||||
}
|
||||
|
||||
pub trait Storer {
|
||||
|
||||
Reference in New Issue
Block a user