Private
Public Access
1
0

daemon: reorg: use channels for comms instead of copying daemon arc/mutex

This commit is contained in:
2025-04-27 12:53:45 -07:00
parent ef74df9f28
commit 22554a7644
4 changed files with 124 additions and 78 deletions

View File

@@ -1,11 +1,15 @@
mod settings;
pub mod settings;
use settings::Settings;
use std::sync::mpsc;
use directories::ProjectDirs;
use std::path::PathBuf;
pub mod events;
use events::*;
use anyhow::Result;
use directories::ProjectDirs;
use std::error::Error;
use std::path::PathBuf;
use thiserror::Error;
use tokio::sync::mpsc::{Sender, Receiver};
use kordophone_db::{
database::Database,
@@ -20,19 +24,20 @@ use kordophone::api::{
TokenManagement,
};
pub enum Event {
SyncAllConversations,
}
#[derive(Debug, Error)]
pub enum DaemonError {
#[error("Client Not Configured")]
ClientNotConfigured,
}
pub type DaemonResult<T> = Result<T, Box<dyn Error + Send + Sync>>;
pub struct Daemon {
pub version: String,
pub event_sender: Sender<Event>,
event_receiver: Receiver<Event>,
version: String,
database: Database,
runtime: tokio::runtime::Runtime,
}
impl Daemon {
@@ -44,15 +49,53 @@ impl Daemon {
let database_dir = database_path.parent().unwrap();
std::fs::create_dir_all(database_dir)?;
// Create event channels
let (event_sender, event_receiver) = tokio::sync::mpsc::channel(100);
// Create background task runtime
let runtime = tokio::runtime::Builder::new_multi_thread()
.enable_all()
.build()
.unwrap();
let database = Database::new(&database_path.to_string_lossy())?;
Ok(Self { version: "0.1.0".to_string(), database })
Ok(Self { version: "0.1.0".to_string(), database, event_receiver, event_sender, runtime })
}
pub fn get_conversations(&mut self) -> Vec<Conversation> {
pub async fn run(&mut self) {
while let Some(event) = self.event_receiver.recv().await {
self.handle_event(event).await;
}
}
async fn handle_event(&mut self, event: Event) {
match event {
Event::GetVersion(reply) => {
reply.send(self.version.clone()).unwrap();
},
Event::SyncAllConversations(reply) => {
self.sync_all_conversations().await.unwrap_or_else(|e| {
log::error!("Error handling sync event: {}", e);
});
reply.send(()).unwrap();
},
Event::GetAllConversations(reply) => {
let conversations = self.get_conversations();
reply.send(conversations).unwrap();
},
}
}
fn get_conversations(&mut self) -> Vec<Conversation> {
self.database.with_repository(|r| r.all_conversations().unwrap())
}
pub async fn sync_all_conversations(&mut self) -> Result<()> {
async fn sync_all_conversations(&mut self) -> Result<()> {
tokio::time::sleep(tokio::time::Duration::from_secs(10)).await;
let mut client = self.get_client()
.map_err(|_| DaemonError::ClientNotConfigured)?;
@@ -84,7 +127,7 @@ impl Daemon {
Ok(())
}
pub fn get_settings(&mut self) -> Result<Settings> {
fn get_settings(&mut self) -> Result<Settings> {
let settings = self.database.with_settings(|s|
Settings::from_db(s)
)?;
@@ -92,16 +135,6 @@ impl Daemon {
Ok(settings)
}
pub async fn handle_event(&mut self, event: Event) {
match event {
Event::SyncAllConversations => {
self.sync_all_conversations().await.unwrap_or_else(|e| {
log::error!("Error handling sync event: {}", e);
});
}
}
}
fn get_client(&mut self) -> Result<HTTPAPIClient> {
let settings = self.database.with_settings(|s|
Settings::from_db(s)