Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Spin Timer Tigger Loop Exit Feature #1381

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 13 additions & 0 deletions examples/spin-timer/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 4 additions & 1 deletion examples/spin-timer/app-example/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
}

Expand Down
9 changes: 8 additions & 1 deletion examples/spin-timer/spin-timer.wit
Original file line number Diff line number Diff line change
@@ -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
}
12 changes: 10 additions & 2 deletions examples/spin-timer/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
}
}
});
}
Expand All @@ -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<ContinueTimer> {
// Load the guest...
let (instance, mut store) = self.engine.prepare_instance(component_id).await?;
let EitherInstance::Component(instance) = instance else {
Expand Down