Skip to content

Commit 84233c4

Browse files
committed
Migrate spin-timer example to new core
Signed-off-by: Lann Martin <lann.martin@fermyon.com>
1 parent 58d42f7 commit 84233c4

File tree

3 files changed

+31
-56
lines changed

3 files changed

+31
-56
lines changed

Cargo.lock

Lines changed: 1 addition & 10 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

examples/spin-timer/Cargo.toml

Lines changed: 1 addition & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -6,20 +6,11 @@ edition = "2021"
66

77
[dependencies]
88
anyhow = "1.0"
9-
async-trait = "0.1"
109
chrono = "0.4"
11-
env_logger = "0.9"
12-
futures = "0.3"
13-
log = { version = "0.4", default-features = false }
14-
spin-engine = { path = "../../crates/engine" }
15-
spin-manifest = { path = "../../crates/manifest" }
16-
spin-trigger = { path = "../../crates/trigger" }
10+
spin-core = { path = "../../crates/core" }
1711
tokio = { version = "1.14", features = [ "full" ] }
1812
tracing = { version = "0.1", features = [ "log" ] }
1913
tracing-subscriber = { version = "0.3.7", features = [ "env-filter" ] }
20-
wasi-common = "0.39.1"
21-
wasmtime = "0.39.1"
22-
wasmtime-wasi = "0.39.1"
2314

2415
[dependencies.wit-bindgen-wasmtime]
2516
git = "https://github.com/bytecodealliance/wit-bindgen"

examples/spin-timer/src/main.rs

Lines changed: 29 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -1,43 +1,47 @@
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

66
use anyhow::Result;
7-
use spin_engine::{Builder, ExecutionContextConfiguration};
8-
use spin_manifest::{CoreComponent, ModuleSource, WasmConfig};
7+
use spin_core::{Engine, InstancePre, Module};
98

109
wit_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]
1514
async 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.
3739
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.
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

Comments
 (0)