recoverable_thread_pool/thread_pool/async/impl.rs
1use crate::*;
2use recoverable_spawn::r#async::*;
3
4/// Async implementation of thread pool operations.
5impl ThreadPool {
6 /// Executes an async job in the thread pool.
7 ///
8 /// # Arguments
9 ///
10 /// - `F` - The async function to execute.
11 ///
12 /// # Returns
13 ///
14 /// - `SendResult` - Result of the job submission.
15 pub fn async_execute<F>(&self, job: F) -> SendResult
16 where
17 F: AsyncRecoverableFunction,
18 {
19 let job_with_handler = Box::new(move || {
20 Builder::new_current_thread()
21 .enable_all()
22 .build()
23 .unwrap()
24 .block_on(async move {
25 let _ = async_run_function(move || async {
26 job.call().await;
27 })
28 .await;
29 });
30 });
31 self.sender.send(job_with_handler)
32 }
33
34 /// Executes an async job with error handling in the thread pool.
35 ///
36 /// # Arguments
37 ///
38 /// - `F` - The async function to execute.
39 /// - `E` - The async error handler function.
40 ///
41 /// # Returns
42 ///
43 /// - `SendResult` - Result of the job submission.
44 pub fn async_execute_with_catch<F, E>(&self, job: F, handle_error: E) -> SendResult
45 where
46 F: AsyncRecoverableFunction,
47 E: AsyncErrorHandlerFunction,
48 {
49 let job_with_handler = Box::new(move || {
50 Builder::new_current_thread()
51 .enable_all()
52 .build()
53 .unwrap()
54 .block_on(async move {
55 let run_result: AsyncSpawnResult = async_run_function(move || async {
56 job.call().await;
57 })
58 .await;
59 if let Err(err) = run_result {
60 let err_string: String = tokio_error_to_string(&err);
61 let _: AsyncSpawnResult = async_run_error_handle_function(
62 move |err_str| async move {
63 handle_error.call(err_str).await;
64 },
65 Arc::new(err_string),
66 )
67 .await;
68 }
69 });
70 });
71 self.sender.send(job_with_handler)
72 }
73
74 /// Executes an async job with error handling and finalization in the thread pool.
75 ///
76 /// # Arguments
77 ///
78 /// - `F` - The async function to execute.
79 /// - `E` - The async error handler function.
80 /// - `L` - The async finally handler function.
81 ///
82 /// # Returns
83 ///
84 /// - `SendResult` - Result of the job submission.
85 pub fn async_execute_with_catch_finally<F, E, L>(
86 &self,
87 job: F,
88 handle_error: E,
89 finally: L,
90 ) -> SendResult
91 where
92 F: AsyncRecoverableFunction,
93 E: AsyncErrorHandlerFunction,
94 L: AsyncRecoverableFunction,
95 {
96 let job_with_handler = Box::new(move || {
97 Builder::new_current_thread()
98 .enable_all()
99 .build()
100 .unwrap()
101 .block_on(async move {
102 let run_result: AsyncSpawnResult = async_run_function(move || async {
103 job.call().await;
104 })
105 .await;
106 if let Err(err) = run_result {
107 let err_string: String = tokio_error_to_string(&err);
108 let _: AsyncSpawnResult = async_run_error_handle_function(
109 move |err_str| async move {
110 handle_error.call(err_str).await;
111 },
112 Arc::new(err_string),
113 )
114 .await;
115 }
116 let _: AsyncSpawnResult = async_run_function(move || async {
117 finally.call().await;
118 })
119 .await;
120 });
121 });
122 self.sender.send(job_with_handler)
123 }
124}