change: Get rid of the task_handler thread

This commit is contained in:
Arne Beer 2024-06-23 13:42:27 +02:00
parent aa229b33f2
commit 9d7f2675ce
No known key found for this signature in database
GPG key ID: CC9408F679023B65
2 changed files with 10 additions and 10 deletions

View file

@ -5,6 +5,7 @@ use std::{fs::create_dir_all, path::PathBuf};
use anyhow::{bail, Context, Result};
use log::warn;
use network::message_handler;
use process_handler::initiate_shutdown;
use pueue_lib::error::Error;
use pueue_lib::network::certificate::create_certificates;
@ -13,6 +14,7 @@ use pueue_lib::network::protocol::socket_cleanup;
use pueue_lib::network::secret::init_shared_secret;
use pueue_lib::settings::Settings;
use pueue_lib::state::{SharedState, State};
use tokio::try_join;
use self::state_helper::{restore_state, save_state};
use crate::daemon::network::socket::accept_incoming;
@ -88,13 +90,11 @@ pub async fn run(config_path: Option<PathBuf>, profile: Option<String>, test: bo
setup_signal_panic_handling(&settings, state.clone())?;
}
std::thread::spawn(move || {
task_handler.run();
});
accept_incoming(settings.clone(), state.clone()).await?;
Ok(())
// Run both the task handler and the message handler in the same tokio task.
// If any of them fails, return an error immediately.
let task_handler = task_handler.run();
let message_handler = accept_incoming(settings.clone(), state.clone());
try_join!(task_handler, message_handler).map(|_| ())
}
/// Initialize all directories needed for normal operation.

View file

@ -1,7 +1,7 @@
use std::collections::BTreeMap;
use std::thread;
use std::time::Duration;
use anyhow::Result;
use chrono::prelude::*;
use log::{error, info};
@ -84,7 +84,7 @@ impl TaskHandler {
/// This first step waits for 200ms while receiving new messages.
/// This prevents this loop from running hot, but also means that we only check if a new task
/// can be scheduled or if tasks are finished, every 200ms.
pub fn run(&mut self) {
pub async fn run(&mut self) -> Result<()> {
loop {
{
let state_clone = self.state.clone();
@ -111,7 +111,7 @@ impl TaskHandler {
// In normal operation, the task handler thread can sleep a bit longer as it
// doesn't do any time critical tasks.
thread::sleep(Duration::from_millis(300));
tokio::time::sleep(Duration::from_millis(300)).await;
}
}
}