recoverable_thread_pool/thread_pool/async/
impl.rs

1use crate::*;
2use recoverable_spawn::r#async::*;
3
4impl ThreadPool {
5    pub fn async_execute<F>(&self, job: F) -> SendResult
6    where
7        F: AsyncRecoverableFunction,
8    {
9        let job_with_handler = Box::new(move || {
10            Builder::new_current_thread()
11                .enable_all()
12                .build()
13                .unwrap()
14                .block_on(async move {
15                    let _ = async_run_function(move || async {
16                        job.call().await;
17                    })
18                    .await;
19                });
20        });
21        self.sender.send(job_with_handler)
22    }
23
24    pub fn async_execute_with_catch<F, E>(&self, job: F, handle_error: E) -> SendResult
25    where
26        F: AsyncRecoverableFunction,
27        E: AsyncErrorHandlerFunction,
28    {
29        let job_with_handler = Box::new(move || {
30            Builder::new_current_thread()
31                .enable_all()
32                .build()
33                .unwrap()
34                .block_on(async move {
35                    let run_result: AsyncSpawnResult = async_run_function(move || async {
36                        job.call().await;
37                    })
38                    .await;
39                    if let Err(err) = run_result {
40                        let err_string: String = tokio_error_to_string(&err);
41                        let _: AsyncSpawnResult = async_run_error_handle_function(
42                            move |err_str| async move {
43                                handle_error.call(err_str).await;
44                            },
45                            Arc::new(err_string),
46                        )
47                        .await;
48                    }
49                });
50        });
51        self.sender.send(job_with_handler)
52    }
53
54    pub fn async_execute_with_catch_finally<F, E, L>(
55        &self,
56        job: F,
57        handle_error: E,
58        finally: L,
59    ) -> SendResult
60    where
61        F: AsyncRecoverableFunction,
62        E: AsyncErrorHandlerFunction,
63        L: AsyncRecoverableFunction,
64    {
65        let job_with_handler = Box::new(move || {
66            Builder::new_current_thread()
67                .enable_all()
68                .build()
69                .unwrap()
70                .block_on(async move {
71                    let run_result: AsyncSpawnResult = async_run_function(move || async {
72                        job.call().await;
73                    })
74                    .await;
75                    if let Err(err) = run_result {
76                        let err_string: String = tokio_error_to_string(&err);
77                        let _: AsyncSpawnResult = async_run_error_handle_function(
78                            move |err_str| async move {
79                                handle_error.call(err_str).await;
80                            },
81                            Arc::new(err_string),
82                        )
83                        .await;
84                    }
85                    let _: AsyncSpawnResult = async_run_function(move || async {
86                        finally.call().await;
87                    })
88                    .await;
89                });
90        });
91        self.sender.send(job_with_handler)
92    }
93}