1
0
Fork 0

Fix task processing

This commit is contained in:
Malte Brandy 2018-07-19 19:56:26 +02:00
parent 8def46a626
commit fb3decdbaa
No known key found for this signature in database
GPG key ID: 226A2D41EF5378C9
2 changed files with 97 additions and 87 deletions

View file

@ -29,10 +29,7 @@ fn is_active(cache: &TaskCache, task: &impl WellKnown) -> bool {
} }
fn update_task(cache: &mut TaskCache, task: &impl WellKnown) -> Result<()> { fn update_task(cache: &mut TaskCache, task: &impl WellKnown) -> Result<()> {
cache.reactivate( cache.reactivate(Some(task.definition()), task.refresh())?;
Some(task.definition().clone()),
task.refresh(),
)?;
check_completion(cache, task) check_completion(cache, task)
} }
@ -40,9 +37,12 @@ pub fn process_task(kassandra: &mut Kassandra, task: &impl WellKnown) -> Result<
update_task(&mut kassandra.cache, task)?; update_task(&mut kassandra.cache, task)?;
if is_active(&mut kassandra.cache, task) { if is_active(&mut kassandra.cache, task) {
kassandra.cache.write()?; kassandra.cache.write()?;
task.process(kassandra)?; if task.process(kassandra)? {
for task in kassandra.cache.filter_mut(|t| task.is_this(t)) {
task.tw_done()
}
}
kassandra.cache.refresh()?; kassandra.cache.refresh()?;
update_task(&mut kassandra.cache, task)?;
} }
kassandra.cache.write()?; kassandra.cache.write()?;
Ok(()) Ok(())

View file

@ -12,15 +12,15 @@ use mail::{mailbox_dirty, SortBox};
use chrono::{NaiveDate, NaiveTime, Duration}; use chrono::{NaiveDate, NaiveTime, Duration};
pub trait WellKnown { pub trait WellKnown {
fn definition(&self) -> &Task; fn definition(&self) -> Task;
fn is_this(&self, task: &Task) -> bool { fn is_this(&self, task: &Task) -> bool {
gen_match(task, self.definition()) gen_match(task, &self.definition())
} }
fn action_necessary(&self, &TaskCache) -> Result<bool> { fn action_necessary(&self, &TaskCache) -> Result<bool> {
Ok(true) Ok(true)
} }
fn process(&self, &mut Kassandra) -> Result<()> { fn process(&self, &mut Kassandra) -> Result<bool> {
Ok(()) Ok(false)
} }
fn refresh(&self) -> Timer; fn refresh(&self) -> Timer;
} }
@ -114,8 +114,8 @@ impl SimpleTask {
impl WellKnown for SimpleTask { impl WellKnown for SimpleTask {
fn definition(&self) -> &Task { fn definition(&self) -> Task {
&self.definition self.definition.clone()
} }
fn refresh(&self) -> Timer { fn refresh(&self) -> Timer {
self.timer.clone() self.timer.clone()
@ -136,11 +136,6 @@ fn simple_tasks(
} }
fn make_simple() -> Vec<SimpleTask> { fn make_simple() -> Vec<SimpleTask> {
let daily = Timer::Repetition(CalendarRepeater {
date: NaiveDate::from_ymd(2018, 5, 8),
time: NaiveTime::from_hms(6, 0, 0),
repeat: Interval::Day(1),
});
let weekly = Timer::Repetition(CalendarRepeater { let weekly = Timer::Repetition(CalendarRepeater {
date: NaiveDate::from_ymd(2018, 5, 8), date: NaiveDate::from_ymd(2018, 5, 8),
time: NaiveTime::from_hms(20, 0, 0), time: NaiveTime::from_hms(20, 0, 0),
@ -166,7 +161,6 @@ fn make_simple() -> Vec<SimpleTask> {
vec!["Friseurtermin machen"], vec!["Friseurtermin machen"],
Timer::DeadTime(Duration::weeks(6)), Timer::DeadTime(Duration::weeks(6)),
)) ))
.chain(simple_tasks(vec!["Klavier üben"], daily))
.chain(simple_tasks(vec!["Verbuche Kontoauszüge"], monthly)) .chain(simple_tasks(vec!["Verbuche Kontoauszüge"], monthly))
.chain(simple_tasks( .chain(simple_tasks(
vec![ vec![
@ -186,9 +180,7 @@ pub struct Inbox {
} }
impl WellKnown for Inbox { impl WellKnown for Inbox {
fn definition(&self) -> &Task { fn definition(&self) -> Task {
lazy_static! {
static ref TASK: Task = {
let mut t = TaskBuilder::default() let mut t = TaskBuilder::default()
.description("Leere Inbox") .description("Leere Inbox")
.build() .build()
@ -196,17 +188,15 @@ impl WellKnown for Inbox {
t.set_gen_name(Some("inbox")); t.set_gen_name(Some("inbox"));
t.set_gen_id(Some("inbox")); t.set_gen_id(Some("inbox"));
t t
};
};
&TASK
} }
fn action_necessary(&self, cache: &TaskCache) -> Result<bool> { fn action_necessary(&self, cache: &TaskCache) -> Result<bool> {
Ok(cache.filter(|t| task_in_inbox(&cache, t)).next().is_some()) Ok(cache.filter(|t| task_in_inbox(&cache, t)).next().is_some())
} }
fn process(&self, kassandra: &mut Kassandra) -> Result<()> { fn process(&self, kassandra: &mut Kassandra) -> Result<bool> {
kassandra.clear_inbox() kassandra.clear_inbox()?;
Ok(true)
} }
fn refresh(&self) -> Timer { fn refresh(&self) -> Timer {
@ -219,9 +209,7 @@ pub struct Accounting {
} }
impl WellKnown for Accounting { impl WellKnown for Accounting {
fn definition(&self) -> &Task { fn definition(&self) -> Task {
lazy_static! {
static ref TASK: Task = {
let mut t = TaskBuilder::default() let mut t = TaskBuilder::default()
.description("Aktualisiere Buchhaltung") .description("Aktualisiere Buchhaltung")
.build() .build()
@ -229,20 +217,17 @@ impl WellKnown for Accounting {
t.set_gen_name(Some("accounting")); t.set_gen_name(Some("accounting"));
t.set_gen_id(Some("accounting")); t.set_gen_id(Some("accounting"));
t t
};
};
&TASK
} }
fn action_necessary(&self, _cache: &TaskCache) -> Result<bool> { fn action_necessary(&self, _cache: &TaskCache) -> Result<bool> {
Ok(true) Ok(true)
} }
fn process(&self, _kassandra: &mut Kassandra) -> Result<()> { fn process(&self, _kassandra: &mut Kassandra) -> Result<bool> {
str2cmd(&term_cmd("sh -c")) str2cmd(&term_cmd("sh -c"))
.arg("jali -l. && task gen_id:accounting done") .arg("jali -l. && task gen_id:accounting done")
.output()?; .output()?;
Ok(()) Ok(false)
} }
fn refresh(&self) -> Timer { fn refresh(&self) -> Timer {
@ -255,9 +240,7 @@ pub struct Treesort {
} }
impl WellKnown for Treesort { impl WellKnown for Treesort {
fn definition(&self) -> &Task { fn definition(&self) -> Task {
lazy_static! {
static ref TASK: Task = {
let mut t = TaskBuilder::default() let mut t = TaskBuilder::default()
.description("Sortiere Tasktree") .description("Sortiere Tasktree")
.build() .build()
@ -265,9 +248,6 @@ impl WellKnown for Treesort {
t.set_gen_name(Some("treesort")); t.set_gen_name(Some("treesort"));
t.set_gen_id(Some("treesort")); t.set_gen_id(Some("treesort"));
t t
};
};
&TASK
} }
fn action_necessary(&self, cache: &TaskCache) -> Result<bool> { fn action_necessary(&self, cache: &TaskCache) -> Result<bool> {
@ -279,8 +259,9 @@ impl WellKnown for Treesort {
) )
} }
fn process(&self, kassandra: &mut Kassandra) -> Result<()> { fn process(&self, kassandra: &mut Kassandra) -> Result<bool> {
kassandra.assure_all_sorted() kassandra.assure_all_sorted()?;
Ok(true)
} }
fn refresh(&self) -> Timer { fn refresh(&self) -> Timer {
@ -314,16 +295,17 @@ impl Mailsort {
} }
impl WellKnown for Mailsort { impl WellKnown for Mailsort {
fn definition(&self) -> &Task { fn definition(&self) -> Task {
&self.task self.task.clone()
} }
fn action_necessary(&self, _cache: &TaskCache) -> Result<bool> { fn action_necessary(&self, _cache: &TaskCache) -> Result<bool> {
mailbox_dirty(&self.mailbox.mailbox) mailbox_dirty(&self.mailbox.mailbox)
} }
fn process(&self, kassandra: &mut Kassandra) -> Result<()> { fn process(&self, kassandra: &mut Kassandra) -> Result<bool> {
kassandra.sort_mailbox(&self.mailbox, true) kassandra.sort_mailbox(&self.mailbox, true)?;
Ok(true)
} }
fn refresh(&self) -> Timer { fn refresh(&self) -> Timer {
@ -336,9 +318,7 @@ pub struct Maintenance {
} }
impl WellKnown for Maintenance { impl WellKnown for Maintenance {
fn definition(&self) -> &Task { fn definition(&self) -> Task {
lazy_static! {
static ref TASK: Task = {
let mut t = TaskBuilder::default() let mut t = TaskBuilder::default()
.description("Run system Maintenance") .description("Run system Maintenance")
.build() .build()
@ -346,22 +326,52 @@ impl WellKnown for Maintenance {
t.set_gen_name(Some("maintenance")); t.set_gen_name(Some("maintenance"));
t.set_gen_id(Some("apollo")); t.set_gen_id(Some("apollo"));
t t
};
};
&TASK
} }
fn action_necessary(&self, _cache: &TaskCache) -> Result<bool> { fn action_necessary(&self, _cache: &TaskCache) -> Result<bool> {
Ok(true) Ok(true)
} }
fn process(&self, kassandra: &mut Kassandra) -> Result<()> { fn process(&self, _kassandra: &mut Kassandra) -> Result<bool> {
str2cmd(&term_cmd("maintenance")).spawn()?; str2cmd("maintenance").spawn()?;
for task in kassandra.cache.filter_mut(|t| self.is_this(t)) { Ok(true)
task.tw_done() }
}
kassandra.cache.write()?; fn refresh(&self) -> Timer {
Ok(()) self.timer.clone()
}
}
pub struct TaskCheck {
timer: Timer,
priority: PriorityState,
}
impl WellKnown for TaskCheck {
fn definition(&self) -> Task {
let mut t = TaskBuilder::default()
.description(format!(
"Check tasks matching \"{:}\"",
if self.priority.0.only_optional() {
"+optional"
} else {
prio_name(self.priority.0.prio().as_ref())
}
))
.build()
.expect("TaskBuilding failed inspite of set description");
t.set_gen_name(Some("task_check"));
t.set_gen_id(Some(format!("{:?}", self.priority)));
t
}
fn action_necessary(&self, cache: &TaskCache) -> Result<bool> {
Ok(get_stale_tasks(cache, self.priority).len() > 0)
}
fn process(&self, kassandra: &mut Kassandra) -> Result<bool> {
kassandra.check_priorities(self.priority)?;
Ok(true)
} }
fn refresh(&self) -> Timer { fn refresh(&self) -> Timer {