You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
145 lines
3.6 KiB
145 lines
3.6 KiB
use super::{JobMeta, JobState, JobType}; |
|
#[cfg(feature = "server")] |
|
use crate::models::schema::*; |
|
use crate::{ |
|
config::get_self_id, |
|
conv::{bytes_to_string_truncated, systime_to_string}, |
|
types::Id, |
|
}; |
|
#[cfg(feature = "server")] |
|
use diesel::{Identifiable, Insertable, Queryable}; |
|
use serde::{Deserialize, Serialize}; |
|
use std::{borrow::Cow, fmt::Debug, time::SystemTime}; |
|
|
|
#[derive(Serialize, Deserialize, Clone, PartialEq)] |
|
#[cfg_attr( |
|
feature = "server", |
|
derive(Queryable, Identifiable, Insertable, AsChangeset), |
|
diesel(table_name = results) |
|
)] |
|
pub struct AssignedJob { |
|
pub agent_id: Id, |
|
pub alias: Option<String>, |
|
pub created: SystemTime, |
|
pub id: Id, |
|
pub job_id: Id, |
|
pub result: Option<Vec<u8>>, |
|
pub state: JobState, |
|
pub exec_type: JobType, |
|
pub retcode: Option<i32>, |
|
pub updated: SystemTime, |
|
} |
|
|
|
impl Debug for AssignedJob { |
|
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { |
|
f.debug_struct("AssignedJob") |
|
.field("agent_id", &self.agent_id.to_string()) |
|
.field("alias", &self.alias) |
|
.field("created", &systime_to_string(self.created)) |
|
.field("id", &self.id.to_string()) |
|
.field("job_id", &self.job_id.to_string()) |
|
.field( |
|
"result", |
|
&self |
|
.result |
|
.as_ref() |
|
.map(|r| bytes_to_string_truncated(&r, 256)), |
|
) |
|
.field("state", &self.state) |
|
.field("exec_type", &self.exec_type) |
|
.field("retcode", &self.retcode) |
|
.field("updated", &systime_to_string(self.updated)) |
|
.finish() |
|
} |
|
} |
|
|
|
#[derive(Serialize, Deserialize, Clone, Copy, Debug)] |
|
pub struct AssignedJobById { |
|
pub agent_id: Id, |
|
pub id: Id, |
|
pub job_id: Id, |
|
} |
|
|
|
impl From<(&JobMeta, AssignedJobById)> for AssignedJob { |
|
fn from((job, ids): (&JobMeta, AssignedJobById)) -> Self { |
|
let AssignedJobById { |
|
agent_id, |
|
id, |
|
job_id, |
|
} = ids; |
|
|
|
AssignedJob { |
|
id, |
|
agent_id, |
|
job_id, |
|
alias: job.alias.clone(), |
|
exec_type: job.exec_type, |
|
..Default::default() |
|
} |
|
} |
|
} |
|
|
|
impl From<&AssignedJob> for AssignedJobById { |
|
fn from(j: &AssignedJob) -> Self { |
|
let &AssignedJob { |
|
agent_id, |
|
id, |
|
job_id, |
|
.. |
|
} = j; |
|
|
|
AssignedJobById { |
|
agent_id, |
|
id, |
|
job_id, |
|
} |
|
} |
|
} |
|
|
|
impl Default for AssignedJobById { |
|
fn default() -> Self { |
|
Self { |
|
agent_id: get_self_id(), |
|
id: Id::new_v4(), |
|
job_id: Id::nil(), |
|
} |
|
} |
|
} |
|
|
|
impl Default for AssignedJob { |
|
fn default() -> Self { |
|
Self { |
|
agent_id: Id::nil(), |
|
alias: None, |
|
created: SystemTime::now(), |
|
id: Id::new_v4(), |
|
job_id: Id::nil(), |
|
result: None, |
|
state: JobState::Queued, |
|
retcode: None, |
|
updated: SystemTime::now(), |
|
exec_type: JobType::default(), |
|
} |
|
} |
|
} |
|
|
|
impl AssignedJob { |
|
pub fn to_raw_result(&self) -> &[u8] { |
|
match self.result.as_ref() { |
|
Some(r) => r, |
|
None => b"No data", |
|
} |
|
} |
|
|
|
pub fn to_str_result(&self) -> Cow<str> { |
|
String::from_utf8_lossy(self.to_raw_result()) |
|
} |
|
|
|
pub fn set_result<S: Serialize>(&mut self, result: &S) { |
|
self.result = Some(serde_json::to_vec(result).unwrap()); |
|
} |
|
|
|
pub fn touch(&mut self) { |
|
self.updated = SystemTime::now() |
|
} |
|
}
|
|
|