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