@@ -4,6 +4,7 @@ use moon_time::chrono::NaiveDateTime;
44use moon_time:: now_timestamp;
55use serde:: Serialize ;
66use starbase_utils:: json;
7+ use std:: env;
78use tokio:: task:: JoinHandle ;
89use tracing:: { debug, trace, warn} ;
910use uuid:: Uuid ;
@@ -21,21 +22,30 @@ pub struct WebhookPayload<'data, T: Serialize> {
2122 pub type_of : & ' data str ,
2223
2324 pub uuid : & ' data str ,
25+
26+ pub trace : & ' data str ,
2427}
2528
2629pub async fn notify_webhook (
2730 url : String ,
2831 body : String ,
32+ require_acknowledge : bool ,
2933) -> Result < reqwest:: Response , reqwest:: Error > {
30- reqwest:: Client :: new ( )
34+ let response = reqwest:: Client :: new ( )
3135 . post ( url)
3236 . body ( body)
3337 . header ( "Accept" , "application/json" )
3438 . header ( "Content-Type" , "application/json" )
3539 . header ( "Connection" , "keep-alive" )
3640 . header ( "Keep-Alive" , "timeout=30, max=120" )
3741 . send ( )
38- . await
42+ . await ?;
43+
44+ if require_acknowledge && !response. status ( ) . is_success ( ) {
45+ return Err ( response. error_for_status ( ) . unwrap_err ( ) ) ;
46+ }
47+
48+ Ok ( response)
3949}
4050
4151pub struct WebhooksNotifier {
@@ -44,11 +54,13 @@ pub struct WebhooksNotifier {
4454 requests : Vec < JoinHandle < ( ) > > ,
4555 url : String ,
4656 uuid : String ,
57+ trace : String ,
4758 verified : bool ,
59+ require_acknowledge : bool ,
4860}
4961
5062impl WebhooksNotifier {
51- pub fn new ( url : String ) -> Self {
63+ pub fn new ( url : String , require_acknowledge : bool ) -> Self {
5264 debug ! ( "Creating webhooks notifier for {}" , color:: url( & url) ) ;
5365
5466 WebhooksNotifier {
@@ -60,8 +72,10 @@ impl WebhooksNotifier {
6072 } else {
6173 Uuid :: new_v4 ( ) . to_string ( )
6274 } ,
75+ trace : env:: var ( "MOON_TRACE_ID" ) . unwrap_or_default ( ) ,
6376 url,
6477 verified : false ,
78+ require_acknowledge,
6579 }
6680 }
6781
@@ -78,15 +92,17 @@ impl WebhooksNotifier {
7892 event,
7993 type_of : name,
8094 uuid : & self . uuid ,
95+ trace : & self . trace ,
8196 } ;
8297 let body = json:: format ( & payload, false ) ?;
8398 let url = self . url . to_owned ( ) ;
99+ let require_acknowledge = self . require_acknowledge . to_owned ( ) ;
84100
85101 // For the first event, we want to ensure that the webhook URL is valid
86102 // by sending the request and checking for a failure. If failed,
87103 // we will disable subsequent requests from being called.
88104 if !self . verified {
89- let response = notify_webhook ( url, body) . await ;
105+ let response = notify_webhook ( url, body, require_acknowledge ) . await ;
90106
91107 if response. is_err ( ) || !response. unwrap ( ) . status ( ) . is_success ( ) {
92108 self . enabled = false ;
@@ -98,9 +114,11 @@ impl WebhooksNotifier {
98114 }
99115 // For every other event, we will make the request and ignore the result.
100116 // We will also avoid awaiting the request to not slow down the overall runner.
101- else {
102- self . requests . push ( tokio:: spawn ( async {
103- let _ = notify_webhook ( url, body) . await ;
117+ else if require_acknowledge {
118+ let _ = notify_webhook ( url. clone ( ) , body, true ) . await ;
119+ } else {
120+ self . requests . push ( tokio:: spawn ( async move {
121+ let _ = notify_webhook ( url, body, false ) . await ;
104122 } ) ) ;
105123 }
106124
0 commit comments