Skip to content

Commit e84c865

Browse files
Merge pull request #11 from fibonacci1729/async
feat: support async handlers
2 parents c11a223 + f842e05 commit e84c865

File tree

7 files changed

+42
-34
lines changed

7 files changed

+42
-34
lines changed

Cargo.lock

Lines changed: 12 additions & 4 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

guest-rust/Cargo.lock

Lines changed: 13 additions & 16 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

guest-rust/src/lib.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ use spin_cron_sdk::{cron_component, Error, Metadata};
22
use spin_sdk::variables;
33

44
#[cron_component]
5-
fn handle_cron_event(metadata: Metadata) -> Result<(), Error> {
5+
async fn handle_cron_event(metadata: Metadata) -> Result<(), Error> {
66
let key = variables::get("something").unwrap_or_default();
77
println!(
88
"[{}] Hello this is me running every {}",

sdk/Cargo.toml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,5 +10,6 @@ include = ["../cron.wit"]
1010
name = "spin_cron_sdk"
1111

1212
[dependencies]
13+
spin-executor = "3.0.1"
1314
spin-cron-macro = { path = "macro" }
14-
wit-bindgen = { workspace = true }
15+
wit-bindgen = { workspace = true }

sdk/macro/Cargo.toml

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -10,10 +10,6 @@ name = "spin_cron_macro"
1010
proc-macro = true
1111

1212
[dependencies]
13-
anyhow = "1"
14-
bytes = "1"
15-
http = "0.2"
1613
proc-macro2 = "1"
1714
quote = "1.0"
18-
syn = { version = "1.0", features = ["full"] }
19-
wit-bindgen = { workspace = true }
15+
syn = { version = "1.0", features = ["full"] }

sdk/macro/src/lib.rs

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ const WIT_PATH: &str = concat!(env!("CARGO_MANIFEST_DIR"), "/../../cron.wit");
77
pub fn cron_component(_attr: TokenStream, item: TokenStream) -> TokenStream {
88
let func = syn::parse_macro_input!(item as syn::ItemFn);
99
let func_name = &func.sig.ident;
10+
let await_postfix = func.sig.asyncness.map(|_| quote!(.await));
1011
let preamble = preamble();
1112

1213
quote!(
@@ -17,13 +18,15 @@ pub fn cron_component(_attr: TokenStream, item: TokenStream) -> TokenStream {
1718
}
1819
impl self::preamble::Guest for preamble::Cron {
1920
fn handle_cron_event(metadata: ::spin_cron_sdk::Metadata) -> ::std::result::Result<(), ::spin_cron_sdk::Error> {
20-
match super::#func_name(metadata) {
21-
::std::result::Result::Ok(()) => ::std::result::Result::Ok(()),
22-
::std::result::Result::Err(e) => {
23-
eprintln!("{}", e);
24-
::std::result::Result::Err(::spin_cron_sdk::Error::Other(e.to_string()))
25-
},
26-
}
21+
::spin_cron_sdk::executor::run(async move {
22+
match super::#func_name(metadata)#await_postfix {
23+
::std::result::Result::Ok(()) => ::std::result::Result::Ok(()),
24+
::std::result::Result::Err(e) => {
25+
eprintln!("{}", e);
26+
::std::result::Result::Err(::spin_cron_sdk::Error::Other(e.to_string()))
27+
},
28+
}
29+
})
2730
}
2831
}
2932
}

sdk/src/lib.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
pub use spin_cron_macro::cron_component;
22

3+
#[doc(hidden)]
4+
pub use spin_executor as executor;
5+
36
#[doc(hidden)]
47
pub mod wit {
58
#![allow(missing_docs)]

0 commit comments

Comments
 (0)