Skip to content

Commit

Permalink
feat: Add get_ext() helper method on WorkerContext
Browse files Browse the repository at this point in the history
  • Loading branch information
leo91000 committed May 29, 2024
1 parent 66254d6 commit 72b924a
Showing 1 changed file with 13 additions and 0 deletions.
13 changes: 13 additions & 0 deletions crates/ctx/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
use std::fmt::Debug;

use getset::Getters;
use graphile_worker_extensions::ReadOnlyExtensions;
use graphile_worker_job::Job;
Expand All @@ -7,14 +9,20 @@ use sqlx::PgPool;
#[derive(Getters, Clone, Debug)]
#[getset(get = "pub")]
pub struct WorkerContext {
/// The payload of the job.
payload: Value,
/// The postgres pool.
pg_pool: PgPool,
/// The job.
job: Job,
/// The worker id.
worker_id: String,
/// Extensions (for providing custom app state)
extensions: ReadOnlyExtensions,
}

impl WorkerContext {
/// Creates a new WorkerContext.
pub fn new(
payload: Value,
pg_pool: PgPool,
Expand All @@ -30,4 +38,9 @@ impl WorkerContext {
extensions,
}
}

/// Returns the extension value associated with the specified type.
pub fn get_ext<T: Send + Sync + 'static>(&self) -> Option<&T> {
self.extensions.get()
}
}

0 comments on commit 72b924a

Please sign in to comment.