recoverable_thread_pool/thread_pool/
async.rs

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
use crate::*;
use recoverable_spawn::*;
use std::sync::Arc;

impl ThreadPool {
    #[inline]
    pub fn async_execute<F>(&self, job: F) -> SendResult
    where
        F: AsyncRecoverableFunction,
    {
        let job_with_handler = Box::new(move || {
            let _ = r#async::run_function(move || async {
                job.call().await;
            });
        });
        self.sender.send(job_with_handler)
    }

    #[inline]
    pub fn async_execute_with_catch<F, E>(&self, job: F, handle_error: E) -> SendResult
    where
        F: AsyncRecoverableFunction,
        E: AsyncErrorHandlerFunction,
    {
        let job_with_handler = Box::new(move || {
            let run_result: AsyncSpawnResult = r#async::run_function(move || async {
                job.call().await;
            });
            if let Err(err) = run_result {
                let err_string: String = r#async::tokio_error_to_string(err);
                let _: AsyncSpawnResult = r#async::run_error_handle_function(
                    move |err_str| async move {
                        handle_error.call(err_str).await;
                    },
                    Arc::new(err_string),
                );
            }
        });
        self.sender.send(job_with_handler)
    }

    #[inline]
    pub fn async_execute_with_catch_finally<F, E, L>(
        &self,
        job: F,
        handle_error: E,
        finally: L,
    ) -> SendResult
    where
        F: AsyncRecoverableFunction,
        E: AsyncErrorHandlerFunction,
        L: AsyncRecoverableFunction,
    {
        let job_with_handler = Box::new(move || {
            let run_result: AsyncSpawnResult = r#async::run_function(move || async {
                job.call().await;
            });
            if let Err(err) = run_result {
                let err_string: String = r#async::tokio_error_to_string(err);
                let _: AsyncSpawnResult = r#async::run_error_handle_function(
                    move |err_str| async move {
                        handle_error.call(err_str).await;
                    },
                    Arc::new(err_string),
                );
            }
            let _: AsyncSpawnResult = r#async::run_function(move || async {
                finally.call().await;
            });
        });
        self.sender.send(job_with_handler)
    }
}