openzeppelin_relayer/jobs/handlers/
mod.rs

1use 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
22mod transaction_cleanup_handler;
23pub use transaction_cleanup_handler::*;
24
25pub fn handle_result(
26    result: Result<(), Report>,
27    attempt: Attempt,
28    job_type: &str,
29    max_attempts: usize,
30) -> Result<(), Error> {
31    if result.is_ok() {
32        info!("{} request handled successfully", job_type);
33        return Ok(());
34    }
35    info!("{} request failed: {:?}", job_type, result);
36
37    if attempt.current() >= max_attempts {
38        info!("Max attempts ({}) reached, failing job", max_attempts);
39        Err(Error::Abort(Arc::new("Failed to handle request".into())))?
40    }
41
42    Err(Error::Failed(Arc::new(
43        "Failed to handle request. Retrying".into(),
44    )))?
45}
46
47#[cfg(test)]
48mod tests {
49    use super::*;
50    use apalis::prelude::Attempt;
51
52    #[test]
53    fn test_handle_result_success() {
54        let result: Result<(), Report> = Ok(());
55        let attempt = Attempt::default();
56
57        let handled = handle_result(result, attempt, "test_job", 3);
58        assert!(handled.is_ok());
59    }
60
61    #[test]
62    fn test_handle_result_retry() {
63        let result: Result<(), Report> = Err(Report::msg("Test error"));
64        let attempt = Attempt::default();
65
66        let handled = handle_result(result, attempt, "test_job", 3);
67
68        assert!(handled.is_err());
69        match handled {
70            Err(Error::Failed(_)) => {
71                // This is the expected error type for a retry
72            }
73            _ => panic!("Expected Failed error for retry"),
74        }
75    }
76
77    #[test]
78    fn test_handle_result_abort() {
79        let result: Result<(), Report> = Err(Report::msg("Test error"));
80        let attempt = Attempt::default();
81        for _ in 0..3 {
82            attempt.increment();
83        }
84
85        let handled = handle_result(result, attempt, "test_job", 3);
86
87        assert!(handled.is_err());
88        match handled {
89            Err(Error::Abort(_)) => {
90                // This is the expected error type for an abort
91            }
92            _ => panic!("Expected Abort error for max attempts"),
93        }
94    }
95
96    #[test]
97    fn test_handle_result_max_attempts_exceeded() {
98        let result: Result<(), Report> = Err(Report::msg("Test error"));
99        let attempt = Attempt::default();
100        for _ in 0..5 {
101            attempt.increment();
102        }
103
104        let handled = handle_result(result, attempt, "test_job", 3);
105
106        assert!(handled.is_err());
107        match handled {
108            Err(Error::Abort(_)) => {
109                // This is the expected error type for exceeding max attempts
110            }
111            _ => panic!("Expected Abort error for exceeding max attempts"),
112        }
113    }
114}