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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
pub type ContextLock = Arc<Mutex<Context>>;
use crate::config::Configuration;
use crate::config::JobParameters;
use scheduler_service_lib::{RunContext, SchedulerContext, ServerStopper, State};
use serde::{Deserialize, Serialize};
use std::net::SocketAddr;
use std::path::Path;
use std::sync::Arc;
use std::sync::Mutex;

pub type ContextState = State<JobParameters, (), ()>;

pub struct Context {
    inner: SchedulerContext,
    state: ContextState,
}

impl RunContext<JobParameters, ()> for Context {
    fn run_requested(&self) -> Option<(Uuid, JobParameters)> {
        self.state.run_requested()
    }

    fn new_run_started(&mut self) -> Result<(), scheduler_service_lib::Error> {
        self.state.new_run_started()
    }

    fn run_finished(
        &mut self,
        output_info: Option<()>,
    ) -> Result<(), scheduler_service_lib::Error> {
        self.state.run_finished(output_info)
    }
}

impl Context {
    pub fn new(config: Configuration) -> Self {
        Self {
            inner: SchedulerContext::new(None, config.inner),
            state: ContextState::Idle,
        }
    }

    pub fn set_server_stopper(&mut self, server_stopper: ServerStopper) {
        self.inner.set_server_stopper(Some(server_stopper));
    }

    pub fn server_stopper(&self) -> &Option<ServerStopper> {
        self.inner.server_stopper()
    }

    pub fn state(&self) -> &ContextState {
        &self.state
    }

    pub fn state_mut(&mut self) -> &mut ContextState {
        &mut self.state
    }

    pub fn address(&self) -> &SocketAddr {
        &self.inner.config().address
    }

    pub fn api_token(&self) -> Option<String> {
        self.inner.config().api_token.clone()
    }

    pub fn set_api_token(&mut self, api_token: String) {
        self.inner.set_api_token(Some(api_token));
    }

    pub fn working_directory(&self) -> &Path {
        &self.inner.config().result_dir
    }
}

impl Context {
    pub fn into_scheduler_context(&self) -> SchedulerContext {
        self.inner.clone()
    }
}

use thiserror::Error;
use uuid::Uuid;

#[derive(Debug, Error, Deserialize, Serialize)]
pub enum Error {
    #[error("job is in progress.")]
    SnaphotInProgress,
    #[error("job hasn't been started")]
    SnaphotNotStarted,
    #[error("no request to start")]
    NoRequestToStart,
    #[error("job was not found")]
    JobNotFound,
    #[error("no job was run yet")]
    NoJobRun,
}