@@ -27,91 +27,102 @@ use crate::{
2727fn InitializeLogging ( ) {
2828 let LogLevel = if cfg ! ( debug_assertions) { "debug" } else { "info" } ;
2929 if std:: env:: var ( "RUST_LOG" ) . is_err ( ) {
30+ // This is unsafe but only runs once at startup.
3031 unsafe { std:: env:: set_var ( "RUST_LOG" , LogLevel ) } ;
3132 }
3233 env_logger:: init ( ) ;
3334}
3435
3536/// The main asynchronous function that sets up and runs the application.
36- #[ tokio:: main]
37- pub async fn main ( ) {
38- InitializeLogging ( ) ;
39- info ! ( "[Main] Starting Mountain application..." ) ;
40-
41- // 1. Create the high-performance scheduler from the `Echo` crate.
42- let NumberOfWorkers = num_cpus:: get ( ) . max ( 2 ) ;
43- let Scheduler = SchedulerBuilder :: Create ( ) . WithWorkerCount ( NumberOfWorkers ) . Build ( ) ;
44-
45- // We need an Arc<> to safely share the scheduler for shutdown handling.
46- let SchedulerForShutdown = Arc :: new ( Scheduler ) ;
47- let SchedulerForRunTime = SchedulerForShutdown . clone ( ) ;
48-
49- let mut Builder = tauri:: Builder :: default ( ) ;
50-
51- Builder
52- . manage ( ApplicationState :: default ( ) )
53- . setup ( move |Application | {
54- info ! ( "[Setup] Tauri setup hook initiated." ) ;
55- let ApplicationHandle = Application . handle ( ) . clone ( ) ;
56-
57- // 2. Create the application Environment and the Echo-powered
58- // ApplicationRunTime.
59- let Environment = Arc :: new ( MountainEnvironment :: Create ( ApplicationHandle . clone ( ) ) ) ;
60- let RunTime = Arc :: new ( ApplicationRunTime :: Create ( SchedulerForRunTime , Environment ) ) ;
61-
62- // 3. Manage the ApplicationRunTime in Tauri's state so it's accessible to all
63- // command handlers.
64- ApplicationHandle . manage ( RunTime ) ;
65- info ! ( "[Setup] Echo scheduler and ApplicationRunTime created and managed." ) ;
66-
67- // 4. Spawn a detached task for all post-setup initializations.
68- // This allows the UI to load faster while the backend finishes starting up.
69- let PostSetupApplicationHandle = ApplicationHandle . clone ( ) ;
70- tauri:: async_runtime:: spawn ( async move {
71- info ! ( "[SetupTask] Starting post-setup initializations..." ) ;
72- let ApplicationState = PostSetupApplicationHandle . state :: < ApplicationState > ( ) ;
73-
74- // TODO: Re-integrate handler logic for these initializations.
75- // Handler::Configuration::InitializeConfiguration(&PostSetupApplicationHandle,
76- // &ApplicationState).await;
77- // Handler::ExtensionManagement::InitializeScanPaths(&
78- // PostSetupApplicationHandle, &ApplicationState).await; ApplicationState.
79- // ScanExtensions(&PostSetupApplicationHandle).await;
80-
81- Vine :: Server :: Initialize :: Initialize ( PostSetupApplicationHandle . clone ( ) , "[::1]:50051" . to_string ( ) ) ;
82- InitializeCocoon ( & PostSetupApplicationHandle ) . await ;
83-
84- info ! ( "[SetupTask] Post-setup initializations complete." ) ;
85- } ) ;
86-
87- Ok ( ( ) )
88- } )
89- . plugin ( tauri_plugin_dialog:: init ( ) )
90- . invoke_handler ( tauri:: generate_handler![
91- // TODO: Re-integrate all Tauri command handlers here.
92- ] )
93- . build ( tauri:: generate_context!( ) )
94- . expect ( "FATAL: Error while building Mountain Tauri application" )
95- . run ( move |ApplicationHandle , Event | {
96- if let RunEvent :: ExitRequested { api, .. } = Event {
97- info ! ( "[RunEvent] Exit requested. Initiating graceful shutdown..." ) ;
98- api. prevent_exit ( ) ;
99- let SchedulerHandle = SchedulerForShutdown . clone ( ) ;
100-
101- // Spawn a new async task to handle the shutdown to avoid blocking the
102- // Tauri event loop.
103- tokio:: spawn ( async move {
104- info ! ( "[Shutdown] Shutting down Echo scheduler..." ) ;
105- if let Ok ( mut Scheduler ) = Arc :: try_unwrap ( SchedulerHandle ) {
106- Scheduler . Stop ( ) . await ;
107- } else {
108- error ! ( "[Shutdown] Could not get exclusive access to scheduler for shutdown." ) ;
37+ pub fn Fn ( ) {
38+ tokio:: runtime:: Builder :: new_multi_thread ( )
39+ . enable_all ( )
40+ . build ( )
41+ . expect ( "Cannot build." )
42+ . block_on ( async {
43+ InitializeLogging ( ) ;
44+ info ! ( "[Main] Starting Mountain application..." ) ;
45+
46+ // 1. Create the high-performance scheduler from the `Echo` crate.
47+ let NumberOfWorkers = num_cpus:: get ( ) . max ( 2 ) ;
48+ let Scheduler = SchedulerBuilder :: Create ( ) . WithWorkerCount ( NumberOfWorkers ) . Build ( ) ;
49+
50+ // We need an Arc<> to safely share the scheduler for shutdown handling.
51+ let SchedulerForShutdown = Arc :: new ( Scheduler ) ;
52+ let SchedulerForRunTime = SchedulerForShutdown . clone ( ) ;
53+
54+ let mut Builder = tauri:: Builder :: default ( ) ;
55+
56+ Builder
57+ . manage ( ApplicationState :: default ( ) )
58+ . setup ( move |Application | {
59+ info ! ( "[Setup] Tauri setup hook initiated." ) ;
60+ let ApplicationHandle = Application . handle ( ) . clone ( ) ;
61+
62+ // 2. Create the application Environment and the Echo-powered
63+ // ApplicationRunTime.
64+ let Environment = Arc :: new ( MountainEnvironment :: Create ( ApplicationHandle . clone ( ) ) ) ;
65+ let RunTime = Arc :: new ( ApplicationRunTime :: Create ( SchedulerForRunTime , Environment ) ) ;
66+
67+ // 3. Manage the ApplicationRunTime in Tauri's state so it's accessible to all
68+ // command handlers.
69+ ApplicationHandle . manage ( RunTime ) ;
70+ info ! ( "[Setup] Echo scheduler and ApplicationRunTime created and managed." ) ;
71+
72+ // 4. Spawn a detached task for all post-setup initializations.
73+ // This allows the UI to load faster while the backend finishes starting up.
74+ let PostSetupApplicationHandle = ApplicationHandle . clone ( ) ;
75+ tauri:: async_runtime:: spawn ( async move {
76+ info ! ( "[SetupTask] Starting post-setup initializations..." ) ;
77+ let _ApplicationState = PostSetupApplicationHandle . state :: < ApplicationState > ( ) ;
78+
79+ // TODO: Re-integrate handler logic for these initializations.
80+ // Handler::Configuration::InitializeConfiguration(&PostSetupApplicationHandle,
81+ // &ApplicationState).await;
82+ // Handler::ExtensionManagement::InitializeScanPaths(&
83+ // PostSetupApplicationHandle, &ApplicationState).await; ApplicationState.
84+ // ScanExtensions(&PostSetupApplicationHandle).await;
85+
86+ Vine :: Server :: Initialize :: Initialize (
87+ PostSetupApplicationHandle . clone ( ) ,
88+ "[::1]:50051" . to_string ( ) ,
89+ ) ;
90+ InitializeCocoon ( & PostSetupApplicationHandle ) . await ;
91+
92+ info ! ( "[SetupTask] Post-setup initializations complete." ) ;
93+ } ) ;
94+
95+ Ok ( ( ) )
96+ } )
97+ . plugin ( tauri_plugin_dialog:: init ( ) )
98+ . invoke_handler ( tauri:: generate_handler![
99+ // TODO: Re-integrate all Tauri command handlers here.
100+ crate :: Track :: DispatchLogic :: DispatchFrontendCommand ,
101+ ] )
102+ . build ( tauri:: generate_context!( ) )
103+ . expect ( "FATAL: Error while building Mountain Tauri application" )
104+ . run ( move |ApplicationHandle , Event | {
105+ if let RunEvent :: ExitRequested { api, .. } = Event {
106+ info ! ( "[RunEvent] Exit requested. Initiating graceful shutdown..." ) ;
107+ api. prevent_exit ( ) ;
108+ let SchedulerHandle = SchedulerForShutdown . clone ( ) ;
109+ let ApplicationHandleClone = ApplicationHandle . clone ( ) ;
110+
111+ // Spawn a new async task to handle the shutdown to avoid blocking the
112+ // Tauri event loop.
113+ tokio:: spawn ( async move {
114+ info ! ( "[Shutdown] Shutting down Echo scheduler..." ) ;
115+ if let Ok ( mut Scheduler ) = Arc :: try_unwrap ( SchedulerHandle ) {
116+ Scheduler . Stop ( ) . await ;
117+ } else {
118+ error ! ( "[Shutdown] Could not get exclusive access to scheduler for shutdown." ) ;
119+ }
120+ info ! ( "[Shutdown] Shutdown complete. Exiting application." ) ;
121+ ApplicationHandleClone . exit ( 0 ) ;
122+ } ) ;
109123 }
110- info ! ( "[Shutdown] Shutdown complete. Exiting application." ) ;
111- ApplicationHandle . exit ( 0 ) ;
112124 } ) ;
113- }
114- } ) ;
115125
116- info ! ( "[Main] Mountain application has shut down." ) ;
126+ info ! ( "[Main] Mountain application has shut down." ) ;
127+ } ) ;
117128}
0 commit comments