Skip to content

Commit 0719cf6

Browse files
author
Mauricio Cassola
committed
Add event handlers
1 parent e716b51 commit 0719cf6

File tree

4 files changed

+39
-12
lines changed

4 files changed

+39
-12
lines changed

src/db.rs

Lines changed: 14 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ use std::sync::{Arc, Mutex};
55
use tokio::sync::{OwnedSemaphorePermit, Semaphore};
66
use tokio_postgres::Client as DbClient;
77
use crate::db::events::*;
8+
use crate::handlers::events::handle_event;
89

910
pub mod events;
1011
pub mod issue_data;
@@ -182,21 +183,25 @@ pub async fn run_migrations(client: &DbClient) -> anyhow::Result<()> {
182183
}
183184

184185
pub async fn run_scheduled_events(db: &DbClient) -> anyhow::Result<()> {
185-
// table lock ????
186-
187-
let events = get_events_to_execute(&db).await;
186+
let events = get_events_to_execute(&db).await.unwrap();
188187
println!("events to execute: {:#?}", events);
188+
tracing::trace!("events to execute: {:#?}", events);
189189

190-
for event in events.unwrap().iter() {
191-
update_event_executed_at(&db, &event.event_id).await;
192-
match call_event_handler_based_on_event_name {
193-
Ok(r) => {
190+
for event in events.iter() {
191+
update_event_executed_at(&db, &event.event_id).await?;
192+
193+
match handle_event(&event.event_name, &event.event_metadata).await {
194+
Ok(_) => {
195+
println!("event succesfully executed (id={})", event.event_id);
194196
tracing::trace!("event succesfully executed (id={})", event.event_id);
195-
delete_event(&db, &event.event_id).await;
197+
198+
delete_event(&db, &event.event_id).await?;
196199
},
197200
Err(e) => {
201+
println!("event failed on execution (id={:?}, error={:?})", event.event_id, e);
198202
tracing::trace!("event failed on execution (id={:?}, error={:?})", event.event_id, e);
199-
update_event_failed_message(&db, &event.event_id, &e).await;
203+
204+
update_event_failed_message(&db, &event.event_id, &e.to_string()).await?;
200205
},
201206
}
202207
}

src/db/events.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ pub struct Event {
1010
pub event_id: Uuid,
1111
pub event_name: String,
1212
pub expected_event_time: DateTime<FixedOffset>,
13-
// pub event_metadata: String,
13+
pub event_metadata: serde_json::Value,
1414
pub executed_at: DateTime<FixedOffset>,
1515
pub failed: Option<String>,
1616
}
@@ -85,15 +85,15 @@ pub async fn get_events_to_execute(db: &DbClient) -> Result<Vec<Event>> {
8585
let event_id: Uuid = event.get(0);
8686
let event_name: String = event.get(1);
8787
let expected_event_time: DateTime<FixedOffset> = event.get(2);
88-
// let event_metadata: String = event.get(3);
88+
let event_metadata: serde_json::Value = event.get(3);
8989
let executed_at: DateTime<FixedOffset> = event.get(4);
9090
let failed: Option<String> = event.get(5);
9191

9292
data.push(Event {
9393
event_id,
9494
event_name,
9595
expected_event_time,
96-
// event_metadata,
96+
event_metadata,
9797
executed_at,
9898
failed
9999
});

src/handlers.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@ mod review_submitted;
4343
mod rfc_helper;
4444
mod rustc_commits;
4545
mod shortcut;
46+
pub mod events;
4647

4748
pub async fn handle(ctx: &Context, event: &Event) -> Vec<HandlerError> {
4849
let config = config::get(&ctx.github, event.repo()).await;

src/handlers/events.rs

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
// Function to match the scheduled event function with its corresponding handler.
2+
// In case you want to add a new one, just add a new clause to the match with
3+
// the event name and the corresponding function.
4+
5+
// The metadata is a serde_json::Value, please visit: https://docs.rs/serde_json/latest/serde_json/value/enum.Value.html
6+
// to refer on how to get values from there.
7+
// Example of accessing an integer id in the metadata:
8+
// event_metadata["id"].as_i64().unwrap();
9+
10+
pub async fn handle_event(event_name: &String, event_metadata: &serde_json::Value) -> anyhow::Result<()> {
11+
match event_name {
12+
_ => default(&event_name, &event_metadata)
13+
}
14+
}
15+
16+
fn default(event_name: &String, event_metadata: &serde_json::Value) -> anyhow::Result<()> {
17+
println!("handle_event fall in default cause: (name={:?}, metadata={:?})", event_name, event_metadata);
18+
tracing::trace!("handle_event fall in default cause: (name={:?}, metadata={:?})", event_name, event_metadata);
19+
20+
Ok(())
21+
}

0 commit comments

Comments
 (0)