1
1
// The wit_bindgen_wasmtime::import below is triggering this lint.
2
2
#![ allow( clippy:: needless_question_mark) ]
3
3
4
- use std:: { sync :: Arc , time:: Duration } ;
4
+ use std:: time:: Duration ;
5
5
6
6
use anyhow:: Result ;
7
- use spin_engine:: { Builder , ExecutionContextConfiguration } ;
8
- use spin_manifest:: { CoreComponent , ModuleSource , WasmConfig } ;
7
+ use spin_core:: { Engine , InstancePre , Module } ;
9
8
10
9
wit_bindgen_wasmtime:: import!( { paths: [ "spin-timer.wit" ] , async : * } ) ;
11
10
12
- type ExecutionContext = spin_engine :: ExecutionContext < spin_timer:: SpinTimerData > ;
11
+ type RuntimeData = spin_timer:: SpinTimerData ;
13
12
14
13
#[ tokio:: main]
15
14
async fn main ( ) -> Result < ( ) > {
16
15
tracing_subscriber:: fmt ( )
17
16
. with_env_filter ( tracing_subscriber:: EnvFilter :: from_default_env ( ) )
18
17
. init ( ) ;
19
18
20
- let component = component ( ) ;
21
- let engine = Builder :: build_default ( ExecutionContextConfiguration {
22
- components : vec ! [ component] ,
23
- label : "timer-app" . to_string ( ) ,
24
- ..Default :: default ( )
25
- } )
26
- . await ?;
19
+ let engine = Engine :: builder ( & Default :: default ( ) ) ?. build ( ) ;
20
+
21
+ let module = Module :: from_file (
22
+ engine. as_ref ( ) ,
23
+ "example/target/wasm32-wasi/release/rust_echo_test.wasm" ,
24
+ ) ?;
25
+
26
+ let instance_pre = engine. instantiate_pre ( & module) ?;
27
+
27
28
let trigger = TimerTrigger {
28
- engine : Arc :: new ( engine) ,
29
+ engine,
30
+ instance_pre,
29
31
interval : Duration :: from_secs ( 1 ) ,
30
32
} ;
33
+
31
34
trigger. run ( ) . await
32
35
}
33
36
34
- /// A custom timer trigger that executes the
35
- /// first component of an application on every interval.
36
- #[ derive( Clone ) ]
37
+ /// A custom timer trigger that executes a component on
38
+ /// every interval.
37
39
pub struct TimerTrigger {
38
- /// The Spin execution context.
39
- engine : Arc < ExecutionContext > ,
40
- /// The interval at which the component is executed.
40
+ /// The Spin core engine.
41
+ pub engine : Engine < RuntimeData > ,
42
+ /// The pre-initialized component instance to execute.
43
+ pub instance_pre : InstancePre < RuntimeData > ,
44
+ /// The interval at which the component is executed.
41
45
pub interval : Duration ,
42
46
}
43
47
@@ -57,26 +61,15 @@ impl TimerTrigger {
57
61
}
58
62
/// Execute the first component in the application configuration.
59
63
async fn handle ( & self , msg : String ) -> Result < ( ) > {
60
- let ( mut store, instance) = self
61
- . engine
62
- . prepare_component ( & self . engine . config . components [ 0 ] . id , None , None , None , None )
64
+ let mut store = self . engine . store_builder ( ) . build ( ) ?;
65
+ let instance = self . instance_pre . instantiate_async ( & mut store) . await ?;
66
+ let timer_instance =
67
+ spin_timer:: SpinTimer :: new ( & mut store, & instance, |data| data. as_mut ( ) ) ?;
68
+ let res = timer_instance
69
+ . handle_timer_request ( & mut store, & msg)
63
70
. await ?;
64
-
65
- let t =
66
- spin_timer:: SpinTimer :: new ( & mut store, & instance, |host| host. data . as_mut ( ) . unwrap ( ) ) ?;
67
- let res = t. handle_timer_request ( & mut store, & msg) . await ?;
68
- log:: info!( "{}\n " , res) ;
71
+ tracing:: info!( "{}\n " , res) ;
69
72
70
73
Ok ( ( ) )
71
74
}
72
75
}
73
-
74
- pub fn component ( ) -> CoreComponent {
75
- CoreComponent {
76
- source : ModuleSource :: FileReference ( "target/test-programs/echo.wasm" . into ( ) ) ,
77
- id : "test" . to_string ( ) ,
78
- description : None ,
79
- wasm : WasmConfig :: default ( ) ,
80
- config : Default :: default ( ) ,
81
- }
82
- }
0 commit comments