You are an expert in writing code in many different languages. Your goal is to perform code completion for the following code, keeping in mind the rest of the code and the file meta data.
    Metadata details:
    filename: spawn_task.rs
    path of file: ./repos/pueue/pueue/src/daemon/task_handler
    
    the code of the file until where you have to start completion: use super::*;

use crate::daemon::state_helper::{pause_on_failure, save_state, LockedState};
use crate::ok_or_shutdown;

impl TaskHandler {
    /// See if we can start a new queued task.
    pub fn spawn_new(&mut self) {
        let cloned_state_mutex = self.state.clone();
        let mut state = cloned_state_mutex.lock().unwrap();
        /
    
    pub fn get_next_task_id(&mut self, state: &LockedState) -> Option<usize> {
        // Get all tasks that could theoretically be started right now.
        let mut potential_tasks: Vec<&Task> = state
            .tasks
            .iter()
            .filter(|(_, task)| task.status == TaskStatus::Queued)
            .filter(|(_, task)| {
                // Make sure the task is assigned to an existing group.
                let group = match state.groups.get(&task.group) {
                    Some(group) => group,
                    None => {
                        error!(
                            "Got task with unknown group {}. Please report this!",
                            &task.group
                        );
                        return false;
                    }
                };

                // Let's check if the group is running. If it isn't, simply return false.
                if group.status != GroupStatus::Running {
                    return false;
                }

                // Get the currently running tasks by looking at the actually running processes.
                // They're sorted by group, which makes this quite convenient.
                let running_tasks = match self.children.0.get(&task.group) {
                    Some(children) => children.len(),
                    None => {
                        error!(
                            "Got valid group {}, but no worker pool has been initialized. This is a bug!",
                            &task.group
                        );
                        return false
                    }
                };

                // Make sure there are free slots in the task's group
                running_tasks < group.parallel_tasks
            })
            .filter(|(_, task)| {
                // Check whether all dependencies for this task are fulfilled.
                task.dependencies
                    .iter()
                    .flat_map(|id| state.tasks.get(id))
                    .all(|task| matches!(task.status, TaskStatus::Done(TaskResult::Success)))
            })
            .map(|(_, task)| {task})
            .collect();

        // Order the tasks based on their priortiy and their task id.
        // Tasks with higher priority go first.
        // Tasks with the same priority are ordered by their id in ascending order, meaning that
        // tasks with smaller id will be processed first.
        potential_tasks.sort_by(|a, b| {
            // If they have the same prio, decide the execution order by task_id!
            if a.priority == b.priority {
                return a.id.cmp(&b.id);
            }

            // Otherwise, let the priority decide.
            b.priority.cmp(&a.priority)
        });

        // Return the id of the first task (if one has been found).
        potential_tasks.first().map(|task| task.id)
    }