-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Event Workflows (phase 1: Custom listeners) (#359)
* Add wrapper types + trait structure to enforce structure for event flows * Decoupling/refactor and ensure integration test passes * periodic web poller working and further refactor of macro code * Everything compiling, integration test passes
- Loading branch information
Showing
17 changed files
with
874 additions
and
295 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -25,3 +25,4 @@ CODEGEN_LOCK | |
blueprint.json | ||
blueprint*.json | ||
blueprint.lock | ||
/blueprints/periodic-web-poller/node_modules/ |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
[package] | ||
name = "periodic-web-poller-blueprint" | ||
version = "0.1.1" | ||
description = "A Simple Blueprint to demo how to run blueprints dependent on an arbitrary events" | ||
authors.workspace = true | ||
edition.workspace = true | ||
license.workspace = true | ||
homepage.workspace = true | ||
repository.workspace = true | ||
publish = false | ||
|
||
[dependencies] | ||
tracing = { workspace = true } | ||
async-trait = { workspace = true } | ||
gadget-sdk = { workspace = true, features = ["std"] } | ||
color-eyre = { workspace = true } | ||
lock_api = { workspace = true } | ||
tokio = { workspace = true, default-features = false, features = ["full"] } | ||
tokio-util = { workspace = true } | ||
sp-core = { workspace = true } | ||
subxt-signer = { workspace = true, features = ["sr25519", "subxt", "std"] } | ||
parking_lot = { workspace = true } | ||
ed25519-zebra = { workspace = true, features = ["pkcs8", "default", "der", "std", "serde", "pem"] } | ||
structopt = { workspace = true } | ||
hex = { workspace = true } | ||
k256 = { workspace = true } | ||
serde_json = { workspace = true } | ||
reqwest = { workspace = true } | ||
|
||
[build-dependencies] | ||
blueprint-metadata = { workspace = true } | ||
|
||
[features] | ||
default = ["std"] | ||
std = [] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
fn main() { | ||
println!("cargo:rerun-if-changed=src/*"); | ||
println!("cargo:rerun-if-changed=src/lib.rs"); | ||
println!("cargo:rerun-if-changed=src/main.rs"); | ||
blueprint_metadata::generate_json(); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,83 @@ | ||
use async_trait::async_trait; | ||
use gadget_sdk::event_listener::periodic::PeriodicEventListener; | ||
use gadget_sdk::event_listener::EventListener; | ||
use gadget_sdk::job; | ||
use std::convert::Infallible; | ||
|
||
#[job( | ||
id = 0, | ||
params(value), | ||
result(_), | ||
event_listener( | ||
listener = PeriodicEventListener<2000, WebPoller, serde_json::Value, reqwest::Client>, | ||
event = serde_json::Value, | ||
pre_processor = pre_process, | ||
post_processor = post_process, | ||
), | ||
)] | ||
// Maps a boolean value obtained from pre-processing to a u8 value | ||
pub async fn web_poller(value: bool, client: reqwest::Client) -> Result<u8, Infallible> { | ||
gadget_sdk::info!("Running web_poller on value: {value}"); | ||
Ok(value as u8) | ||
} | ||
|
||
// Maps a JSON response to a boolean value | ||
async fn pre_process(event: serde_json::Value) -> Result<bool, gadget_sdk::Error> { | ||
gadget_sdk::info!("Running web_poller pre-processor on value: {event}"); | ||
let completed = event["completed"].as_bool().unwrap_or(false); | ||
Ok(completed) | ||
} | ||
|
||
// Received the u8 value output from the job and performs any last post-processing | ||
async fn post_process(job_output: u8) -> Result<(), gadget_sdk::Error> { | ||
gadget_sdk::info!("Running web_poller post-processor on value: {job_output}"); | ||
if job_output == 1 { | ||
Ok(()) | ||
} else { | ||
Err(gadget_sdk::Error::Other( | ||
"Job failed since query returned with a false status".to_string(), | ||
)) | ||
} | ||
} | ||
|
||
/// Define an event listener that polls a webserver | ||
pub struct WebPoller { | ||
pub client: reqwest::Client, | ||
} | ||
|
||
#[async_trait] | ||
impl EventListener<serde_json::Value, reqwest::Client> for WebPoller { | ||
async fn new(context: &reqwest::Client) -> Result<Self, gadget_sdk::Error> | ||
where | ||
Self: Sized, | ||
{ | ||
Ok(Self { | ||
client: context.clone(), | ||
}) | ||
} | ||
|
||
/// Implement the logic that polls the web server | ||
async fn next_event(&mut self) -> Option<serde_json::Value> { | ||
// Send a GET request to the JSONPlaceholder API | ||
let response = self | ||
.client | ||
.get("https://jsonplaceholder.typicode.com/todos/10") | ||
.send() | ||
.await | ||
.ok()?; | ||
|
||
// Check if the request was successful | ||
if response.status().is_success() { | ||
// Parse the JSON response | ||
let resp: serde_json::Value = response.json().await.ok()?; | ||
Some(resp) | ||
} else { | ||
None | ||
} | ||
} | ||
|
||
/// Implement any handler logic when an event is received | ||
async fn handle_event(&mut self, _event: serde_json::Value) -> Result<(), gadget_sdk::Error> { | ||
unreachable!("Not called here") | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
use color_eyre::Result; | ||
use gadget_sdk::info; | ||
use gadget_sdk::job_runner::MultiJobRunner; | ||
use periodic_web_poller_blueprint as blueprint; | ||
|
||
#[gadget_sdk::main] | ||
async fn main() { | ||
let web_poller = blueprint::WebPollerEventHandler { | ||
client: reqwest::Client::new(), | ||
}; | ||
|
||
info!("~~~ Executing the periodic web poller ~~~"); | ||
MultiJobRunner::new(None) | ||
.with_job() | ||
.finish(web_poller) | ||
.run() | ||
.await?; | ||
|
||
info!("Exiting..."); | ||
Ok(()) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.