]>
Commit | Line | Data |
---|---|---|
d1993187 WB |
1 | use anyhow::Error; |
2 | ||
3 | /// `WorkerTask` methods commonly used from contexts otherwise not related to the API server. | |
4 | pub trait TaskState { | |
5 | /// If the task should be aborted, this should fail with a reasonable error message. | |
6 | fn check_abort(&self) -> Result<(), Error>; | |
7 | ||
8 | /// Create a log message for this task. | |
9 | fn log(&self, level: log::Level, message: &std::fmt::Arguments); | |
10 | } | |
11 | ||
12 | /// Convenience implementation: | |
13 | impl<T: TaskState + ?Sized> TaskState for std::sync::Arc<T> { | |
14 | fn check_abort(&self) -> Result<(), Error> { | |
15 | <T as TaskState>::check_abort(&*self) | |
16 | } | |
17 | ||
18 | fn log(&self, level: log::Level, message: &std::fmt::Arguments) { | |
19 | <T as TaskState>::log(&*self, level, message) | |
20 | } | |
21 | } | |
22 | ||
23 | #[macro_export] | |
24 | macro_rules! task_error { | |
25 | ($task:expr, $($fmt:tt)+) => {{ | |
26 | $crate::task::TaskState::log(&*$task, log::Level::Error, &format_args!($($fmt)+)) | |
27 | }}; | |
28 | } | |
29 | ||
30 | #[macro_export] | |
31 | macro_rules! task_warn { | |
32 | ($task:expr, $($fmt:tt)+) => {{ | |
33 | $crate::task::TaskState::log(&*$task, log::Level::Warn, &format_args!($($fmt)+)) | |
34 | }}; | |
35 | } | |
36 | ||
37 | #[macro_export] | |
38 | macro_rules! task_log { | |
39 | ($task:expr, $($fmt:tt)+) => {{ | |
40 | $crate::task::TaskState::log(&*$task, log::Level::Info, &format_args!($($fmt)+)) | |
41 | }}; | |
42 | } | |
43 | ||
44 | #[macro_export] | |
45 | macro_rules! task_debug { | |
46 | ($task:expr, $($fmt:tt)+) => {{ | |
47 | $crate::task::TaskState::log(&*$task, log::Level::Debug, &format_args!($($fmt)+)) | |
48 | }}; | |
49 | } | |
50 | ||
51 | #[macro_export] | |
52 | macro_rules! task_trace { | |
53 | ($task:expr, $($fmt:tt)+) => {{ | |
54 | $crate::task::TaskState::log(&*$task, log::Level::Trace, &format_args!($($fmt)+)) | |
55 | }}; | |
56 | } |