Postgres persistance + job queue + api with sse #9
@@ -1,5 +1,5 @@
|
||||
use std::collections::{HashMap, VecDeque};
|
||||
use std::sync::{Arc, Mutex, RwLock};
|
||||
use std::sync::{Arc, Mutex};
|
||||
|
||||
use serde::Serialize;
|
||||
use tokio::sync::broadcast;
|
||||
@@ -13,7 +13,7 @@ const MAX_EVENT_ARGUMENTS: usize = 200;
|
||||
|
||||
/// Lifecycle event of a job, broadcast globally.
|
||||
#[derive(Clone, Debug, Serialize)]
|
||||
#[serde(tag = "type", rename_all = "snake_case")]
|
||||
#[serde(rename_all = "snake_case")]
|
||||
pub enum JobEvent {
|
||||
Queued {
|
||||
id: u64,
|
||||
@@ -46,7 +46,7 @@ pub enum JobEvent {
|
||||
}
|
||||
|
||||
#[derive(Clone, Debug, Serialize)]
|
||||
#[serde(tag = "type", rename_all = "snake_case")]
|
||||
#[serde(rename_all = "snake_case")]
|
||||
pub enum Progress {
|
||||
ToolCall {
|
||||
iteration: usize,
|
||||
@@ -98,7 +98,7 @@ pub struct JobSubscription {
|
||||
#[derive(Clone)]
|
||||
pub struct EventBus {
|
||||
lifecycle: broadcast::Sender<JobEvent>,
|
||||
jobs: Arc<RwLock<HashMap<u64, Arc<JobChannel>>>>,
|
||||
jobs: Arc<Mutex<HashMap<u64, Arc<JobChannel>>>>,
|
||||
}
|
||||
|
||||
impl EventBus {
|
||||
@@ -107,7 +107,7 @@ impl EventBus {
|
||||
|
||||
Self {
|
||||
lifecycle,
|
||||
jobs: Arc::new(RwLock::new(HashMap::new())),
|
||||
jobs: Arc::new(Mutex::new(HashMap::new())),
|
||||
}
|
||||
}
|
||||
|
||||
@@ -127,7 +127,7 @@ impl EventBus {
|
||||
});
|
||||
|
||||
self.jobs
|
||||
.write()
|
||||
.lock()
|
||||
.expect("job registry lock poisoned")
|
||||
.insert(id, channel.clone());
|
||||
|
||||
@@ -162,7 +162,7 @@ impl EventBus {
|
||||
|
||||
if let Some(channel) = self
|
||||
.jobs
|
||||
.write()
|
||||
.lock()
|
||||
.expect("job registry lock poisoned")
|
||||
.remove(&id)
|
||||
{
|
||||
@@ -181,7 +181,7 @@ impl EventBus {
|
||||
pub fn subscribe_job(&self, id: u64) -> Option<JobSubscription> {
|
||||
let channel = self
|
||||
.jobs
|
||||
.read()
|
||||
.lock()
|
||||
.expect("job registry lock poisoned")
|
||||
.get(&id)
|
||||
.cloned()?;
|
||||
|
||||
Reference in new issue
Block a user