1use awa_model::JobRow;
2use std::any::Any;
3use std::collections::HashMap;
4use std::sync::atomic::{AtomicBool, Ordering};
5use std::sync::Arc;
6
7pub struct JobContext {
11 pub job: JobRow,
13 cancelled: Arc<AtomicBool>,
15 state: Arc<HashMap<std::any::TypeId, Box<dyn Any + Send + Sync>>>,
17}
18
19impl JobContext {
20 pub fn new(
21 job: JobRow,
22 cancelled: Arc<AtomicBool>,
23 state: Arc<HashMap<std::any::TypeId, Box<dyn Any + Send + Sync>>>,
24 ) -> Self {
25 Self {
26 job,
27 cancelled,
28 state,
29 }
30 }
31
32 pub fn is_cancelled(&self) -> bool {
34 self.cancelled.load(Ordering::SeqCst)
35 }
36
37 pub fn cancellation_flag(&self) -> Arc<AtomicBool> {
39 self.cancelled.clone()
40 }
41
42 pub fn cancel(&self) {
44 self.cancelled.store(true, Ordering::SeqCst);
45 }
46
47 pub fn extract<T: Any + Send + Sync + Clone>(&self) -> Option<T> {
51 self.state
52 .get(&std::any::TypeId::of::<T>())
53 .and_then(|v| v.downcast_ref::<T>())
54 .cloned()
55 }
56}