Skip to main content

JobRegistry

Struct JobRegistry 

Source
pub struct JobRegistry { /* private fields */ }
Expand description

The map from a stored job name back to code that can run it.

Laravel needs nothing like this: it stores the serialised PHP object and asks the runtime to bring the class back to life. A compiled language cannot resurrect a type from a string — the type may not even be linked into the binary that reads the row — so the application states the connection once, at boot:

ⓘ
let mut jobs = JobRegistry::new();
jobs.register::<SendWelcomeEmail>();
jobs.register::<GenerateInvoice>();

The cost is one line per job. What is bought is that queue:work fails to compile against a renamed job type, instead of failing at three in the morning against a class that is no longer there.

Implementations§

Source§

impl JobRegistry

Source

pub fn new() -> Self

Source

pub fn register<J: Job>(&mut self) -> &mut Self

Teach the registry about one job type.

Registering the same name twice replaces the handler, so a test can substitute a job without rebuilding the registry.

Source

pub fn register_fn<F>(&mut self, name: &str, handler: F) -> &mut Self
where F: Fn(Json) -> BoxFuture<'static, Result<()>> + Send + Sync + 'static,

Register a handler directly, for a name with no Rust type behind it — a job pushed by another service, or a stub in a test.

Source

pub fn handler( &self, name: &str, ) -> Option<Arc<dyn Fn(Json) -> BoxFuture<'static, Result<()>> + Send + Sync>>

The handler for a name, if one was registered.

Source

pub fn contains(&self, name: &str) -> bool

Source

pub fn len(&self) -> usize

Source

pub fn is_empty(&self) -> bool

Source

pub fn names(&self) -> Vec<String>

Every registered name, sorted — for queue:work’s boot log and for the error message below.

Source

pub fn run(&self, name: &str, payload: Json) -> BoxFuture<'static, Result<()>>

Run a stored job, or explain why it cannot be run.

The error names what is registered: “no handler for x” on its own leaves the reader guessing whether they misspelled the job or forgot the registration line.

Trait Implementations§

Source§

impl Default for JobRegistry

Source§

fn default() -> JobRegistry

Returns the “default value” for a type. Read more

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> Same for T

Source§

type Output = T

Should always be Self
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = !

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, !>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.