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
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
use std::collections::HashMap;
use crate::controllers::Controllers;
use crate::Module;
use std::io;
use std::process::ExitCode;
use std::sync::Arc;
use std::time::Duration;
use tokio_util::sync::CancellationToken;
use serde::Deserialize;
use thiserror::Error;
use shiny_common::clock::SystemClock;
use shiny_common::error::MultilineDisplayAdapter;
use shiny_configuration::{Configuration, GetConfigurationError};
use shiny_configuration::configuration_builder::BuildConfigurationError;
use shiny_configuration::configuration_provider::env_provider::EnvironmentVariablesProvider;
use shiny_configuration::configuration_provider::yaml_provider::{CreateYamlProviderFromFileError, YamlProvider};
use shiny_jobs::job_trigger::cron_trigger::{CronTrigger, NewCronTriggerError};
use shiny_jobs::job_trigger::interval_trigger::{IntervalTriggerFromStrError, IntervalTrigger};
use shiny_jobs::job_trigger::JobTrigger;
use shiny_jobs::jobs_executor::JobsExecutor;
use crate::context_factory::DefaultContextFactory;

pub struct Application {
    controller: Controllers,
    cancellation_signal_factory: Arc<dyn CancellationSignalFactory>,
    jobs_configuration_factory: Arc<dyn ConfigurationFactory>,
    dynamic_configuration_factory: Arc<dyn ConfigurationFactory>,
}

#[async_trait::async_trait]
pub trait CancellationSignalFactory {
    async fn cancellation_signal(&self) -> io::Result<()>;
}

struct CtrlCCancellationSignalFactory;

#[async_trait::async_trait]
impl CancellationSignalFactory for CtrlCCancellationSignalFactory {
    async fn cancellation_signal(&self) -> io::Result<()> {
        tokio::signal::ctrl_c().await
    }
}

impl Application {
    pub fn new<TClients, TModule>(clients: TClients, module: TModule) -> Self
        where
            TModule: Module<Clients=TClients>,
    {
        Application {
            controller: module.create(clients),
            cancellation_signal_factory: Arc::new(CtrlCCancellationSignalFactory),
            jobs_configuration_factory: Arc::new(DefaultJobsConfigurationFactory),
            dynamic_configuration_factory: Arc::new(DefaultDynamicConfigurationFactory),
        }
    }

    pub async fn run(self) -> ExitCode {
        match self.run_internal().await {
            Ok(()) => ExitCode::SUCCESS,
            Err(error) => {
                tracing::error!("Application failed: {}", error);

                // Better error message
                eprintln!("\n{}", MultilineDisplayAdapter(error));
                ExitCode::FAILURE
            }
        }
    }

    async fn run_internal(self) -> Result<(), RunApplicationError> {
        tracing::info!("Starting application");

        let configuration = self.jobs_configuration_factory.configuration()?;
        let jobs_configuration: HashMap<String, JobConfiguration> = configuration.get("jobs").map_err(RunApplicationError::FailedToGetJobsConfiguration)?;

        let configuration = self.dynamic_configuration_factory.configuration()?;

        let context_factory = DefaultContextFactory::new(configuration);
        let mut job_executor = JobsExecutor::new(Arc::new(context_factory));

        let clock = Arc::new(SystemClock);
        for (name, job) in self.controller.job.take_jobs() {
            if let Some(configuration) = jobs_configuration.get(&name) {
                if configuration.enabled.unwrap_or(true) {
                    let trigger: Arc<dyn JobTrigger> = if configuration.schedule.starts_with("PT") {
                        let trigger = IntervalTrigger::from_str(&configuration.schedule)?;
                        tracing::info!("Registered job `{name}` with interval `{} seconds`", trigger.interval().as_secs());
                        Arc::new(trigger)
                    } else {
                        let trigger = CronTrigger::new(&configuration.schedule, clock.clone())?;
                        tracing::info!("Registered job `{name}` with schedule `{}`", configuration.schedule);
                        Arc::new(trigger)
                    };

                    job_executor.schedule(name, job, trigger);
                }
            }
        }

        let cancellation_signal = self.cancellation_signal_factory.cancellation_signal();
        let cancellation_token = CancellationToken::new();

        tokio::select! {
            _ = job_executor.run(cancellation_token.child_token()) => {
                return Err(RunApplicationError::JobExecutorStoppedUnexpectedly)
            }
            result = cancellation_signal => {
                match result {
                    Ok(_) => {
                        tracing::info!("Cancellation signal received, starting graceful shutdown");
                        cancellation_token.cancel();
                    },
                    Err(err) => { return Err(RunApplicationError::FailedToRegisterCtrlCHandler(err)) }
                }
            }
        }

        // Cancellation signal received
        let graceful_period = tokio::time::sleep(Duration::from_secs(10));
        tokio::select! {
            _ = job_executor.run(cancellation_token.child_token()) => {
                tracing::info!("Application successfully stopped");
                Ok(())
            }
            _ = graceful_period => {
                return Err(RunApplicationError::JobExecutorFailedToStopDuringGracefulShutdownPeriod)
            }
        }
    }

    pub fn set_cancellation_signal_factory(&mut self, cancellation_signal_factory: Arc<dyn CancellationSignalFactory>) {
        self.cancellation_signal_factory = cancellation_signal_factory;
    }

    pub fn set_infrastructure_configuration_factory(&mut self, configuration_factory: Arc<dyn ConfigurationFactory>) {
        self.jobs_configuration_factory = configuration_factory;
    }


    pub fn set_dynamic_configuration_factory(&mut self, dynamic_configuration_factory: Arc<dyn ConfigurationFactory>) {
        self.dynamic_configuration_factory = dynamic_configuration_factory;
    }
}

#[derive(Debug, Error)]
enum RunApplicationError {
    #[error("Failed to register control-c handler")]
    FailedToRegisterCtrlCHandler(#[source] io::Error),
    #[error("Failed to create configuration")]
    FailedToCreateConfiguration(#[from] CreateInfrastructureConfigurationError),
    #[error("Failed to get jobs configuration")]
    FailedToGetJobsConfiguration(#[from] GetConfigurationError),
    #[error("Failed to create interval job trigger")]
    FailedToCreateIntervalJobTrigger(#[from] IntervalTriggerFromStrError),
    #[error("Failed to create cron job trigger")]
    FailedToCreateCronJobTrigger(#[from] NewCronTriggerError),
    #[error("Job executor failed to stop during graceful shutdown")]
    JobExecutorFailedToStopDuringGracefulShutdownPeriod,
    #[error("Job executor stopped unexpectedly")]
    JobExecutorStoppedUnexpectedly,
}

pub trait ConfigurationFactory {
    fn configuration(&self) -> Result<Configuration, CreateInfrastructureConfigurationError>;
}

struct DefaultJobsConfigurationFactory;

impl ConfigurationFactory for DefaultJobsConfigurationFactory {
    fn configuration(&self) -> Result<Configuration, CreateInfrastructureConfigurationError> {
        let root_provider = YamlProvider::from_path("./configuration/production/infrastructure.yaml")?;
        let env_provider = EnvironmentVariablesProvider::new();

        Ok(
            Configuration::builder(root_provider)
                .with_provider("environment", env_provider)
                .build()?
        )
    }
}


struct DefaultDynamicConfigurationFactory;

impl ConfigurationFactory for DefaultDynamicConfigurationFactory {
    fn configuration(&self) -> Result<Configuration, CreateInfrastructureConfigurationError> {
        let root_provider = YamlProvider::from_path("./configuration/production/dynamic.yaml")?;
        let env_provider = EnvironmentVariablesProvider::new();

        Ok(
            Configuration::builder(root_provider)
                .with_provider("environment", env_provider)
                .build()?
        )
    }
}

#[derive(Debug, Error)]
pub enum CreateInfrastructureConfigurationError {
    #[error("Failed to create yaml provider")]
    FailedToCreateYamlProvider(#[from] CreateYamlProviderFromFileError),
    #[error("Failed to create configuration")]
    FailedToCreateConfiguration(#[from] BuildConfigurationError),
}

#[derive(Deserialize)]
struct JobConfiguration {
    schedule: String,
    enabled: Option<bool>,
}