Skip to main content

hyperlane_plugin/database/
fn.rs

1use super::*;
2
3#[instrument_trace]
4pub fn get_connection_timeout_duration() -> Duration {
5    let timeout_seconds: u64 = std::env::var(ENV_KEY_DB_CONNECTION_TIMEOUT_MILLIS)
6        .ok()
7        .and_then(|value: String| value.parse::<u64>().ok())
8        .unwrap_or(DEFAULT_DB_CONNECTION_TIMEOUT_MILLIS);
9    Duration::from_millis(timeout_seconds)
10}
11
12#[instrument_trace]
13pub fn get_retry_cooldown_duration() -> Duration {
14    let cooldown_millis: u64 = std::env::var(ENV_KEY_DB_RETRY_COOLDOWN_MILLIS)
15        .ok()
16        .and_then(|value: String| value.parse::<u64>().ok())
17        .unwrap_or(DEFAULT_DB_RETRY_COOLDOWN_MILLIS);
18    Duration::from_millis(cooldown_millis)
19}
20
21#[instrument_trace]
22pub async fn initialize_auto_creation() -> Result<(), String> {
23    initialize_auto_creation_with_schema(None, None, None).await
24}
25
26#[instrument_trace]
27pub async fn initialize_auto_creation_with_schema(
28    mysql_schema: Option<DatabaseSchema>,
29    postgresql_schema: Option<DatabaseSchema>,
30    _redis_schema: Option<()>,
31) -> Result<(), String> {
32    if let Err(error) = AutoCreationConfig::validate() {
33        return Err(format!(
34            "Auto-creation configuration validation failed{COLON_SPACE}{error}"
35        ));
36    }
37    let env: &'static EnvConfig = get_global_env_config();
38    let mut initialization_results: Vec<String> = Vec::new();
39    for instance in &env.mysql_instances {
40        match mysql::perform_mysql_auto_creation(instance, mysql_schema.clone()).await {
41            Ok(result) => {
42                initialization_results.push(format!(
43                    "MySQL ({}) {COLON_SPACE}{}",
44                    instance.name,
45                    if result.has_changes() {
46                        "initialized with changes"
47                    } else {
48                        "verified"
49                    }
50                ));
51            }
52            Err(error) => {
53                if !error.should_continue() {
54                    return Err(format!(
55                        "MySQL ({}) auto-creation failed{COLON_SPACE}{error}",
56                        instance.name
57                    ));
58                }
59                initialization_results.push(format!(
60                    "MySQL ({}) : failed but continuing ({error})",
61                    instance.name
62                ));
63            }
64        }
65    }
66    for instance in &env.postgresql_instances {
67        match postgresql::perform_postgresql_auto_creation(instance, postgresql_schema.clone())
68            .await
69        {
70            Ok(result) => {
71                initialization_results.push(format!(
72                    "PostgreSQL ({}) {COLON_SPACE}{}",
73                    instance.name,
74                    if result.has_changes() {
75                        "initialized with changes"
76                    } else {
77                        "verified"
78                    }
79                ));
80            }
81            Err(error) => {
82                if !error.should_continue() {
83                    return Err(format!(
84                        "PostgreSQL ({}) auto-creation failed{COLON_SPACE}{error}",
85                        instance.name
86                    ));
87                }
88                initialization_results.push(format!(
89                    "PostgreSQL ({}) : failed but continuing ({error})",
90                    instance.name
91                ));
92            }
93        }
94    }
95    for instance in &env.redis_instances {
96        match redis::perform_redis_auto_creation(instance).await {
97            Ok(result) => {
98                initialization_results.push(format!(
99                    "Redis ({}) {COLON_SPACE}{}",
100                    instance.name,
101                    if result.has_changes() {
102                        "initialized with changes"
103                    } else {
104                        "verified"
105                    }
106                ));
107            }
108            Err(error) => {
109                if !error.should_continue() {
110                    return Err(format!(
111                        "Redis ({}) auto-creation failed{COLON_SPACE}{error}",
112                        instance.name
113                    ));
114                }
115                initialization_results.push(format!(
116                    "Redis ({}) : failed but continuing ({error})",
117                    instance.name
118                ));
119            }
120        }
121    }
122    if initialization_results.is_empty() {
123        info!("[AUTO-CREATION] No plugins enabled for auto-creation");
124    } else {
125        let results_summary: String = initialization_results.join(", ");
126        info!("[AUTO-CREATION] Initialization complete{COLON_SPACE}{results_summary}");
127    }
128    Ok(())
129}