refactor: keep unit timestamps as one Activity
This commit is contained in:
1 parent
8d96034574
commit
c11a509090
5 files changed
+25
-34
No files matched your search
+1
-4
@@ -55,10 +55,7 @@ enum Progress {
|
||||
#[derive(Clone, Debug)]
|
||||
struct UnitRecord {
|
||||
facts: UnitFacts,
|
||||
state: ActiveState,
|
||||
inactive_exit: Option<Duration>,
|
||||
active_enter: Option<Duration>,
|
||||
active_exit: Option<Duration>,
|
||||
activity: Activity,
|
||||
progress: Progress,
|
||||
}
|
||||
|
||||
|
||||
@@ -7,25 +7,10 @@ use super::{
|
||||
};
|
||||
|
||||
impl UnitRecord {
|
||||
fn apply(&mut self, activity: Activity) {
|
||||
if let Some(state) = activity.state {
|
||||
self.state = state;
|
||||
}
|
||||
if activity.inactive_exit.is_some() {
|
||||
self.inactive_exit = activity.inactive_exit;
|
||||
}
|
||||
if activity.active_enter.is_some() {
|
||||
self.active_enter = activity.active_enter;
|
||||
}
|
||||
if activity.active_exit.is_some() {
|
||||
self.active_exit = activity.active_exit;
|
||||
}
|
||||
}
|
||||
|
||||
fn transition_started(&self, kind: JobKind) -> Option<Duration> {
|
||||
match kind {
|
||||
JobKind::TakesDown => self.active_exit,
|
||||
JobKind::BringsUp | JobKind::Unknown => self.inactive_exit,
|
||||
JobKind::TakesDown => self.activity.active_exit,
|
||||
JobKind::BringsUp | JobKind::Unknown => self.activity.inactive_exit,
|
||||
}
|
||||
}
|
||||
|
||||
@@ -34,8 +19,8 @@ impl UnitRecord {
|
||||
return None;
|
||||
};
|
||||
let transitioning = matches!(
|
||||
self.state,
|
||||
ActiveState::Activating | ActiveState::Deactivating | ActiveState::Reloading
|
||||
self.activity.state,
|
||||
Some(ActiveState::Activating | ActiveState::Deactivating | ActiveState::Reloading)
|
||||
);
|
||||
transitioning.then(|| self.transition_started(job.kind).unwrap_or(job.queued_at))
|
||||
}
|
||||
@@ -113,19 +98,16 @@ impl BootState {
|
||||
.entry(facts.name.clone())
|
||||
.or_insert_with(|| UnitRecord {
|
||||
facts: facts.clone(),
|
||||
state: ActiveState::Inactive,
|
||||
inactive_exit: None,
|
||||
active_enter: None,
|
||||
active_exit: None,
|
||||
activity: Activity::default(),
|
||||
progress: Progress::Untouched,
|
||||
});
|
||||
record.facts = facts;
|
||||
record.apply(activity);
|
||||
record.activity = record.activity.merge(activity);
|
||||
}
|
||||
|
||||
pub fn update_activity(&mut self, unit: &str, activity: Activity) {
|
||||
if let Some(record) = self.units.get_mut(unit) {
|
||||
record.apply(activity);
|
||||
record.activity = record.activity.merge(activity);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -234,7 +216,8 @@ impl BootState {
|
||||
.handoff_unit
|
||||
.as_deref()
|
||||
.and_then(|unit| self.units.get(unit))
|
||||
.is_some_and(|handoff| handoff.state != ActiveState::Inactive)
|
||||
.and_then(|handoff| handoff.activity.state)
|
||||
.is_some_and(|state| state != ActiveState::Inactive)
|
||||
{
|
||||
return true;
|
||||
}
|
||||
|
||||
@@ -41,9 +41,9 @@ impl BootState {
|
||||
let record = self.units.get(target)?;
|
||||
match record.progress {
|
||||
Progress::Finished(Outcome::Succeeded { at }) => Some(at),
|
||||
Progress::Untouched => record
|
||||
.active_enter
|
||||
.filter(|entered| record.state == ActiveState::Active && *entered >= self.origin),
|
||||
Progress::Untouched => record.activity.active_enter.filter(|entered| {
|
||||
record.activity.state == Some(ActiveState::Active) && *entered >= self.origin
|
||||
}),
|
||||
Progress::Pending(_) | Progress::Finished(Outcome::Failed | Outcome::Abandoned) => None,
|
||||
}
|
||||
}
|
||||
|
||||
@@ -64,6 +64,17 @@ pub struct Activity {
|
||||
pub active_exit: Option<Duration>,
|
||||
}
|
||||
|
||||
impl Activity {
|
||||
pub(super) fn merge(self, newer: Self) -> Self {
|
||||
Self {
|
||||
state: newer.state.or(self.state),
|
||||
inactive_exit: newer.inactive_exit.or(self.inactive_exit),
|
||||
active_enter: newer.active_enter.or(self.active_enter),
|
||||
active_exit: newer.active_exit.or(self.active_exit),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
|
||||
pub enum JobKind {
|
||||
BringsUp,
|
||||
|
||||
@@ -226,7 +226,7 @@ impl BootState {
|
||||
.running_since()
|
||||
.map_or(SiblingRank::Waiting, SiblingRank::RunningSince),
|
||||
Progress::Finished(Outcome::Succeeded { at }) => {
|
||||
SiblingRank::RunningSince(record.inactive_exit.unwrap_or(at))
|
||||
SiblingRank::RunningSince(record.activity.inactive_exit.unwrap_or(at))
|
||||
}
|
||||
Progress::Finished(Outcome::Failed) => SiblingRank::Failed,
|
||||
Progress::Finished(Outcome::Abandoned) | Progress::Untouched => {
|
||||
|
||||
Reference in new issue
Block a user