use crate::errors::GraphileWorkerError;
use crate::{Job, JobSpec, TaskHandler};
use graphile_worker_ctx::WorkerContext;
use graphile_worker_queries::add_job::types::RawJobSpec;
use graphile_worker_task_handler::BatchTaskHandler;
use graphile_worker_utils::WorkerUtils;
use serde::Serialize;
pub trait WorkerContextExt {
fn utils(&self) -> WorkerUtils;
fn add_job<T: TaskHandler + 'static>(
&self,
payload: T,
spec: JobSpec,
) -> impl core::future::Future<Output = Result<Job, GraphileWorkerError>> + Send;
fn add_raw_job<P: Serialize + Send + 'static>(
&self,
identifier: &str,
payload: P,
spec: JobSpec,
) -> impl core::future::Future<Output = Result<Job, GraphileWorkerError>> + Send;
fn add_jobs<T: TaskHandler + Clone + 'static>(
&self,
jobs: &[(T, &JobSpec)],
) -> impl core::future::Future<Output = Result<Vec<Job>, GraphileWorkerError>> + Send;
fn add_raw_jobs(
&self,
jobs: &[RawJobSpec],
) -> impl core::future::Future<Output = Result<Vec<Job>, GraphileWorkerError>> + Send;
fn add_batch_job<T: BatchTaskHandler + 'static>(
&self,
payloads: Vec<T>,
spec: JobSpec,
) -> impl core::future::Future<Output = Result<Job, GraphileWorkerError>> + Send;
}
impl WorkerContextExt for WorkerContext {
fn utils(&self) -> WorkerUtils {
WorkerUtils::new(self.database().clone(), self.schema().clone())
.with_use_local_time(self.use_local_time())
.with_task_details(self.task_details().clone())
}
async fn add_job<T: TaskHandler + 'static>(
&self,
payload: T,
spec: JobSpec,
) -> Result<Job, GraphileWorkerError> {
self.utils().add_job(payload, spec).await
}
async fn add_raw_job<P: Serialize + Send + 'static>(
&self,
identifier: &str,
payload: P,
spec: JobSpec,
) -> Result<Job, GraphileWorkerError> {
self.utils().add_raw_job(identifier, payload, spec).await
}
async fn add_jobs<T: TaskHandler + Clone + 'static>(
&self,
jobs: &[(T, &JobSpec)],
) -> Result<Vec<Job>, GraphileWorkerError> {
self.utils().add_jobs(jobs).await
}
async fn add_raw_jobs(&self, jobs: &[RawJobSpec]) -> Result<Vec<Job>, GraphileWorkerError> {
self.utils().add_raw_jobs(jobs).await
}
async fn add_batch_job<T: BatchTaskHandler + 'static>(
&self,
payloads: Vec<T>,
spec: JobSpec,
) -> Result<Job, GraphileWorkerError> {
self.utils().add_batch_job(payloads, spec).await
}
}