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