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
//! # Worker Stats
//! What one worker looked like at the moment it was asked
use std::fmt;
/// A snapshot of one worker
///
/// #### Note
/// A snapshot, not a lock. The worker carries on while this is
/// read, so treat the numbers as approximate
#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub struct WorkerStats {
busy: bool,
backlog: usize,
completed: u64,
}
impl WorkerStats {
pub(crate) fn new(busy: bool, backlog: usize, completed: u64) -> Self {
Self {
busy,
backlog,
completed,
}
}
/// Whether the worker was inside a task
pub fn busy(&self) -> bool {
self.busy
}
/// Tasks waiting in the worker's own queue
pub fn backlog(&self) -> usize {
self.backlog
}
/// Tasks this worker has finished since it started
///
/// Reset when the worker is restarted
pub fn completed(&self) -> u64 {
self.completed
}
}
impl fmt::Display for WorkerStats {
/// One worker on one line: busy or idle, its backlog, and what
/// it has finished
fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(
formatter,
"{}, {} queued, {} done",
match self.busy {
true => "busy",
false => "idle",
},
self.backlog,
self.completed,
)
}
}