fn restore(&mut self) {
        let path = Path::new(&self.settings.shared.pueue_directory).join("state.json");

        // 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;
        }
        info!("Start restoring state");

        // 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 the state file.
        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 mut state = deserialized.unwrap();

        // Restore the state from the file.
        // While restoring the tasks, check for any invalid/broken stati.
        for (task_id, task) in state.tasks.iter_mut() {
            // Handle ungraceful shutdowns while executing tasks.
            if task.status == TaskStatus::Running || task.status == TaskStatus::Paused {
                info!(
                    "Setting task {} with previous status {:?} to new status {:?}",
                    task.id,
                    task.status,
                    TaskResult::Killed
                );
                task.status = TaskStatus::Done;
                task.result = Some(TaskResult::Killed);
            }
            // Crash during editing of the task command.
            if task.status == TaskStatus::Locked {
                task.status = TaskStatus::Stashed;
            }
            // If there are any queued tasks, pause the daemon.
            // This should prevent any unwanted execution of tasks due to a system crash.
            if task.status == TaskStatus::Queued {
                info!("Pausing daemon to prevent unwanted execution of previous tasks");
                state.running = false;
            }

            self.tasks.insert(*task_id, task.clone());
        }

        if !self.running {
            // If the daemon isn't running, stop all groups.
            for group in self.settings.daemon.groups.keys() {
                self.groups.insert(group.into(), false);
            }
        } else {
            // If the daemon running, apply all running group states from the previous state.
            for (group, running) in state.groups.iter() {
                if self.groups.contains_key(group) {
                    self.groups.insert(group.into(), *running);
                }
            }
        }

        // Go trough all tasks and set all groups that are no longer
        // listed in the configuration file to the default.
        for (_, task) in self.tasks.iter_mut() {
            if let Some(group) = &task.group {
                if !self.groups.contains_key(group) {
                    task.group = None;
                }
            }
        }

        self.running = state.running;
        self.max_id = state.max_id;
    }
/// Restore the last state from a previous session.
/// The state is stored as json in the log directory.
Restore the last state from a previous session.
The state is stored as json in the log directory.
Restore the last state from a previous session . The state is stored as json in the log directory .
fn restore(&mut self) {
        let path = Path::new(&self.settings.shared.pueue_directory).join("state.json");
        // 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;
        }
        info!("Start restoring state");
        // 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 the state file.
        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 mut state = deserialized.unwrap();
        // Restore the state from the file.
        // While restoring the tasks, check for any invalid/broken stati.
        for (task_id, task) in state.tasks.iter_mut() {
            // Handle ungraceful shutdowns while executing tasks.
            if task.status == TaskStatus::Running || task.status == TaskStatus::Paused {
                info!(
                    "Setting task {} with previous status {:?} to new status {:?}",
                    task.id,
                    task.status,
                    TaskResult::Killed
                );
                task.status = TaskStatus::Done;
                task.result = Some(TaskResult::Killed);
            }
            // Crash during editing of the task command.
            if task.status == TaskStatus::Locked {
                task.status = TaskStatus::Stashed;
            }
            // If there are any queued tasks, pause the daemon.
            // This should prevent any unwanted execution of tasks due to a system crash.
            if task.status == TaskStatus::Queued {
                info!("Pausing daemon to prevent unwanted execution of previous tasks");
                state.running = false;
            }
            self.tasks.insert(*task_id, task.clone());
        }
        if !self.running {
            // If the daemon isn't running, stop all groups.
            for group in self.settings.daemon.groups.keys() {
                self.groups.insert(group.into(), false);
            }
        } else {
            // If the daemon running, apply all running group states from the previous state.
            for (group, running) in state.groups.iter() {
                if self.groups.contains_key(group) {
                    self.groups.insert(group.into(), *running);
                }
            }
        }
        // Go trough all tasks and set all groups that are no longer
        // listed in the configuration file to the default.
        for (_, task) in self.tasks.iter_mut() {
            if let Some(group) = &task.group {
                if !self.groups.contains_key(group) {
                    task.group = None;
                }
            }
        }
        self.running = state.running;
        self.max_id = state.max_id;
    }
fn restore ( & mut self ) { let path = Path :: new ( & self . settings . shared . pueue_directory ) . join ( " " ) ; // Ignore if the file doesn't exist. It doesn't have to. if ! path . exists ( ) { info ! ( " " path ) ; return ; } info ! ( " " ) ; // Try to load the file. let data = fs :: read_to_string ( & path ) ; if let Err ( error ) = data { error ! ( " " error ) ; return ; } let data = data . unwrap ( ) ; // Try to deserialize the state file. let deserialized : Result < State , serde_json :: error :: Error > = serde_json :: from_str ( & data ) ; if let Err ( error ) = deserialized { error ! ( " " error ) ; return ; } let mut state = deserialized . unwrap ( ) ; // Restore the state from the file. // While restoring the tasks, check for any invalid/broken stati. for ( task_id , task ) in state . tasks . iter_mut ( ) { // Handle ungraceful shutdowns while executing tasks. if task . status == TaskStatus :: Running || task . status == TaskStatus :: Paused { info ! ( " " task id task status TaskResult Killed ) ; task . status = TaskStatus :: Done ; task . result = Some ( TaskResult :: Killed ) ; } // Crash during editing of the task command. if task . status == TaskStatus :: Locked { task . status = TaskStatus :: Stashed ; } // If there are any queued tasks, pause the daemon. // This should prevent any unwanted execution of tasks due to a system crash. if task . status == TaskStatus :: Queued { info ! ( " " ) ; state . running = false ; } self . tasks . insert ( * task_id , task . clone ( ) ) ; } if ! self . running { // If the daemon isn't running, stop all groups. for group in self . settings . daemon . groups . keys ( ) { self . groups . insert ( group . into ( ) , false ) ; } } else { // If the daemon running, apply all running group states from the previous state. for ( group , running ) in state . groups . iter ( ) { if self . groups . contains_key ( group ) { self . groups . insert ( group . into ( ) , * running ) ; } } } // Go trough all tasks and set all groups that are no longer // listed in the configuration file to the default. for ( _ , task ) in self . tasks . iter_mut ( ) { if let Some ( group ) = & task . group { if ! self . groups . contains_key ( group ) { task . group = None ; } } } self . running = state . running ; self . max_id = state . max_id ; }
Restore the last state from a previous session.
Restore the last state from a previous session .
&mut self