Show last worker thread heartbeat on status page

memfd
Manos Pitsidianakis 2020-05-08 11:07:10 +03:00
parent c8391983ee
commit d405aa9797
Signed by: Manos Pitsidianakis
GPG Key ID: 73627C2F690DF710
2 changed files with 43 additions and 8 deletions

View File

@ -20,6 +20,7 @@
*/
use super::*;
use std::collections::HashMap;
use std::fmt;
#[derive(Debug)]
@ -27,6 +28,7 @@ pub struct StatusPanel {
cursor: (usize, usize),
account_cursor: usize,
status: Option<AccountStatus>,
date_cache: HashMap<UnixTimestamp, String>,
content: CellBuffer,
dirty: bool,
theme_default: ThemeAttribute,
@ -68,9 +70,10 @@ impl Component for StatusPanel {
for worker in workers {
let (x, y_off) = write_string_to_grid(
&format!(
"- {:<max_name$} {}",
"- {:<max_name$} {} [{}]",
worker.name.as_str(),
worker.status.as_str(),
self.timestamp_fmt(worker.heartbeat),
max_name = max_name
),
&mut self.content,
@ -108,9 +111,10 @@ impl Component for StatusPanel {
for worker in workers {
let (x, y_off) = write_string_to_grid(
&format!(
"- {:<max_name$} {}",
"- {:<max_name$} {} [{}]",
worker.name.as_str(),
worker.status.as_str(),
self.timestamp_fmt(worker.heartbeat),
max_name = max_name
),
&mut self.content,
@ -241,6 +245,7 @@ impl StatusPanel {
account_cursor: 0,
content,
status: None,
date_cache: Default::default(),
dirty: true,
theme_default,
id: ComponentId::new_v4(),
@ -379,6 +384,15 @@ impl StatusPanel {
}
}
}
fn timestamp_fmt(&mut self, t: UnixTimestamp) -> &str {
if !self.date_cache.contains_key(&t) {
self.date_cache.insert(
t,
melib::datetime::timestamp_to_string(t, Some("%Y-%m-%d %T")),
);
}
&self.date_cache[&t]
}
}
impl Component for AccountStatus {

View File

@ -28,6 +28,7 @@ use crossbeam::{
};
use fnv::FnvHashMap;
use melib::async_workers::{Work, WorkContext};
use melib::datetime::{self, UnixTimestamp};
use melib::text_processing::Truncate;
use std::sync::Arc;
use std::sync::Mutex;
@ -41,6 +42,7 @@ const MAX_WORKER: usize = 4;
pub struct Worker {
pub name: String,
pub status: String,
pub heartbeat: UnixTimestamp,
}
impl From<String> for Worker {
@ -48,6 +50,7 @@ impl From<String> for Worker {
Worker {
name: val,
status: String::new(),
heartbeat: datetime::now(),
}
}
}
@ -265,12 +268,23 @@ impl WorkController {
new_name.truncate_at_boundary(256);
let mut threads = threads_lock.lock().unwrap();
let mut static_threads = _static_threads_lock.lock().unwrap();
let now = datetime::now();
if threads.contains_key(&thread_id) {
threads.entry(thread_id).and_modify(|e| e.name = new_name);
threads.entry(thread_id).and_modify(|e| {
e.name = new_name;
e.heartbeat = now;
});
} else if static_threads.contains_key(&thread_id) {
static_threads.entry(thread_id).and_modify(|e| e.name = new_name);
static_threads.entry(thread_id).and_modify(|e| {
e.name = new_name;
e.heartbeat = now;
});
} else {
static_threads.insert(thread_id, new_name.into());
static_threads.insert(thread_id, Worker { heartbeat: now, .. new_name.into() });
static_threads.entry(thread_id).and_modify(|e| {
e.heartbeat = now;
});
}
pulse.send(ThreadEvent::Pulse).unwrap();
}
@ -280,13 +294,20 @@ impl WorkController {
new_status.truncate_at_boundary(256);
let mut threads = threads_lock.lock().unwrap();
let mut static_threads = _static_threads_lock.lock().unwrap();
let now = datetime::now();
if threads.contains_key(&thread_id) {
threads.entry(thread_id).and_modify(|e| e.status = new_status);
threads.entry(thread_id).and_modify(|e| {
e.status = new_status;
e.heartbeat = now;
});
} else if static_threads.contains_key(&thread_id) {
static_threads.entry(thread_id).and_modify(|e| e.status = new_status);
static_threads.entry(thread_id).and_modify(|e| {
e.status = new_status;
e.heartbeat = now;
});
debug!(&static_threads[&thread_id]);
} else {
static_threads.insert(thread_id, Worker { status: new_status, .. String::new().into() });
static_threads.insert(thread_id, Worker { status: new_status, heartbeat: now, .. String::new().into() });
}
pulse.send(ThreadEvent::Pulse).unwrap();
}