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 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162
use std::cmp::{max, min, Ordering}; use std::collections::BinaryHeap; use ic_cdk::export::candid::utils::ArgumentEncoder; use ic_cdk::export::candid::{encode_args, CandidType, Deserialize, Result as CandidResult}; use union_utils::{RemoteCallEndpoint, RemoteCallPayload}; pub type TaskId = u64; #[derive(Clone, CandidType, Deserialize)] pub enum Iterations { Infinite, Exact(u64), } #[derive(Clone, CandidType, Deserialize)] pub struct SchedulingInterval { pub duration_nano: u64, pub iterations: Iterations, } #[derive(Clone, CandidType, Deserialize)] pub struct Task { pub id: TaskId, pub payload: RemoteCallPayload, pub scheduled_at: u64, pub rescheduled_at: Option<u64>, pub scheduling_interval: SchedulingInterval, } impl Task { pub fn new<Tuple: ArgumentEncoder>( id: TaskId, endpoint: RemoteCallEndpoint, args: Tuple, cycles: u64, scheduled_at: u64, rescheduled_at: Option<u64>, scheduling_interval: SchedulingInterval, ) -> CandidResult<Self> { let payload = RemoteCallPayload { endpoint, cycles, args_raw: encode_args(args)?, }; Ok(Self { id, payload, scheduled_at, rescheduled_at, scheduling_interval, }) } } pub struct TaskTimestamp { pub task_id: TaskId, pub timestamp: u64, } impl PartialEq for TaskTimestamp { fn eq(&self, other: &Self) -> bool { self.timestamp.eq(&other.timestamp) && self.task_id.eq(&other.task_id) } } impl Eq for TaskTimestamp {} impl PartialOrd for TaskTimestamp { fn partial_cmp(&self, other: &Self) -> Option<Ordering> { self.timestamp .partial_cmp(&other.timestamp) .map(|it| it.reverse()) } fn lt(&self, other: &Self) -> bool { self.timestamp.gt(&other.timestamp) } fn le(&self, other: &Self) -> bool { self.timestamp.ge(&other.timestamp) } fn gt(&self, other: &Self) -> bool { self.timestamp.lt(&other.timestamp) } fn ge(&self, other: &Self) -> bool { self.timestamp.le(&other.timestamp) } } impl Ord for TaskTimestamp { fn cmp(&self, other: &Self) -> Ordering { self.timestamp.cmp(&other.timestamp).reverse() } fn max(self, other: Self) -> Self where Self: Sized, { max(self, other) } fn min(self, other: Self) -> Self where Self: Sized, { min(self, other) } fn clamp(self, min: Self, max: Self) -> Self where Self: Sized, { if self.timestamp < max.timestamp { max } else if self.timestamp > min.timestamp { min } else { self } } } #[derive(Default)] pub struct TaskExecutionQueue(BinaryHeap<TaskTimestamp>); impl TaskExecutionQueue { pub fn push(&mut self, task: TaskTimestamp) { self.0.push(task); } pub fn pop_ready(&mut self, timestamp: u64) -> Vec<TaskTimestamp> { let mut cur = self.0.peek(); if cur.is_none() { return Vec::new(); } let mut result = vec![]; while cur.unwrap().timestamp <= timestamp { result.push(self.0.pop().unwrap()); cur = self.0.peek(); if cur.is_none() { break; } } result } pub fn is_empty(&self) -> bool { self.0.is_empty() } pub fn len(&self) -> usize { self.0.len() } }