use std::{str::FromStr, sync::Arc, time::Duration};
use apalis_core::{
backend::{
Backend, BackendExt, FetchById, ResumeAbandoned, ResumeById, TaskResult, TaskSink, Update,
Vacuum, WaitForCompletion, codec::Codec,
},
task::task_id::TaskId,
};
use apalis_rtui_types::{
ActionOutcome, ApiError, BulkAction, BulkFailure, BulkOutcome, RetryMode, TaskStatusEvent,
};
use futures::StreamExt as _;
use serde::{Serialize, de::DeserializeOwned};
use tokio::sync::RwLock;
use crate::api::capability::{
LiveResult, LiveStatusUpgrade, RescheduleUpgrade, ResumeUpgrade, UpdateUpgrade,
};
pub async fn retry<B, T, Compact>(
task_id: String,
storage: Arc<RwLock<B>>,
upgrade: Option<RescheduleUpgrade>,
) -> Result<Option<ActionOutcome>, ApiError>
where
T: Serialize + DeserializeOwned + 'static,
B: TaskSink<T> + FetchById<T> + BackendExt + Send + 'static,
B::Error: std::error::Error,
B::IdType: FromStr,
<B::IdType as FromStr>::Err: std::error::Error,
B::Codec: Codec<T, Compact = Compact>,
<B::Codec as Codec<T>>::Error: std::error::Error,
{
if let Some(upgrade) = upgrade {
let rescheduled = upgrade(task_id.clone(), Duration::ZERO).await?;
return Ok(rescheduled.then_some(ActionOutcome::Retry {
task_id,
mode: RetryMode::Reschedule,
}));
}
let id = TaskId::<B::IdType>::from_str(&task_id)
.map_err(|e| ApiError::BackendError(e.to_string()))?;
let Some(existing) = storage
.write()
.await
.fetch_by_id(&id)
.await
.map_err(|e| ApiError::BackendError(e.to_string()))?
else {
return Ok(None);
};
storage
.write()
.await
.push(existing.args)
.await
.map_err(|e| ApiError::BackendError(e.to_string()))?;
Ok(Some(ActionOutcome::Retry {
task_id,
mode: RetryMode::NewTask,
}))
}
pub async fn reschedule<B>(
task_id: String,
storage: Arc<RwLock<B>>,
wait: Duration,
) -> Result<Option<ActionOutcome>, ApiError>
where
B: apalis_core::backend::Reschedule + FetchById<<B as Backend>::Args>,
B::Error: std::error::Error,
B::IdType: FromStr,
<B::IdType as FromStr>::Err: std::error::Error,
{
Ok(reschedule_task(&task_id, storage, wait)
.await?
.then_some(ActionOutcome::Rescheduled { task_id }))
}
async fn reschedule_task<B>(
task_id: &str,
storage: Arc<RwLock<B>>,
wait: Duration,
) -> Result<bool, ApiError>
where
B: apalis_core::backend::Reschedule + FetchById<<B as Backend>::Args>,
B::Error: std::error::Error,
B::IdType: FromStr,
<B::IdType as FromStr>::Err: std::error::Error,
{
let id = TaskId::<B::IdType>::from_str(task_id)
.map_err(|e| ApiError::BackendError(e.to_string()))?;
let mut guard = storage.write().await;
let Some(task) = guard
.fetch_by_id(&id)
.await
.map_err(|e| ApiError::BackendError(e.to_string()))?
else {
return Ok(false);
};
guard
.reschedule(task, wait)
.await
.map_err(|e| ApiError::BackendError(e.to_string()))?;
Ok(true)
}
pub(crate) fn reschedule_upgrade<B>(backend: Arc<RwLock<B>>) -> RescheduleUpgrade
where
B: apalis_core::backend::Reschedule + FetchById<<B as Backend>::Args> + Send + Sync + 'static,
B::Error: std::error::Error,
B::IdType: FromStr + Send,
<B::IdType as FromStr>::Err: std::error::Error,
{
Arc::new(move |task_id: String, wait: Duration| {
let storage = Arc::clone(&backend);
Box::pin(async move { reschedule_task(&task_id, storage, wait).await })
})
}
pub async fn resume<B>(
task_id: String,
storage: Arc<RwLock<B>>,
) -> Result<Option<ActionOutcome>, ApiError>
where
B: ResumeById,
B::Error: std::error::Error,
B::IdType: FromStr,
<B::IdType as FromStr>::Err: std::error::Error,
{
Ok(resume_task(&task_id, storage)
.await?
.then_some(ActionOutcome::Resumed { task_id }))
}
async fn resume_task<B>(task_id: &str, storage: Arc<RwLock<B>>) -> Result<bool, ApiError>
where
B: ResumeById,
B::Error: std::error::Error,
B::IdType: FromStr,
<B::IdType as FromStr>::Err: std::error::Error,
{
let id = TaskId::<B::IdType>::from_str(task_id)
.map_err(|e| ApiError::BackendError(e.to_string()))?;
storage
.write()
.await
.resume_by_id(id)
.await
.map_err(|e| ApiError::BackendError(e.to_string()))
}
pub(crate) fn resume_upgrade<B>(backend: Arc<RwLock<B>>) -> ResumeUpgrade
where
B: ResumeById + Send + Sync + 'static,
B::Error: std::error::Error,
B::IdType: FromStr + Send,
<B::IdType as FromStr>::Err: std::error::Error,
{
Arc::new(move |task_id: String| {
let storage = Arc::clone(&backend);
Box::pin(async move { resume_task(&task_id, storage).await })
})
}
pub async fn resume_abandoned<B>(storage: Arc<RwLock<B>>) -> Result<ActionOutcome, ApiError>
where
B: ResumeAbandoned,
B::Error: std::error::Error,
{
let count = storage
.write()
.await
.resume_abandoned()
.await
.map_err(|e| ApiError::BackendError(e.to_string()))?;
Ok(ActionOutcome::ResumedAbandoned {
count: Some(u64::try_from(count).unwrap_or(u64::MAX)),
})
}
pub async fn vacuum<B>(storage: Arc<RwLock<B>>) -> Result<ActionOutcome, ApiError>
where
B: Vacuum,
B::Error: std::error::Error,
{
let count = storage
.write()
.await
.vacuum()
.await
.map_err(|e| ApiError::BackendError(e.to_string()))?;
Ok(ActionOutcome::Vacuumed {
count: Some(u64::try_from(count).unwrap_or(u64::MAX)),
})
}
pub async fn update<B>(
task_id: String,
storage: Arc<RwLock<B>>,
_priority: Option<i32>,
_meta: Option<serde_json::Value>,
) -> Result<Option<ActionOutcome>, ApiError>
where
B: Update + FetchById<<B as Backend>::Args>,
B::Error: std::error::Error,
B::IdType: FromStr,
<B::IdType as FromStr>::Err: std::error::Error,
{
Ok(update_task(&task_id, storage)
.await?
.then_some(ActionOutcome::Updated { task_id }))
}
async fn update_task<B>(task_id: &str, storage: Arc<RwLock<B>>) -> Result<bool, ApiError>
where
B: Update + FetchById<<B as Backend>::Args>,
B::Error: std::error::Error,
B::IdType: FromStr,
<B::IdType as FromStr>::Err: std::error::Error,
{
let id = TaskId::<B::IdType>::from_str(task_id)
.map_err(|e| ApiError::BackendError(e.to_string()))?;
let mut guard = storage.write().await;
let Some(task) = guard
.fetch_by_id(&id)
.await
.map_err(|e| ApiError::BackendError(e.to_string()))?
else {
return Ok(false);
};
guard
.update(task)
.await
.map_err(|e| ApiError::BackendError(e.to_string()))?;
Ok(true)
}
pub(crate) fn update_upgrade<B>(backend: Arc<RwLock<B>>) -> UpdateUpgrade
where
B: Update + FetchById<<B as Backend>::Args> + Send + Sync + 'static,
B::Error: std::error::Error,
B::IdType: FromStr + Send,
<B::IdType as FromStr>::Err: std::error::Error,
{
Arc::new(move |task_id: String| {
let storage = Arc::clone(&backend);
Box::pin(async move { update_task(&task_id, storage).await })
})
}
pub(crate) fn live_status_upgrade<B>(queue: String, backend: Arc<RwLock<B>>) -> LiveStatusUpgrade
where
B: WaitForCompletion<LiveResult> + BackendExt + Send + Sync + 'static,
B::Error: std::error::Error,
B::IdType: FromStr + Send + Sync,
<B::IdType as FromStr>::Err: std::error::Error,
{
Arc::new(move |task_id: String| {
let queue = queue.clone();
let backend = Arc::clone(&backend);
Box::pin(async move {
let id = TaskId::<B::IdType>::from_str(&task_id).ok()?;
let mut stream = Box::pin(backend.read().await.wait_for_single(id));
let result = stream.next().await?.ok()?;
Some(task_result_to_event(task_id, queue, result))
})
})
}
fn task_result_to_event<Id>(
task_id: String,
queue: String,
result: TaskResult<LiveResult, Id>,
) -> TaskStatusEvent {
let status = result.status.to_string();
let result = Some(match result.result {
Ok(LiveResult(value)) => value.to_string(),
Err(message) => message,
});
TaskStatusEvent {
task_id,
queue,
status,
attempt: 0,
done_in_ms: None,
result,
}
}
pub async fn bulk<B, T, Compact>(
request: apalis_rtui_types::BulkRequest,
storage: Arc<RwLock<B>>,
reschedule: Option<RescheduleUpgrade>,
resume: Option<ResumeUpgrade>,
update: Option<UpdateUpgrade>,
) -> BulkOutcome
where
T: Serialize + DeserializeOwned + 'static,
B: TaskSink<T> + FetchById<T> + BackendExt + Send + 'static,
B::Error: std::error::Error,
B::IdType: FromStr,
<B::IdType as FromStr>::Err: std::error::Error,
B::Codec: Codec<T, Compact = Compact>,
<B::Codec as Codec<T>>::Error: std::error::Error,
{
let wait = Duration::from_secs(request.wait_secs.unwrap_or(0));
let mut succeeded = Vec::new();
let mut failed = Vec::new();
for id in request.task_ids {
let outcome: Result<bool, String> = match request.action {
BulkAction::Retry => {
retry::<B, T, Compact>(id.clone(), Arc::clone(&storage), reschedule.clone())
.await
.map(|found| found.is_some())
.map_err(|e| e.to_string())
}
BulkAction::Reschedule => match &reschedule {
Some(hook) => hook(id.clone(), wait).await.map_err(|e| e.to_string()),
None => Err("reschedule not registered for this queue".to_owned()),
},
BulkAction::Resume => match &resume {
Some(hook) => hook(id.clone()).await.map_err(|e| e.to_string()),
None => Err("resume not registered for this queue".to_owned()),
},
BulkAction::Update => match &update {
Some(hook) => hook(id.clone()).await.map_err(|e| e.to_string()),
None => Err("update not registered for this queue".to_owned()),
},
};
match outcome {
Ok(true) => succeeded.push(id),
Ok(false) => failed.push(BulkFailure {
id,
error: "no task with that id".to_owned(),
}),
Err(error) => failed.push(BulkFailure { id, error }),
}
}
BulkOutcome { succeeded, failed }
}