omni_orchestrator/
main.rs

1//=============================================================================
2// OmniOrchestrator - A distributed system for managing and orchestrating
3//=============================================================================
4// Maintained by: Tristan J. Poland, Maxine Deandrade, Caznix, Haywood Spartian
5// and the OmniCloud community.
6//=============================================================================
7// This is the entry point for the OmniOrchestrator server application.
8// It manages the entirety of the OmniCloud platform and its components:
9//    - Database
10//    - API
11//    - Cluster Management
12//    - Bootstrapping
13//    - Load Balancing
14//=============================================================================
15
16// +-------------+
17// | MODULES     |
18// +-------------+
19mod cors;
20mod state;
21mod server;
22mod leader;
23mod config;
24mod cluster;
25mod network;
26mod schemas;
27mod logging;
28mod endpoints;
29mod db_manager;
30mod api_models;
31mod initialization;
32
33// +-------------+
34// | IMPORTS     |
35// +-------------+
36// Third-party dependencies
37use std::sync::Arc;
38use anyhow::Result;
39use colored::Colorize;
40use tokio::sync::RwLock;
41use lazy_static::lazy_static;
42
43// Internal imports
44// use crate::server::build_rocket; // removed, now used in initialization::launch_server
45
46// Convenience re-exports
47pub use crate::state::SharedState;
48pub use crate::config::SERVER_CONFIG;
49pub use crate::leader::LeaderElection;
50pub use crate::cluster::ClusterManager;
51pub use crate::db_manager::DatabaseManager;
52
53// We ignore this import as it always says
54// unused even when that is not the case
55#[allow(unused_imports)]
56#[macro_use]
57extern crate rocket;
58
59pub static PROJECT_ROOT: &str = env!("CARGO_MANIFEST_DIR");
60
61// +-------------+
62// | GLOBALS     |
63// +-------------+
64// Global singleton instance of the cluster manager
65// Manages node discovery and peer connections
66lazy_static! {
67    static ref CLUSTER_MANAGER: Arc<RwLock<ClusterManager>> = {
68        let state = format!("{}:{}", SERVER_CONFIG.address, SERVER_CONFIG.port);
69
70        let bind1 = &state;
71        let bind = bind1.clone();
72        let state = SharedState::new(bind.into());
73        let shared_state = Arc::new(RwLock::new(state));
74        Arc::new(RwLock::new(ClusterManager::new(shared_state)))
75    };
76}
77
78// +-------------+
79// | MAIN        |
80// +-------------+
81/// Main entry point for the OmniOrchestrator server
82#[rocket::main]
83async fn main() -> Result<(), Box<dyn std::error::Error>> {
84    // Setup initial convenience variables
85    let port = SERVER_CONFIG.port;
86
87    // ====================== INITIALIZATION ======================
88    logging::print_banner("OMNI ORCHESTRATOR SERVER STARTING", |s| s.bright_cyan());
89    println!("{}", format!("⇒ Starting server on port {}", port).green());
90
91    // ====================== Setup logging ======================
92    logging::print_banner("LOGGING SETUP", |s| s.bright_yellow());
93    initialization::setup_logging().await;
94
95    // ====================== Setup database ======================
96    logging::print_banner("DATABASE SETUP", |s| s.bright_yellow());
97    let db_manager = initialization::setup_database().await?;
98    let pool = db_manager.get_main_pool();
99
100    // ====================== Setup ClickHouse ======================
101    logging::print_banner("CLICKHOUSE SETUP", |s| s.bright_yellow());
102    let clickhouse_client = initialization::setup_clickhouse().await?;
103
104    // ====================== Setup Schema ======================
105    logging::print_banner("SCHEMA SETUP", |s| s.bright_yellow());
106    initialization::setup_schema(&clickhouse_client, &pool).await?;
107
108    // ====================== CLUSTER SETUP ======================
109    logging::print_banner("CLUSTER MANAGEMENT", |s| s.bright_magenta());
110    let (shared_state, node_id) = initialization::setup_cluster_management();
111
112    // Clone shared_state for later use
113    let shared_state_for_leader = shared_state.clone();
114    let shared_state_for_server = shared_state.clone();
115
116    // ====================== Start Peer Discovery ======================
117
118    logging::print_banner("START PEER DISCOVERY", |s| s.bright_magenta());
119    initialization::start_peer_discovery(port);
120
121    // ====================== AUTOSCALER SETUP ======================
122    logging::print_banner("AUTOSCALER SETUP", |s| s.bright_yellow());
123
124    // TODO: Implement autoscaler setup via Lighthouse integration
125    // This will require establishing communication with the Lighthouse service
126    // for automatic scaling decisions based on cluster metrics and load patterns.
127    // Reference: https://github.com/OmniCloudOrg/Lighthouse
128
129    // ====================== LEADER ELECTION ======================
130    logging::print_banner("LEADER ELECTION", |s| s.bright_green());
131
132    initialization::start_leader_election(shared_state_for_leader, node_id);
133
134    // ====================== SERVER STARTUP ======================
135    logging::print_banner("SERVER STARTUP", |s| s.bright_cyan());
136
137    initialization::launch_server(
138        port,
139        db_manager.clone(),
140        pool.clone(),
141        CLUSTER_MANAGER.clone(),
142        clickhouse_client,
143        shared_state_for_server,
144    ).await?;
145
146    Ok(())
147}