openzeppelin_relayer/jobs/handlers/
mod.rs1use std::sync::Arc;
2
3use apalis::prelude::{Attempt, Error};
4use eyre::Report;
5
6mod transaction_request_handler;
7use log::info;
8pub use transaction_request_handler::*;
9
10mod transaction_submission_handler;
11pub use transaction_submission_handler::*;
12
13mod notification_handler;
14pub use notification_handler::*;
15
16mod transaction_status_handler;
17pub use transaction_status_handler::*;
18
19mod solana_swap_request_handler;
20pub use solana_swap_request_handler::*;
21
22pub fn handle_result(
23 result: Result<(), Report>,
24 attempt: Attempt,
25 job_type: &str,
26 max_attempts: usize,
27) -> Result<(), Error> {
28 if result.is_ok() {
29 info!("{} request handled successfully", job_type);
30 return Ok(());
31 }
32 info!("{} request failed: {:?}", job_type, result);
33
34 if attempt.current() >= max_attempts {
35 info!("Max attempts ({}) reached, failing job", max_attempts);
36 Err(Error::Abort(Arc::new("Failed to handle request".into())))?
37 }
38
39 Err(Error::Failed(Arc::new(
40 "Failed to handle request. Retrying".into(),
41 )))?
42}
43
44#[cfg(test)]
45mod tests {
46 use super::*;
47 use apalis::prelude::Attempt;
48
49 #[test]
50 fn test_handle_result_success() {
51 let result: Result<(), Report> = Ok(());
52 let attempt = Attempt::default();
53
54 let handled = handle_result(result, attempt, "test_job", 3);
55 assert!(handled.is_ok());
56 }
57
58 #[test]
59 fn test_handle_result_retry() {
60 let result: Result<(), Report> = Err(Report::msg("Test error"));
61 let attempt = Attempt::default();
62
63 let handled = handle_result(result, attempt, "test_job", 3);
64
65 assert!(handled.is_err());
66 match handled {
67 Err(Error::Failed(_)) => {
68 }
70 _ => panic!("Expected Failed error for retry"),
71 }
72 }
73
74 #[test]
75 fn test_handle_result_abort() {
76 let result: Result<(), Report> = Err(Report::msg("Test error"));
77 let attempt = Attempt::default();
78 for _ in 0..3 {
79 attempt.increment();
80 }
81
82 let handled = handle_result(result, attempt, "test_job", 3);
83
84 assert!(handled.is_err());
85 match handled {
86 Err(Error::Abort(_)) => {
87 }
89 _ => panic!("Expected Abort error for max attempts"),
90 }
91 }
92
93 #[test]
94 fn test_handle_result_max_attempts_exceeded() {
95 let result: Result<(), Report> = Err(Report::msg("Test error"));
96 let attempt = Attempt::default();
97 for _ in 0..5 {
98 attempt.increment();
99 }
100
101 let handled = handle_result(result, attempt, "test_job", 3);
102
103 assert!(handled.is_err());
104 match handled {
105 Err(Error::Abort(_)) => {
106 }
108 _ => panic!("Expected Abort error for exceeding max attempts"),
109 }
110 }
111}