@@ -4,6 +4,7 @@ use std::{collections::HashMap, sync::Arc};
4
4
5
5
use crate :: {
6
6
event:: HermesEventPayload ,
7
+ pool,
7
8
runtime_context:: HermesRuntimeContext ,
8
9
runtime_extensions:: {
9
10
init:: trait_event:: { RteEvent , RteInitEvent } ,
@@ -41,7 +42,7 @@ pub(crate) struct Application {
41
42
name : ApplicationName ,
42
43
43
44
/// WASM modules
44
- indexed_modules : HashMap < ModuleId , Module > ,
45
+ indexed_modules : HashMap < ModuleId , Arc < Module > > ,
45
46
46
47
/// Application's `Vfs` instance
47
48
vfs : Arc < Vfs > ,
@@ -57,7 +58,7 @@ impl Application {
57
58
) -> Self {
58
59
let indexed_modules = modules
59
60
. into_iter ( )
60
- . map ( |module| ( module. id ( ) . clone ( ) , module) )
61
+ . map ( |module| ( module. id ( ) . clone ( ) , Arc :: new ( module) ) )
61
62
. collect ( ) ;
62
63
Self {
63
64
name : app_name,
@@ -79,66 +80,78 @@ impl Application {
79
80
/// Dispatch event for all available modules.
80
81
pub ( crate ) fn dispatch_event (
81
82
& self ,
82
- event : & dyn HermesEventPayload ,
83
- ) -> anyhow :: Result < ( ) > {
83
+ event : & Arc < dyn HermesEventPayload > ,
84
+ ) {
84
85
for module in self . indexed_modules . values ( ) {
85
86
module_dispatch_event (
86
- module,
87
+ module. clone ( ) ,
87
88
self . name . clone ( ) ,
88
89
module. id ( ) . clone ( ) ,
89
90
self . vfs . clone ( ) ,
90
- event,
91
- ) ? ;
91
+ event. clone ( ) ,
92
+ ) ;
92
93
}
93
- Ok ( ( ) )
94
94
}
95
95
96
96
/// Dispatch event for the target module by the `module_id`.
97
97
pub ( crate ) fn dispatch_event_for_target_module (
98
98
& self ,
99
99
module_id : ModuleId ,
100
- event : & dyn HermesEventPayload ,
100
+ event : Arc < dyn HermesEventPayload > ,
101
101
) -> anyhow:: Result < ( ) > {
102
102
let module = self
103
103
. indexed_modules
104
104
. get ( & module_id)
105
105
. ok_or ( anyhow:: anyhow!( "Module {module_id} not found" ) ) ?;
106
106
module_dispatch_event (
107
- module,
107
+ module. clone ( ) ,
108
108
self . name . clone ( ) ,
109
109
module_id,
110
110
self . vfs . clone ( ) ,
111
111
event,
112
- )
112
+ ) ;
113
+ Ok ( ( ) )
113
114
}
114
115
}
115
116
116
117
/// Dispatch event
117
118
pub ( crate ) fn module_dispatch_event (
118
- module : & Module ,
119
+ module : Arc < Module > ,
119
120
app_name : ApplicationName ,
120
121
module_id : ModuleId ,
121
122
vfs : Arc < Vfs > ,
122
- event : & dyn HermesEventPayload ,
123
- ) -> anyhow:: Result < ( ) > {
124
- let runtime_ctx = HermesRuntimeContext :: new (
125
- app_name,
126
- module_id,
127
- event. event_name ( ) . to_string ( ) ,
128
- module. exec_counter ( ) ,
129
- vfs,
130
- ) ;
131
-
132
- // Advise Runtime Extensions of a new context
133
- // TODO: Better handle errors.
134
- RteEvent :: new ( ) . init ( & runtime_ctx) ?;
135
- // TODO: (SJ) Remove when all RTE's are migrated.
136
- new_context ( & runtime_ctx) ;
137
-
138
- module. execute_event ( event, runtime_ctx. clone ( ) ) ?;
139
-
140
- // Advise Runtime Extensions that context can be cleaned up.
141
- RteEvent :: new ( ) . fini ( & runtime_ctx) ?;
142
-
143
- Ok ( ( ) )
123
+ event : Arc < dyn HermesEventPayload > ,
124
+ ) {
125
+ // TODO(@aido-mth): fix how init is processed. https://github.com/input-output-hk/hermes/issues/490
126
+ pool:: execute ( move || {
127
+ let runtime_ctx = HermesRuntimeContext :: new (
128
+ app_name,
129
+ module_id,
130
+ event. event_name ( ) . to_string ( ) ,
131
+ module. exec_counter ( ) ,
132
+ vfs,
133
+ ) ;
134
+
135
+ // Advise Runtime Extensions of a new context
136
+ // TODO: Better handle errors.
137
+ if let Err ( err) = RteEvent :: new ( ) . init ( & runtime_ctx) {
138
+ tracing:: error!( "module event initialization failed: {err}" ) ;
139
+ return ;
140
+ }
141
+
142
+ // TODO: (SJ) Remove when all RTE's are migrated.
143
+ new_context ( & runtime_ctx) ;
144
+
145
+ if let Err ( err) = module. execute_event ( event. as_ref ( ) , runtime_ctx. clone ( ) ) {
146
+ tracing:: error!( "module event execution failed: {err}" ) ;
147
+ return ;
148
+ }
149
+
150
+ // Advise Runtime Extensions that context can be cleaned up.
151
+ drop (
152
+ RteEvent :: new ( )
153
+ . fini ( & runtime_ctx)
154
+ . inspect_err ( |err| tracing:: error!( "module event finalization failed: {err}" ) ) ,
155
+ ) ;
156
+ } ) ;
144
157
}
0 commit comments