oximedia-workflow 0.2.0

Comprehensive workflow orchestration engine for OxiMedia
Documentation
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
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
#![allow(dead_code)]
//! Priority-based task scheduling queue.
//!
//! Provides a priority queue that orders workflow tasks by priority level,
//! deadline, and submission order. Supports multi-level priority with
//! starvation prevention through priority aging.

use std::collections::BinaryHeap;

/// Priority level for a queued task.
#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub enum PriorityLevel {
    /// Lowest priority — background tasks.
    Low = 0,
    /// Default priority for normal tasks.
    Normal = 1,
    /// Elevated priority for time-sensitive work.
    High = 2,
    /// Highest priority — critical / emergency tasks.
    Critical = 3,
}

impl PriorityLevel {
    /// Return the numeric weight of this priority level.
    #[must_use]
    pub fn weight(self) -> u32 {
        self as u32
    }

    /// Promote to the next higher level (caps at Critical).
    #[must_use]
    pub fn promote(self) -> Self {
        match self {
            Self::Low => Self::Normal,
            Self::Normal => Self::High,
            Self::High | Self::Critical => Self::Critical,
        }
    }
}

impl std::fmt::Display for PriorityLevel {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        match self {
            Self::Low => write!(f, "low"),
            Self::Normal => write!(f, "normal"),
            Self::High => write!(f, "high"),
            Self::Critical => write!(f, "critical"),
        }
    }
}

/// An entry in the priority queue.
#[derive(Debug, Clone)]
pub struct PriorityEntry {
    /// Task identifier.
    pub task_id: String,
    /// Current effective priority.
    pub priority: PriorityLevel,
    /// Original priority before any aging.
    pub base_priority: PriorityLevel,
    /// Optional deadline (seconds since epoch; 0 = none).
    pub deadline_secs: u64,
    /// Submission timestamp (seconds since epoch).
    pub submitted_secs: u64,
    /// Internal insertion order for tie-breaking.
    insertion_order: u64,
    /// Number of times this entry has been aged.
    pub age_count: u32,
}

impl PriorityEntry {
    /// Create a new priority entry.
    pub fn new(
        task_id: impl Into<String>,
        priority: PriorityLevel,
        submitted_secs: u64,
        insertion_order: u64,
    ) -> Self {
        Self {
            task_id: task_id.into(),
            priority,
            base_priority: priority,
            deadline_secs: 0,
            submitted_secs,
            insertion_order,
            age_count: 0,
        }
    }

    /// Set a deadline.
    #[must_use]
    pub fn with_deadline(mut self, deadline_secs: u64) -> Self {
        self.deadline_secs = deadline_secs;
        self
    }

    /// Return true if this entry has a deadline.
    #[must_use]
    pub fn has_deadline(&self) -> bool {
        self.deadline_secs > 0
    }

    /// Check whether the deadline has passed relative to `now_secs`.
    #[must_use]
    pub fn is_overdue(&self, now_secs: u64) -> bool {
        self.has_deadline() && now_secs > self.deadline_secs
    }

    /// Return the time this entry has been waiting (in seconds).
    #[must_use]
    pub fn wait_time(&self, now_secs: u64) -> u64 {
        now_secs.saturating_sub(self.submitted_secs)
    }
}

// Ordering: higher priority first, then earlier deadline, then earlier submission
impl PartialEq for PriorityEntry {
    fn eq(&self, other: &Self) -> bool {
        self.task_id == other.task_id
    }
}

impl Eq for PriorityEntry {}

impl PartialOrd for PriorityEntry {
    fn partial_cmp(&self, other: &Self) -> Option<std::cmp::Ordering> {
        Some(self.cmp(other))
    }
}

impl Ord for PriorityEntry {
    fn cmp(&self, other: &Self) -> std::cmp::Ordering {
        // Higher priority first
        self.priority
            .cmp(&other.priority)
            // Then earlier deadline first (reverse: smaller is better)
            .then_with(|| {
                if self.deadline_secs == 0 && other.deadline_secs == 0 {
                    std::cmp::Ordering::Equal
                } else if self.deadline_secs == 0 {
                    std::cmp::Ordering::Less // no deadline => lower urgency
                } else if other.deadline_secs == 0 {
                    std::cmp::Ordering::Greater
                } else {
                    other.deadline_secs.cmp(&self.deadline_secs) // smaller deadline is more urgent
                }
            })
            // Then earlier submission first (FIFO for ties)
            .then_with(|| other.submitted_secs.cmp(&self.submitted_secs))
            // Final tie-break: earlier insertion wins
            .then_with(|| other.insertion_order.cmp(&self.insertion_order))
    }
}

/// A priority queue for scheduling workflow tasks.
#[derive(Debug)]
pub struct TaskPriorityQueue {
    /// The underlying binary heap.
    heap: BinaryHeap<PriorityEntry>,
    /// Counter for insertion ordering.
    insertion_counter: u64,
    /// How many seconds of wait time before priority is promoted.
    aging_threshold_secs: u64,
}

impl TaskPriorityQueue {
    /// Create a new empty priority queue.
    #[must_use]
    pub fn new() -> Self {
        Self {
            heap: BinaryHeap::new(),
            insertion_counter: 0,
            aging_threshold_secs: 300, // 5 minutes default
        }
    }

    /// Set the aging threshold (seconds of waiting before priority promotion).
    #[must_use]
    pub fn with_aging_threshold(mut self, secs: u64) -> Self {
        self.aging_threshold_secs = secs;
        self
    }

    /// Enqueue a task with the given priority and submission time.
    pub fn enqueue(
        &mut self,
        task_id: impl Into<String>,
        priority: PriorityLevel,
        submitted_secs: u64,
    ) -> u64 {
        let order = self.insertion_counter;
        self.insertion_counter += 1;
        let entry = PriorityEntry::new(task_id, priority, submitted_secs, order);
        self.heap.push(entry);
        order
    }

    /// Enqueue a task with a deadline.
    pub fn enqueue_with_deadline(
        &mut self,
        task_id: impl Into<String>,
        priority: PriorityLevel,
        submitted_secs: u64,
        deadline_secs: u64,
    ) -> u64 {
        let order = self.insertion_counter;
        self.insertion_counter += 1;
        let entry = PriorityEntry::new(task_id, priority, submitted_secs, order)
            .with_deadline(deadline_secs);
        self.heap.push(entry);
        order
    }

    /// Dequeue the highest-priority task.
    pub fn dequeue(&mut self) -> Option<PriorityEntry> {
        self.heap.pop()
    }

    /// Peek at the highest-priority task without removing it.
    #[must_use]
    pub fn peek(&self) -> Option<&PriorityEntry> {
        self.heap.peek()
    }

    /// Return the number of enqueued tasks.
    #[must_use]
    pub fn len(&self) -> usize {
        self.heap.len()
    }

    /// Check if the queue is empty.
    #[must_use]
    pub fn is_empty(&self) -> bool {
        self.heap.is_empty()
    }

    /// Apply priority aging: promote tasks that have been waiting longer than the threshold.
    ///
    /// This drains and rebuilds the heap, so call sparingly.
    pub fn apply_aging(&mut self, now_secs: u64) {
        let threshold = self.aging_threshold_secs;
        let entries: Vec<PriorityEntry> = self.heap.drain().collect();
        for mut entry in entries {
            if entry.wait_time(now_secs) > threshold * (u64::from(entry.age_count) + 1) {
                entry.priority = entry.priority.promote();
                entry.age_count += 1;
            }
            self.heap.push(entry);
        }
    }

    /// Drain all overdue tasks (past their deadline).
    pub fn drain_overdue(&mut self, now_secs: u64) -> Vec<PriorityEntry> {
        let mut overdue = Vec::new();
        let mut remaining = Vec::new();
        while let Some(entry) = self.heap.pop() {
            if entry.is_overdue(now_secs) {
                overdue.push(entry);
            } else {
                remaining.push(entry);
            }
        }
        for e in remaining {
            self.heap.push(e);
        }
        overdue
    }

    /// Clear all tasks from the queue.
    pub fn clear(&mut self) {
        self.heap.clear();
    }

    /// Count tasks at each priority level.
    #[must_use]
    pub fn count_by_priority(&self) -> [usize; 4] {
        let mut counts = [0_usize; 4];
        for entry in &self.heap {
            let idx = entry.priority.weight() as usize;
            if idx < 4 {
                counts[idx] += 1;
            }
        }
        counts
    }
}

impl Default for TaskPriorityQueue {
    fn default() -> Self {
        Self::new()
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn test_priority_level_ordering() {
        assert!(PriorityLevel::Critical > PriorityLevel::High);
        assert!(PriorityLevel::High > PriorityLevel::Normal);
        assert!(PriorityLevel::Normal > PriorityLevel::Low);
    }

    #[test]
    fn test_priority_level_promote() {
        assert_eq!(PriorityLevel::Low.promote(), PriorityLevel::Normal);
        assert_eq!(PriorityLevel::Normal.promote(), PriorityLevel::High);
        assert_eq!(PriorityLevel::High.promote(), PriorityLevel::Critical);
        assert_eq!(PriorityLevel::Critical.promote(), PriorityLevel::Critical);
    }

    #[test]
    fn test_priority_level_display() {
        assert_eq!(format!("{}", PriorityLevel::Low), "low");
        assert_eq!(format!("{}", PriorityLevel::Normal), "normal");
        assert_eq!(format!("{}", PriorityLevel::High), "high");
        assert_eq!(format!("{}", PriorityLevel::Critical), "critical");
    }

    #[test]
    fn test_priority_entry_deadline() {
        let entry = PriorityEntry::new("t1", PriorityLevel::Normal, 1000, 0).with_deadline(2000);
        assert!(entry.has_deadline());
        assert!(!entry.is_overdue(1500));
        assert!(entry.is_overdue(2500));
    }

    #[test]
    fn test_priority_entry_no_deadline() {
        let entry = PriorityEntry::new("t1", PriorityLevel::Normal, 1000, 0);
        assert!(!entry.has_deadline());
        assert!(!entry.is_overdue(9999));
    }

    #[test]
    fn test_priority_entry_wait_time() {
        let entry = PriorityEntry::new("t1", PriorityLevel::Normal, 1000, 0);
        assert_eq!(entry.wait_time(1500), 500);
        assert_eq!(entry.wait_time(500), 0); // saturating
    }

    #[test]
    fn test_queue_enqueue_dequeue_priority() {
        let mut q = TaskPriorityQueue::new();
        q.enqueue("low", PriorityLevel::Low, 1000);
        q.enqueue("critical", PriorityLevel::Critical, 1000);
        q.enqueue("normal", PriorityLevel::Normal, 1000);

        let first = q.dequeue().expect("should succeed in test");
        assert_eq!(first.task_id, "critical");
        let second = q.dequeue().expect("should succeed in test");
        assert_eq!(second.task_id, "normal");
        let third = q.dequeue().expect("should succeed in test");
        assert_eq!(third.task_id, "low");
    }

    #[test]
    fn test_queue_fifo_within_same_priority() {
        let mut q = TaskPriorityQueue::new();
        q.enqueue("a", PriorityLevel::Normal, 1000);
        q.enqueue("b", PriorityLevel::Normal, 1000);
        q.enqueue("c", PriorityLevel::Normal, 1000);

        assert_eq!(q.dequeue().expect("should succeed in test").task_id, "a");
        assert_eq!(q.dequeue().expect("should succeed in test").task_id, "b");
        assert_eq!(q.dequeue().expect("should succeed in test").task_id, "c");
    }

    #[test]
    fn test_queue_deadline_ordering() {
        let mut q = TaskPriorityQueue::new();
        q.enqueue_with_deadline("far", PriorityLevel::Normal, 1000, 5000);
        q.enqueue_with_deadline("near", PriorityLevel::Normal, 1000, 2000);

        // Nearer deadline should come first
        assert_eq!(q.dequeue().expect("should succeed in test").task_id, "near");
        assert_eq!(q.dequeue().expect("should succeed in test").task_id, "far");
    }

    #[test]
    fn test_queue_len_and_empty() {
        let mut q = TaskPriorityQueue::new();
        assert!(q.is_empty());
        assert_eq!(q.len(), 0);
        q.enqueue("a", PriorityLevel::Normal, 1000);
        assert!(!q.is_empty());
        assert_eq!(q.len(), 1);
    }

    #[test]
    fn test_queue_peek() {
        let mut q = TaskPriorityQueue::new();
        assert!(q.peek().is_none());
        q.enqueue("a", PriorityLevel::High, 1000);
        q.enqueue("b", PriorityLevel::Low, 1000);
        assert_eq!(q.peek().expect("should succeed in test").task_id, "a");
        assert_eq!(q.len(), 2); // peek doesn't remove
    }

    #[test]
    fn test_queue_apply_aging() {
        let mut q = TaskPriorityQueue::new().with_aging_threshold(100);
        q.enqueue("old", PriorityLevel::Low, 0);
        q.enqueue("new", PriorityLevel::Low, 900);

        // At time 200, "old" has waited 200s > threshold 100, should promote
        q.apply_aging(200);

        let first = q.dequeue().expect("should succeed in test");
        assert_eq!(first.task_id, "old");
        assert_eq!(first.priority, PriorityLevel::Normal);
        assert_eq!(first.age_count, 1);
    }

    #[test]
    fn test_queue_drain_overdue() {
        let mut q = TaskPriorityQueue::new();
        q.enqueue_with_deadline("overdue", PriorityLevel::Normal, 1000, 1500);
        q.enqueue_with_deadline("ok", PriorityLevel::Normal, 1000, 3000);
        q.enqueue("no-deadline", PriorityLevel::Normal, 1000);

        let overdue = q.drain_overdue(2000);
        assert_eq!(overdue.len(), 1);
        assert_eq!(overdue[0].task_id, "overdue");
        assert_eq!(q.len(), 2);
    }

    #[test]
    fn test_queue_clear() {
        let mut q = TaskPriorityQueue::new();
        q.enqueue("a", PriorityLevel::Normal, 1000);
        q.enqueue("b", PriorityLevel::High, 1000);
        q.clear();
        assert!(q.is_empty());
    }

    #[test]
    fn test_queue_count_by_priority() {
        let mut q = TaskPriorityQueue::new();
        q.enqueue("a", PriorityLevel::Low, 0);
        q.enqueue("b", PriorityLevel::Normal, 0);
        q.enqueue("c", PriorityLevel::Normal, 0);
        q.enqueue("d", PriorityLevel::Critical, 0);

        let counts = q.count_by_priority();
        assert_eq!(counts[PriorityLevel::Low.weight() as usize], 1);
        assert_eq!(counts[PriorityLevel::Normal.weight() as usize], 2);
        assert_eq!(counts[PriorityLevel::High.weight() as usize], 0);
        assert_eq!(counts[PriorityLevel::Critical.weight() as usize], 1);
    }
}