recoverable_thread_pool/thread_pool/
async.rs

1use crate::*;
2use recoverable_spawn::*;
3use std::sync::Arc;
4
5impl ThreadPool {
6    #[inline]
7    pub fn async_execute<F>(&self, job: F) -> SendResult
8    where
9        F: AsyncRecoverableFunction,
10    {
11        let job_with_handler = Box::new(move || {
12            let _ = r#async::run_function(move || async {
13                job.call().await;
14            });
15        });
16        self.sender.send(job_with_handler)
17    }
18
19    #[inline]
20    pub fn async_execute_with_catch<F, E>(&self, job: F, handle_error: E) -> SendResult
21    where
22        F: AsyncRecoverableFunction,
23        E: AsyncErrorHandlerFunction,
24    {
25        let job_with_handler = Box::new(move || {
26            let run_result: AsyncSpawnResult = r#async::run_function(move || async {
27                job.call().await;
28            });
29            if let Err(err) = run_result {
30                let err_string: String = r#async::tokio_error_to_string(err);
31                let _: AsyncSpawnResult = r#async::run_error_handle_function(
32                    move |err_str| async move {
33                        handle_error.call(err_str).await;
34                    },
35                    Arc::new(err_string),
36                );
37            }
38        });
39        self.sender.send(job_with_handler)
40    }
41
42    #[inline]
43    pub fn async_execute_with_catch_finally<F, E, L>(
44        &self,
45        job: F,
46        handle_error: E,
47        finally: L,
48    ) -> SendResult
49    where
50        F: AsyncRecoverableFunction,
51        E: AsyncErrorHandlerFunction,
52        L: AsyncRecoverableFunction,
53    {
54        let job_with_handler = Box::new(move || {
55            let run_result: AsyncSpawnResult = r#async::run_function(move || async {
56                job.call().await;
57            });
58            if let Err(err) = run_result {
59                let err_string: String = r#async::tokio_error_to_string(err);
60                let _: AsyncSpawnResult = r#async::run_error_handle_function(
61                    move |err_str| async move {
62                        handle_error.call(err_str).await;
63                    },
64                    Arc::new(err_string),
65                );
66            }
67            let _: AsyncSpawnResult = r#async::run_function(move || async {
68                finally.call().await;
69            });
70        });
71        self.sender.send(job_with_handler)
72    }
73}