fix: fix subscriptions api
This commit is contained in:
@@ -1,19 +1,22 @@
|
||||
use std::{ops::Deref, sync::Arc};
|
||||
|
||||
use apalis::prelude::*;
|
||||
use apalis_sql::{Config, postgres::PostgresStorage};
|
||||
use apalis_sql::{
|
||||
Config,
|
||||
postgres::{PgListen, PostgresStorage},
|
||||
};
|
||||
use tokio::sync::RwLock;
|
||||
|
||||
use crate::{
|
||||
app::AppContextTrait,
|
||||
errors::RecorderResult,
|
||||
task::{SUBSCRIBER_TASK_APALIS_NAME, SubscriberTask, SubscriberTaskPayload, TaskConfig},
|
||||
task::{SUBSCRIBER_TASK_APALIS_NAME, SubscriberTask, TaskConfig},
|
||||
};
|
||||
|
||||
pub struct TaskService {
|
||||
pub config: TaskConfig,
|
||||
ctx: Arc<dyn AppContextTrait>,
|
||||
pub subscriber_task_storage: Arc<RwLock<PostgresStorage<SubscriberTask>>>,
|
||||
subscriber_task_storage: Arc<RwLock<PostgresStorage<SubscriberTask>>>,
|
||||
}
|
||||
|
||||
impl TaskService {
|
||||
@@ -23,15 +26,12 @@ impl TaskService {
|
||||
) -> RecorderResult<Self> {
|
||||
let pool = ctx.db().get_postgres_connection_pool().clone();
|
||||
let storage_config = Config::new(SUBSCRIBER_TASK_APALIS_NAME);
|
||||
let subscriber_task_storage = Arc::new(RwLock::new(PostgresStorage::new_with_config(
|
||||
pool,
|
||||
storage_config,
|
||||
)));
|
||||
let subscriber_task_storage = PostgresStorage::new_with_config(pool, storage_config);
|
||||
|
||||
Ok(Self {
|
||||
config,
|
||||
ctx,
|
||||
subscriber_task_storage,
|
||||
subscriber_task_storage: Arc::new(RwLock::new(subscriber_task_storage)),
|
||||
})
|
||||
}
|
||||
|
||||
@@ -41,19 +41,14 @@ impl TaskService {
|
||||
) -> RecorderResult<()> {
|
||||
let ctx = data.deref().clone();
|
||||
|
||||
job.payload.run(ctx).await
|
||||
job.run(ctx).await
|
||||
}
|
||||
|
||||
pub async fn add_subscriber_task(
|
||||
&self,
|
||||
subscriber_id: i32,
|
||||
task_payload: SubscriberTaskPayload,
|
||||
_subscriber_id: i32,
|
||||
subscriber_task: SubscriberTask,
|
||||
) -> RecorderResult<TaskId> {
|
||||
let subscriber_task = SubscriberTask {
|
||||
subscriber_id,
|
||||
payload: task_payload,
|
||||
};
|
||||
|
||||
let task_id = {
|
||||
let mut storage = self.subscriber_task_storage.write().await;
|
||||
storage.push(subscriber_task).await?.task_id
|
||||
@@ -62,22 +57,27 @@ impl TaskService {
|
||||
Ok(task_id)
|
||||
}
|
||||
|
||||
pub async fn setup(&self) -> RecorderResult<()> {
|
||||
pub async fn setup_monitor(&self) -> RecorderResult<Monitor> {
|
||||
let monitor = Monitor::new();
|
||||
let worker = WorkerBuilder::new(SUBSCRIBER_TASK_APALIS_NAME)
|
||||
.catch_panic()
|
||||
.enable_tracing()
|
||||
.data(self.ctx.clone())
|
||||
.backend({
|
||||
let storage = self.subscriber_task_storage.read().await;
|
||||
storage.clone()
|
||||
})
|
||||
.backend(self.subscriber_task_storage.read().await.clone())
|
||||
.build_fn(Self::run_subscriber_task);
|
||||
|
||||
let monitor = monitor.register(worker);
|
||||
Ok(monitor.register(worker))
|
||||
}
|
||||
|
||||
monitor.run().await?;
|
||||
pub async fn setup_listener(&self) -> RecorderResult<PgListen> {
|
||||
let pool = self.ctx.db().get_postgres_connection_pool().clone();
|
||||
let mut subscriber_task_listener = PgListen::new(pool).await?;
|
||||
|
||||
Ok(())
|
||||
{
|
||||
let mut subscriber_task_storage = self.subscriber_task_storage.write().await;
|
||||
subscriber_task_listener.subscribe_with(&mut subscriber_task_storage);
|
||||
}
|
||||
|
||||
Ok(subscriber_task_listener)
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user