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
impl JobRegistry
pub fn new() -> Self
Sourcepub fn register<J: Job>(&mut self) -> &mut Self
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.
Sourcepub fn register_fn<F>(&mut self, name: &str, handler: F) -> &mut Self
pub fn register_fn<F>(&mut self, name: &str, handler: F) -> &mut Self
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.
Sourcepub fn handler(
&self,
name: &str,
) -> Option<Arc<dyn Fn(Json) -> BoxFuture<'static, Result<()>> + Send + Sync>>
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.