2019-11-11 14:59:01 +00:00
|
|
|
use ::std::collections::BTreeMap;
|
2019-11-13 16:29:36 +00:00
|
|
|
use ::std::sync::{Arc, Mutex};
|
2019-11-30 15:59:42 +00:00
|
|
|
use ::std::fs;
|
2019-12-11 23:24:04 +00:00
|
|
|
use ::std::path::{Path, PathBuf};
|
2019-11-30 15:59:42 +00:00
|
|
|
use ::chrono::prelude::*;
|
|
|
|
use ::serde_derive::{Deserialize, Serialize};
|
2019-11-21 14:10:24 +00:00
|
|
|
use ::strum::IntoEnumIterator;
|
2019-11-30 15:59:42 +00:00
|
|
|
use ::log::{info, error};
|
2019-11-11 14:59:01 +00:00
|
|
|
|
2019-11-15 15:38:56 +00:00
|
|
|
use crate::task::{Task, TaskStatus};
|
2019-11-30 15:59:42 +00:00
|
|
|
use crate::settings::Settings;
|
2019-11-11 14:59:01 +00:00
|
|
|
|
|
|
|
pub type SharedState = Arc<Mutex<State>>;
|
|
|
|
|
2019-11-14 00:26:30 +00:00
|
|
|
#[derive(Serialize, Deserialize, Clone, Debug)]
|
2019-11-11 14:59:01 +00:00
|
|
|
pub struct State {
|
|
|
|
max_id: i32,
|
2019-11-30 15:59:42 +00:00
|
|
|
settings: Settings,
|
2019-11-27 21:59:12 +00:00
|
|
|
pub running: bool,
|
2019-11-15 20:01:30 +00:00
|
|
|
pub tasks: BTreeMap<i32, Task>,
|
2019-11-11 14:59:01 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
impl State {
|
2019-11-30 15:59:42 +00:00
|
|
|
pub fn new(settings: &Settings) -> State {
|
2019-11-11 22:02:13 +00:00
|
|
|
return State {
|
|
|
|
max_id: 0,
|
2019-11-30 15:59:42 +00:00
|
|
|
settings: settings.clone(),
|
2019-11-27 21:59:12 +00:00
|
|
|
running: true,
|
2019-11-13 23:15:46 +00:00
|
|
|
tasks: BTreeMap::new(),
|
2019-11-13 16:29:36 +00:00
|
|
|
};
|
2019-11-11 22:02:13 +00:00
|
|
|
}
|
|
|
|
|
2019-11-21 00:14:32 +00:00
|
|
|
pub fn add_task(&mut self, mut task: Task) -> i32 {
|
2019-11-13 16:29:26 +00:00
|
|
|
task.id = self.max_id;
|
2019-11-13 23:15:46 +00:00
|
|
|
self.tasks.insert(self.max_id, task);
|
2019-11-11 14:59:01 +00:00
|
|
|
self.max_id += 1;
|
2019-11-30 15:59:42 +00:00
|
|
|
self.save();
|
2019-11-21 00:14:32 +00:00
|
|
|
self.max_id - 1
|
2019-11-11 14:59:01 +00:00
|
|
|
}
|
|
|
|
|
2019-11-13 23:15:46 +00:00
|
|
|
pub fn remove_task(&mut self, id: i32) -> Option<Task> {
|
|
|
|
self.tasks.remove(&id)
|
|
|
|
}
|
|
|
|
|
2019-11-15 02:06:41 +00:00
|
|
|
pub fn get_task_clone(&mut self, id: i32) -> Option<Task> {
|
|
|
|
let task = self.tasks.remove(&id);
|
|
|
|
let clone = task.clone();
|
|
|
|
if let Some(task) = task {
|
|
|
|
self.tasks.insert(id, task);
|
|
|
|
}
|
|
|
|
|
|
|
|
return clone;
|
|
|
|
}
|
|
|
|
|
2019-11-15 20:01:30 +00:00
|
|
|
pub fn get_next_task(&mut self) -> Option<i32> {
|
2019-11-13 23:15:46 +00:00
|
|
|
for (id, task) in self.tasks.iter() {
|
2019-11-25 00:49:19 +00:00
|
|
|
if task.status == TaskStatus::Queued {
|
|
|
|
return Some(*id);
|
2019-11-11 14:59:01 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
None
|
|
|
|
}
|
|
|
|
|
2019-11-17 23:44:08 +00:00
|
|
|
pub fn change_status(&mut self, id: i32, new_status: TaskStatus) {
|
2019-11-13 23:15:46 +00:00
|
|
|
if let Some(ref mut task) = self.tasks.get_mut(&id) {
|
2019-11-17 23:44:08 +00:00
|
|
|
if new_status == TaskStatus::Running {
|
|
|
|
if TaskStatus::Queued == task.status || TaskStatus::Stashed == task.status {
|
|
|
|
task.start = Some(Local::now());
|
2019-11-15 20:01:30 +00:00
|
|
|
}
|
|
|
|
}
|
2019-11-17 23:44:08 +00:00
|
|
|
task.status = new_status;
|
2019-11-30 15:59:42 +00:00
|
|
|
self.save();
|
2019-11-11 14:59:01 +00:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2019-11-22 14:51:51 +00:00
|
|
|
pub fn add_error_message(&mut self, id: i32, message: String) {
|
|
|
|
if let Some(ref mut task) = self.tasks.get_mut(&id) {
|
|
|
|
task.stderr = Some(message);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-11-13 23:15:46 +00:00
|
|
|
pub fn get_task_status(&mut self, id: i32) -> Option<TaskStatus> {
|
|
|
|
if let Some(ref task) = self.tasks.get(&id) {
|
2019-11-11 14:59:01 +00:00
|
|
|
return Some(task.status.clone());
|
|
|
|
};
|
|
|
|
None
|
|
|
|
}
|
|
|
|
|
2019-11-21 14:10:24 +00:00
|
|
|
/// This checks, whether the given task_ids are in the specified statuses.
|
|
|
|
/// The first result is the list of task_ids that match these statuses.
|
|
|
|
/// The second result is the list of task_ids that don't match these statuses.
|
2019-11-21 00:14:32 +00:00
|
|
|
///
|
|
|
|
/// Additionally, if no task_ids are specified, return ids of all tasks
|
2019-11-21 14:11:35 +00:00
|
|
|
pub fn tasks_in_statuses(
|
|
|
|
&mut self,
|
|
|
|
task_ids: Option<Vec<i32>>,
|
|
|
|
statuses: Vec<TaskStatus>,
|
|
|
|
) -> (Vec<i32>, Vec<i32>) {
|
2019-11-21 00:14:32 +00:00
|
|
|
let task_ids = match task_ids {
|
|
|
|
Some(ids) => ids,
|
|
|
|
None => self.tasks.keys().cloned().collect(),
|
|
|
|
};
|
|
|
|
|
|
|
|
let mut matching = Vec::new();
|
|
|
|
let mut mismatching = Vec::new();
|
|
|
|
|
2019-11-21 14:10:24 +00:00
|
|
|
// Filter all task id's that match the provided statuses.
|
2019-11-21 00:14:32 +00:00
|
|
|
for task_id in task_ids.iter() {
|
|
|
|
// We aren't interested in this task, continue
|
|
|
|
if !self.tasks.contains_key(&task_id) {
|
|
|
|
mismatching.push(*task_id);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Unwrap, since we just checked, whether it exists.
|
2019-11-21 00:14:52 +00:00
|
|
|
let task = self.tasks.get(&task_id).unwrap();
|
2019-11-21 00:14:32 +00:00
|
|
|
|
2019-11-21 14:10:24 +00:00
|
|
|
if statuses.contains(&task.status) {
|
2019-11-21 00:14:32 +00:00
|
|
|
matching.push(*task_id);
|
|
|
|
} else {
|
|
|
|
mismatching.push(*task_id);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
(matching, mismatching)
|
|
|
|
}
|
2019-11-21 14:10:24 +00:00
|
|
|
|
|
|
|
/// The same as tasks_in_statuses, but with inverted statuses
|
2019-11-21 14:11:35 +00:00
|
|
|
pub fn tasks_not_in_statuses(
|
|
|
|
&mut self,
|
|
|
|
task_ids: Option<Vec<i32>>,
|
|
|
|
excluded_statuses: Vec<TaskStatus>,
|
|
|
|
) -> (Vec<i32>, Vec<i32>) {
|
2019-11-21 14:10:24 +00:00
|
|
|
let mut valid_statuses = Vec::new();
|
|
|
|
// Create a list of all valid statuses
|
|
|
|
// (statuses that aren't the exl
|
|
|
|
for status in TaskStatus::iter() {
|
|
|
|
if !excluded_statuses.contains(&status) {
|
|
|
|
valid_statuses.push(status);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
self.tasks_in_statuses(task_ids, valid_statuses)
|
|
|
|
}
|
2019-11-22 14:30:47 +00:00
|
|
|
|
2019-12-11 22:34:34 +00:00
|
|
|
pub fn clean(&mut self) {
|
2019-12-11 23:24:04 +00:00
|
|
|
self.backup();
|
2019-12-11 22:34:34 +00:00
|
|
|
let statuses = vec![TaskStatus::Done, TaskStatus::Failed];
|
|
|
|
let (matching, _) = self.tasks_in_statuses(None, statuses);
|
|
|
|
|
|
|
|
for task_id in &matching {
|
|
|
|
let _ = self.tasks.remove(task_id).unwrap();
|
|
|
|
}
|
|
|
|
|
|
|
|
self.save();
|
|
|
|
}
|
|
|
|
|
2019-11-25 00:49:31 +00:00
|
|
|
pub fn reset(&mut self) {
|
2019-12-11 23:24:04 +00:00
|
|
|
self.backup();
|
2019-11-22 14:30:47 +00:00
|
|
|
self.max_id = 0;
|
|
|
|
self.tasks = BTreeMap::new();
|
2019-11-30 15:59:42 +00:00
|
|
|
self.save();
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Save the current state to disk.
|
|
|
|
/// We do this to restore in case of a crash
|
|
|
|
pub fn save(&mut self) {
|
2019-12-11 23:24:04 +00:00
|
|
|
self.save_to_file(false);
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Save the current current state in a file with a timestamp
|
|
|
|
/// At the same time remove old state logs from the log directory
|
|
|
|
/// This function is called, when large changes to the state are applied, e.g. clean/reset
|
|
|
|
pub fn backup(&mut self) {
|
|
|
|
self.save_to_file(true);
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn save_to_file(&mut self, log: bool) {
|
2019-11-30 15:59:42 +00:00
|
|
|
let serialized = serde_json::to_string(&self);
|
|
|
|
if let Err(error) = serialized {
|
|
|
|
error!("Failed to serialize state: {:?}", error);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
let serialized = serialized.unwrap();
|
|
|
|
|
2019-12-11 23:24:04 +00:00
|
|
|
let path = Path::new(&self.settings.daemon.pueue_directory);
|
|
|
|
let temp: PathBuf;
|
|
|
|
let real: PathBuf;
|
|
|
|
if log {
|
|
|
|
temp = path.join(format!("backup.json.partial"));
|
|
|
|
real = path.join(format!("state.json", ));
|
|
|
|
} else {
|
|
|
|
temp = path.join("state.json.partial");
|
|
|
|
real = path.join("state.json");
|
|
|
|
}
|
|
|
|
|
2019-11-30 15:59:42 +00:00
|
|
|
|
|
|
|
// Write to temporary log file first, to prevent loss due to crashes
|
|
|
|
if let Err(error) = fs::write(&temp, serialized) {
|
|
|
|
error!("Failed to write log to directory. File permissions? Error: {:?}", error);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Overwrite the original with the temp file, if everything went fine
|
|
|
|
if let Err(error) = fs::rename(&temp, real) {
|
|
|
|
error!("Failed to overwrite old log file. File permissions? Error: {:?}", error);
|
|
|
|
return
|
|
|
|
}
|
2019-11-22 14:30:47 +00:00
|
|
|
}
|
2019-11-30 15:59:42 +00:00
|
|
|
|
|
|
|
|
|
|
|
/// Restore the last state from a previous session
|
|
|
|
/// The state is stored as json in the log directory
|
|
|
|
pub fn restore(&mut self) {
|
2019-12-11 23:24:04 +00:00
|
|
|
let path = Path::new(&self.settings.daemon.pueue_directory).join("state.json");
|
2019-11-30 15:59:42 +00:00
|
|
|
|
|
|
|
// Ignore if the file doesn't exist. It doesn't have to.
|
|
|
|
if !path.exists() {
|
|
|
|
info!("Couldn't find state from previous session at location: {:?}", path);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Try to load the file
|
|
|
|
let data = fs::read_to_string(&path);
|
|
|
|
if let Err(error) = data {
|
|
|
|
error!("Failed to read previous state log: {:?}", error);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
let data = data.unwrap();
|
|
|
|
|
|
|
|
// Try to deserialize it into a state
|
|
|
|
let deserialized: Result<State, serde_json::error::Error> = serde_json::from_str(&data);
|
|
|
|
if let Err(error) = deserialized {
|
|
|
|
error!("Failed to deserialize previous state log: {:?}", error);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
let state = deserialized.unwrap();
|
|
|
|
|
|
|
|
// Actually restore it from the deserialized json
|
|
|
|
for (task_id, task) in state.tasks.iter() {
|
|
|
|
self.tasks.insert(*task_id, task.clone());
|
|
|
|
}
|
|
|
|
|
|
|
|
self.running = state.running;
|
|
|
|
self.max_id = state.max_id;
|
|
|
|
}
|
|
|
|
|
2019-11-11 14:59:01 +00:00
|
|
|
}
|