0
0
Laravelframework~20 mins

Dispatching jobs in Laravel - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Laravel Job Dispatcher Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
component_behavior
intermediate
2:00remaining
What happens when a job is dispatched synchronously?

Consider a Laravel job dispatched using dispatchSync(). What is the behavior of the application during this dispatch?

Laravel
MyJob::dispatchSync($data);
AThe job is queued and runs later asynchronously.
BThe job is dispatched to a remote server automatically.
CThe job is ignored if the queue worker is not running.
DThe job runs immediately in the current process before moving on.
Attempts:
2 left
💡 Hint

Think about what dispatchSync() means compared to dispatch().

state_output
intermediate
2:00remaining
What is the queue name used when dispatching a job with ->onQueue('emails')?

Given the code below, what queue will the job be sent to?

MyJob::dispatch($data)->onQueue('emails');
Laravel
MyJob::dispatch($data)->onQueue('emails');
AThe job will be sent to a queue named 'default_emails'.
BThe job will be sent to the default queue regardless of 'emails'.
CThe job will be sent to the 'emails' queue.
DThe job will fail to dispatch due to invalid queue name.
Attempts:
2 left
💡 Hint

Check how onQueue() affects the job dispatch.

📝 Syntax
advanced
2:00remaining
Which code snippet correctly dispatches a job with a delay of 10 minutes?

Select the correct Laravel code to dispatch MyJob with a 10-minute delay.

AMyJob::dispatch()->delay(now()->addMinutes(10));
BMyJob::dispatch()->delay(10);
Cdispatch((new MyJob())->delay(now()->addMinutes(10)));
DMyJob::dispatch()->delay('10 minutes');
Attempts:
2 left
💡 Hint

Remember the delay method expects a DateTime or Carbon instance.

🔧 Debug
advanced
2:00remaining
Why does this dispatched job never run?

Given the code below, the job never executes. What is the most likely cause?

MyJob::dispatch($data);

The queue worker is running and configured properly.

Laravel
MyJob::dispatch($data);
AThe job is dispatched synchronously and finishes instantly.
BThe queue connection is set to 'sync' in the config.
CThe job class does not implement the ShouldQueue interface.
DThe job is missing the handle() method.
Attempts:
2 left
💡 Hint

Check the queue connection configuration.

🧠 Conceptual
expert
3:00remaining
What is the effect of chaining ->onConnection('redis')->onQueue('emails') when dispatching a job?

Consider the code below:

MyJob::dispatch($data)->onConnection('redis')->onQueue('emails');

What does this chaining do?

AIt dispatches the job to the 'emails' queue on the 'redis' connection.
BIt dispatches the job to the default queue on the default connection ignoring parameters.
CIt causes a runtime error because chaining these methods is invalid.
DIt dispatches the job synchronously bypassing the queue.
Attempts:
2 left
💡 Hint

Think about what onConnection() and onQueue() specify.