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