Alex Mikhalev
4 years ago
4 changed files with 225 additions and 80 deletions
@ -1,43 +0,0 @@
@@ -1,43 +0,0 @@
|
||||
use super::*; |
||||
use crate::{model::SectionId, section_runner::SectionRunHandle}; |
||||
use eyre::WrapErr; |
||||
use serde::{Deserialize, Serialize}; |
||||
use std::time::Duration; |
||||
|
||||
#[derive(Debug, Deserialize, Serialize)] |
||||
#[serde(rename_all = "camelCase")] |
||||
pub struct RequestData { |
||||
pub section_id: SectionId, |
||||
#[serde(with = "crate::serde::duration")] |
||||
pub duration: Duration, |
||||
} |
||||
|
||||
#[derive(Debug, Deserialize, Serialize)] |
||||
#[serde(rename_all = "camelCase")] |
||||
pub struct ResponseData { |
||||
pub message: String, |
||||
pub run_id: SectionRunHandle, |
||||
} |
||||
|
||||
impl IRequest for RequestData { |
||||
fn exec(&mut self, ctx: &mut RequestContext) -> RequestFuture { |
||||
let mut section_runner = ctx.section_runner.clone(); |
||||
let section = ctx.sections.get(&self.section_id).cloned(); |
||||
let duration = self.duration; |
||||
Box::pin(async move { |
||||
let section = section.ok_or_else(|| { |
||||
RequestError::with_name(ErrorCode::NotFound, "section not found", "section") |
||||
})?; |
||||
let handle = section_runner |
||||
.queue_run(section.clone(), duration) |
||||
.await |
||||
.wrap_err("could not queue run")?; |
||||
let res = ResponseData { |
||||
message: format!("running section '{}' for {:?}", §ion.name, duration), |
||||
run_id: handle, |
||||
}; |
||||
let res_value = serde_json::to_value(res).wrap_err("could not serialize response")?; |
||||
Ok(res_value) |
||||
}) |
||||
} |
||||
} |
@ -0,0 +1,87 @@
@@ -0,0 +1,87 @@
|
||||
use super::*; |
||||
use crate::{model::SectionRef, section_runner::SectionRunHandle}; |
||||
use eyre::WrapErr; |
||||
use serde::{Deserialize, Serialize}; |
||||
use std::time::Duration; |
||||
|
||||
#[derive(Copy, Clone, Debug, Serialize, Deserialize)] |
||||
#[serde(transparent)] |
||||
pub struct SectionId(pub crate::model::SectionId); |
||||
|
||||
impl SectionId { |
||||
fn get_section(self, sections: &Sections) -> Result<SectionRef, RequestError> { |
||||
sections.get(&self.0).cloned().ok_or_else(|| { |
||||
RequestError::with_name(ErrorCode::NotFound, "section not found", "section") |
||||
}) |
||||
} |
||||
} |
||||
|
||||
#[derive(Debug, Deserialize, Serialize)] |
||||
#[serde(rename_all = "camelCase")] |
||||
pub struct RunSectionRequest { |
||||
pub section_id: SectionId, |
||||
#[serde(with = "crate::serde::duration")] |
||||
pub duration: Duration, |
||||
} |
||||
|
||||
#[derive(Debug, Deserialize, Serialize)] |
||||
#[serde(rename_all = "camelCase")] |
||||
pub struct RunSectionResponse { |
||||
pub message: String, |
||||
pub run_id: SectionRunHandle, |
||||
} |
||||
|
||||
impl IRequest for RunSectionRequest { |
||||
type Response = RunSectionResponse; |
||||
fn exec(&mut self, ctx: &mut RequestContext) -> RequestFuture<Self::Response> { |
||||
let mut section_runner = ctx.section_runner.clone(); |
||||
let section = self.section_id.get_section(&ctx.sections); |
||||
let duration = self.duration; |
||||
Box::pin(async move { |
||||
let section = section?; |
||||
let handle = section_runner |
||||
.queue_run(section.clone(), duration) |
||||
.await |
||||
.wrap_err("could not queue run")?; |
||||
Ok(RunSectionResponse { |
||||
message: format!("running section '{}' for {:?}", §ion.name, duration), |
||||
run_id: handle, |
||||
}) |
||||
}) |
||||
} |
||||
} |
||||
|
||||
#[derive(Debug, Deserialize, Serialize)] |
||||
#[serde(rename_all = "camelCase")] |
||||
pub struct CancelSectionRequest { |
||||
pub section_id: SectionId, |
||||
} |
||||
|
||||
#[derive(Debug, Deserialize, Serialize)] |
||||
#[serde(rename_all = "camelCase")] |
||||
pub struct CancelSectionResponse { |
||||
pub message: String, |
||||
pub cancelled: usize, |
||||
} |
||||
|
||||
impl IRequest for CancelSectionRequest { |
||||
type Response = CancelSectionResponse; |
||||
fn exec(&mut self, ctx: &mut RequestContext) -> RequestFuture<Self::Response> { |
||||
let mut section_runner = ctx.section_runner.clone(); |
||||
let section = self.section_id.get_section(&ctx.sections); |
||||
Box::pin(async move { |
||||
let section = section?; |
||||
let cancelled = section_runner |
||||
.cancel_by_section(section.id) |
||||
.await |
||||
.wrap_err("could not cancel section")?; |
||||
Ok(CancelSectionResponse { |
||||
message: format!( |
||||
"cancelled {} runs for section '{}'", |
||||
cancelled, section.name |
||||
), |
||||
cancelled, |
||||
}) |
||||
}) |
||||
} |
||||
} |
Loading…
Reference in new issue