11// The wit_bindgen_wasmtime::import below is triggering this lint.
22#![ allow( clippy:: needless_question_mark) ]
33
4- use std:: { sync :: Arc , time:: Duration } ;
4+ use std:: time:: Duration ;
55
66use anyhow:: Result ;
7- use spin_engine:: { Builder , ExecutionContextConfiguration } ;
8- use spin_manifest:: { CoreComponent , ModuleSource , WasmConfig } ;
7+ use spin_core:: { Engine , InstancePre , Module } ;
98
109wit_bindgen_wasmtime:: import!( { paths: [ "spin-timer.wit" ] , async : * } ) ;
1110
12- type ExecutionContext = spin_engine :: ExecutionContext < spin_timer:: SpinTimerData > ;
11+ type RuntimeData = spin_timer:: SpinTimerData ;
1312
1413#[ tokio:: main]
1514async fn main ( ) -> Result < ( ) > {
1615 tracing_subscriber:: fmt ( )
1716 . with_env_filter ( tracing_subscriber:: EnvFilter :: from_default_env ( ) )
1817 . init ( ) ;
1918
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+
2728 let trigger = TimerTrigger {
28- engine : Arc :: new ( engine) ,
29+ engine,
30+ instance_pre,
2931 interval : Duration :: from_secs ( 1 ) ,
3032 } ;
33+
3134 trigger. run ( ) . await
3235}
3336
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.
3739pub 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.
4145 pub interval : Duration ,
4246}
4347
@@ -57,26 +61,15 @@ impl TimerTrigger {
5761 }
5862 /// Execute the first component in the application configuration.
5963 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)
6370 . 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) ;
6972
7073 Ok ( ( ) )
7174 }
7275}
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