diff --git a/examples/spin-timer/Cargo.lock b/examples/spin-timer/Cargo.lock index 17ac3b7ed..15218fadd 100644 --- a/examples/spin-timer/Cargo.lock +++ b/examples/spin-timer/Cargo.lock @@ -3481,6 +3481,18 @@ dependencies = [ "wit-bindgen-wasmtime", ] +[[package]] +name = "spin-key-value-redis" +version = "0.1.0" +dependencies = [ + "anyhow", + "redis", + "spin-core", + "spin-key-value", + "tokio", + "url", +] + [[package]] name = "spin-key-value-sqlite" version = "0.1.0" @@ -3557,6 +3569,7 @@ dependencies = [ "spin-config", "spin-core", "spin-key-value", + "spin-key-value-redis", "spin-key-value-sqlite", "spin-loader", "spin-manifest", diff --git a/examples/spin-timer/app-example/src/lib.rs b/examples/spin-timer/app-example/src/lib.rs index 49a8fe2d6..24ed18b59 100644 --- a/examples/spin-timer/app-example/src/lib.rs +++ b/examples/spin-timer/app-example/src/lib.rs @@ -6,9 +6,12 @@ wit_bindgen::generate!({ struct MySpinTimer; impl SpinTimer for MySpinTimer { - fn handle_timer_request() { + fn handle_timer_request() -> ContinueTimer { let text = spin_sdk::config::get("message").unwrap(); println!("{text}"); + + // Return ContinueTimer::True if you want to continue the timer loop calling this component/function subsequently. + ContinueTimer::True } } diff --git a/examples/spin-timer/spin-timer.wit b/examples/spin-timer/spin-timer.wit index 9d5fa73cc..7f4e85a99 100644 --- a/examples/spin-timer/spin-timer.wit +++ b/examples/spin-timer/spin-timer.wit @@ -1,3 +1,10 @@ default world spin-timer { - export handle-timer-request: func() + // Timer execution loop exit variant + variant continue-timer { + true, + false + } + + // Get the value of a key. + export handle-timer-request: func() -> continue-timer } diff --git a/examples/spin-timer/src/main.rs b/examples/spin-timer/src/main.rs index 6db0107f7..b3bd137dc 100644 --- a/examples/spin-timer/src/main.rs +++ b/examples/spin-timer/src/main.rs @@ -109,7 +109,15 @@ impl TriggerExecutor for TimerTrigger { let duration = tokio::time::Duration::from_millis(*d * 1000 / speedup); loop { tokio::time::sleep(duration).await; - self.handle_timer_event(c).await.unwrap(); + + // Inverse the control of breaking out of loop, let the component decide this by returning ContinueTimer enum. + let exit_condition = self.handle_timer_event(c).await.unwrap(); + + // Exit the loop if the component asks for it. + match exit_condition { + ContinueTimer::True => continue, + ContinueTimer::False => break, + } } }); } @@ -120,7 +128,7 @@ impl TriggerExecutor for TimerTrigger { } impl TimerTrigger { - async fn handle_timer_event(&self, component_id: &str) -> anyhow::Result<()> { + async fn handle_timer_event(&self, component_id: &str) -> anyhow::Result { // Load the guest... let (instance, mut store) = self.engine.prepare_instance(component_id).await?; let EitherInstance::Component(instance) = instance else {