diff options
author | Matthias Beyer <mail@beyermatthias.de> | 2020-11-13 08:53:06 +0100 |
---|---|---|
committer | Matthias Beyer <mail@beyermatthias.de> | 2020-11-13 09:48:56 +0100 |
commit | 2eda1f45ae17669481822c6163a5effa0ec7b2b4 (patch) | |
tree | dda8ef30c485045ad64f80e1bf3d428556459d5b /src/db | |
parent | d63ed21ee075a13513cf9156da02deda43cf814f (diff) |
Add model definition for Job
Signed-off-by: Matthias Beyer <mail@beyermatthias.de>
Diffstat (limited to 'src/db')
-rw-r--r-- | src/db/models/job.rs | 64 | ||||
-rw-r--r-- | src/db/models/mod.rs | 3 |
2 files changed, 67 insertions, 0 deletions
diff --git a/src/db/models/job.rs b/src/db/models/job.rs new file mode 100644 index 0000000..f910ed3 --- /dev/null +++ b/src/db/models/job.rs @@ -0,0 +1,64 @@ +use anyhow::Error; +use anyhow::Result; +use diesel::PgConnection; +use diesel::prelude::*; + +use crate::db::models::{Submit, Endpoint, Package, Image}; +use crate::package::Script; +use crate::schema::jobs::*; +use crate::schema::jobs; +use crate::util::docker::ContainerHash; + +#[derive(Queryable)] +pub struct Job { + pub id: i32, + pub submit_id: i32, + pub endpoint_id: i32, + pub package_id: i32, + pub image_id: i32, + pub container_hash: String, + pub script_text: String, + pub log_text: String, +} + +#[derive(Insertable)] +#[table_name="jobs"] +struct NewJob<'a> { + pub submit_id: i32, + pub endpoint_id: i32, + pub package_id: i32, + pub image_id: i32, + pub container_hash: &'a str, + pub script_text: &'a str, + pub log_text: &'a str, +} + +impl Job { + pub fn create(database_connection: &PgConnection, + submit: &Submit, + endpoint: &Endpoint, + package: &Package, + image: &Image, + container: &ContainerHash, + script: &Script, + log: &str, + ) -> Result<()> { + let new_job = NewJob { + submit_id: submit.id, + endpoint_id: endpoint.id, + package_id: package.id, + image_id: image.id, + container_hash: container.as_ref(), + script_text: script.as_ref(), + log_text: log, + }; + + diesel::insert_into(jobs::table) + .values(&new_job) + .on_conflict_do_nothing() + .execute(database_connection) + .map_err(Error::from) + .map(|_| ()) + } +} + diff --git a/src/db/models/mod.rs b/src/db/models/mod.rs index 745b71f..26875bc 100644 --- a/src/db/models/mod.rs +++ b/src/db/models/mod.rs @@ -10,6 +10,9 @@ pub use envvar::*; mod image; pub use image::*; +mod job; +pub use job::*; + mod githash; pub use githash::*; |