summaryrefslogtreecommitdiffstats
path: root/src/db/models/job.rs
blob: 90d9732e37e25b841bc6049626d3c3fa6c678150 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
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(Identifiable, Queryable, Associations)]
#[belongs_to(Submit)]
#[belongs_to(Endpoint)]
#[belongs_to(Package)]
#[belongs_to(Image)]
#[table_name="jobs"]
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,
    pub uuid: ::uuid::Uuid,
}

#[derive(Debug, 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,
    pub uuid: &'a ::uuid::Uuid,
}

impl Job {
    pub fn create(database_connection: &PgConnection,
                           job_uuid: &::uuid::Uuid,
                           submit: &Submit,
                           endpoint: &Endpoint,
                           package: &Package,
                           image: &Image,
                           container: &ContainerHash,
                           script: &Script,
                           log: &str,
                           ) -> Result<Job> {
        let new_job = NewJob {
            uuid: job_uuid,
            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,
        };

        trace!("Creating Job in database: {:?}", new_job);
        diesel::insert_into(jobs::table)
            .values(&new_job)
            .on_conflict_do_nothing()
            .execute(database_connection)?;

        dsl::jobs
            .filter(uuid.eq(job_uuid))
            .first::<Job>(database_connection)
            .map_err(Error::from)
    }
}