pub struct WorkerUtils { /* private fields */ }Expand description
The WorkerUtils struct provides a set of utility methods for managing jobs.
This is the primary interface for adding jobs to the queue, managing existing jobs, performing maintenance tasks, and migrating the database schema.
Implementations§
Source§impl WorkerUtils
impl WorkerUtils
Sourcepub fn with_hooks(self, hooks: Arc<HookRegistry>) -> Self
pub fn with_hooks(self, hooks: Arc<HookRegistry>) -> Self
Adds lifecycle hooks to this WorkerUtils instance.
Sourcepub fn with_task_details(self, task_details: SharedTaskDetails) -> Self
pub fn with_task_details(self, task_details: SharedTaskDetails) -> Self
Adds task details to this WorkerUtils instance.
When task_details is provided, cleanup operations that include GcTaskIdentifiers
will automatically refresh the task details to ensure the worker can still pick
up jobs after task identifiers are garbage collected.
Sourcepub fn with_use_local_time(self, use_local_time: bool) -> Self
pub fn with_use_local_time(self, use_local_time: bool) -> Self
Sets whether to use local application time or database time for timestamps.
When use_local_time is true, the application’s Utc::now() is used for timestamps,
which can help handle clock drift between the application server and database server.
When false (default), PostgreSQL’s now() is used instead.
Source§impl WorkerUtils
impl WorkerUtils
Sourcepub async fn remove_job(&self, job_key: &str) -> Result<(), GraphileWorkerError>
pub async fn remove_job(&self, job_key: &str) -> Result<(), GraphileWorkerError>
Removes a job from the queue by its job key.
Useful for cancelling scheduled jobs that haven’t run yet.
Sourcepub async fn complete_jobs(
&self,
ids: &[i64],
) -> Result<Vec<DbJob>, GraphileWorkerError>
pub async fn complete_jobs( &self, ids: &[i64], ) -> Result<Vec<DbJob>, GraphileWorkerError>
Marks multiple jobs as completed.
Sourcepub async fn permanently_fail_jobs(
&self,
ids: &[i64],
reason: &str,
) -> Result<Vec<DbJob>, GraphileWorkerError>
pub async fn permanently_fail_jobs( &self, ids: &[i64], reason: &str, ) -> Result<Vec<DbJob>, GraphileWorkerError>
Marks multiple jobs as permanently failed with a reason.
Sourcepub async fn reschedule_jobs(
&self,
ids: &[i64],
options: RescheduleJobOptions,
) -> Result<Vec<DbJob>, GraphileWorkerError>
pub async fn reschedule_jobs( &self, ids: &[i64], options: RescheduleJobOptions, ) -> Result<Vec<DbJob>, GraphileWorkerError>
Reschedules multiple jobs with new parameters.
This allows changing when jobs will run next, their priority, and their retry behavior.
Sourcepub async fn list_active_workers(
&self,
sweep_threshold: Duration,
) -> Result<Vec<ActiveWorkerRow>, GraphileWorkerError>
pub async fn list_active_workers( &self, sweep_threshold: Duration, ) -> Result<Vec<ActiveWorkerRow>, GraphileWorkerError>
Lists workers registered in the heartbeat table.
Sourcepub async fn sweep_stale_workers(
&self,
options: SweepStaleWorkersOptions,
) -> Result<SweepStaleWorkersResult, GraphileWorkerError>
pub async fn sweep_stale_workers( &self, options: SweepStaleWorkersOptions, ) -> Result<SweepStaleWorkersResult, GraphileWorkerError>
Sweeps inactive workers and orphan locks, recovering their jobs.
Sourcepub async fn sweep_stale_workers_with_config(
&self,
recovery_config: &WorkerRecoveryConfig,
options: SweepStaleWorkersOptions,
) -> Result<SweepStaleWorkersResult, GraphileWorkerError>
pub async fn sweep_stale_workers_with_config( &self, recovery_config: &WorkerRecoveryConfig, options: SweepStaleWorkersOptions, ) -> Result<SweepStaleWorkersResult, GraphileWorkerError>
Sweeps inactive workers with an explicit recovery configuration.
Sourcepub async fn force_unlock_workers(
&self,
worker_ids: &[&str],
) -> Result<(), GraphileWorkerError>
pub async fn force_unlock_workers( &self, worker_ids: &[&str], ) -> Result<(), GraphileWorkerError>
Force unlocks worker records in the database.
Useful for recovering from situations where workers crashed without properly releasing their locks, allowing their jobs to be picked up by other workers.
Sourcepub async fn cleanup(
&self,
tasks: &[CleanupTask],
) -> Result<(), GraphileWorkerError>
pub async fn cleanup( &self, tasks: &[CleanupTask], ) -> Result<(), GraphileWorkerError>
Runs database cleanup tasks to maintain performance.
When GcTaskIdentifiers is included in the tasks and this WorkerUtils instance
was created with task details (via with_task_details), the task identifiers
known to this worker will be preserved and refreshed after cleanup.
Sourcepub async fn migrate(&self) -> Result<(), MigrateError>
pub async fn migrate(&self) -> Result<(), MigrateError>
Runs database migrations to ensure the schema is up to date.
Source§impl WorkerUtils
impl WorkerUtils
Sourcepub async fn add_job<T: TaskHandler>(
&self,
payload: T,
spec: JobSpec,
) -> Result<Job, GraphileWorkerError>
pub async fn add_job<T: TaskHandler>( &self, payload: T, spec: JobSpec, ) -> Result<Job, GraphileWorkerError>
Adds a job to the queue with type safety.
Uses the TaskHandler trait to ensure that the job identifier and payload type match, providing compile-time type safety.
§Example
let job = utils.add_job(
MyTask { data: "hello".to_string() },
JobSpec::default()
).await?;Sourcepub async fn add_raw_job<P>(
&self,
identifier: &str,
payload: P,
spec: JobSpec,
) -> Result<Job, GraphileWorkerError>where
P: Serialize,
pub async fn add_raw_job<P>(
&self,
identifier: &str,
payload: P,
spec: JobSpec,
) -> Result<Job, GraphileWorkerError>where
P: Serialize,
Adds a job to the queue with a raw identifier and payload.
Doesn’t require the task handler to be defined at compile time,
allowing for dynamic job creation. However, lacks the compile-time type safety
of add_job.
§Example
let job = utils.add_raw_job(
"send_email",
json!({ "to": "user@example.com", "subject": "Hello" }),
JobSpec::default()
).await?;Sourcepub async fn add_jobs<T: TaskHandler + Clone>(
&self,
jobs: &[(T, &JobSpec)],
) -> Result<Vec<Job>, GraphileWorkerError>
pub async fn add_jobs<T: TaskHandler + Clone>( &self, jobs: &[(T, &JobSpec)], ) -> Result<Vec<Job>, GraphileWorkerError>
Adds multiple jobs of the same type to the queue in a single batch operation.
This is more efficient than calling add_job multiple times when you need to
add many jobs of the same type, as it uses a single database round trip.
§Limitations
job_key_mode: UnsafeDedupeis not supported in batch operationsjob_key_modeis applied uniformly: if any job usesPreserveRunAt, it applies to all jobs in the batch. For per-jobjob_key_modecontrol, useadd_jobindividually.
Sourcepub async fn add_raw_jobs(
&self,
jobs: &[RawJobSpec],
) -> Result<Vec<Job>, GraphileWorkerError>
pub async fn add_raw_jobs( &self, jobs: &[RawJobSpec], ) -> Result<Vec<Job>, GraphileWorkerError>
Adds multiple jobs with raw identifiers and payloads in a single batch operation.
This allows adding jobs of different types in a single batch, but without
compile-time type safety. More efficient than multiple add_raw_job calls.
§Limitations
job_key_mode: UnsafeDedupeis not supported in batch operationsjob_key_modeis applied uniformly: if any job usesPreserveRunAt, it applies to all jobs in the batch. For per-jobjob_key_modecontrol, useadd_raw_jobindividually.
Sourcepub async fn add_batch_job<T: BatchTaskHandler>(
&self,
payloads: Vec<T>,
spec: JobSpec,
) -> Result<Job, GraphileWorkerError>
pub async fn add_batch_job<T: BatchTaskHandler>( &self, payloads: Vec<T>, spec: JobSpec, ) -> Result<Job, GraphileWorkerError>
Adds a batch job to the queue with type safety.
The database payload is a JSON array of T items. Register the same
identifier with the worker builder’s batch-job registration API so the worker can
run the batch and retry only failed items after partial success.
Trait Implementations§
Source§impl Clone for WorkerUtils
impl Clone for WorkerUtils
Source§fn clone(&self) -> WorkerUtils
fn clone(&self) -> WorkerUtils
1.0.0 (const: unstable) · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
source. Read moreAuto Trait Implementations§
impl !RefUnwindSafe for WorkerUtils
impl !UnwindSafe for WorkerUtils
impl Freeze for WorkerUtils
impl Send for WorkerUtils
impl Sync for WorkerUtils
impl Unpin for WorkerUtils
impl UnsafeUnpin for WorkerUtils
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
Source§impl<T> Instrument for T
impl<T> Instrument for T
Source§fn instrument(self, span: Span) -> Instrumented<Self>
fn instrument(self, span: Span) -> Instrumented<Self>
Source§fn in_current_span(self) -> Instrumented<Self>
fn in_current_span(self) -> Instrumented<Self>
Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self>
fn into_either(self, into_left: bool) -> Either<Self, Self>
self into a Left variant of Either<Self, Self>
if into_left is true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
self into a Left variant of Either<Self, Self>
if into_left(&self) returns true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read more