Alex Mikhalev
4 years ago
2 changed files with 48 additions and 50 deletions
@ -0,0 +1,43 @@ |
|||||||
|
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) |
||||||
|
}) |
||||||
|
} |
||||||
|
} |
Loading…
Reference in new issue