Entries tagged queue

Laravel QueueWorker: database not configured

Posted on 4. November 2019 Comments

It doesn’t matter which queue system you’re using: Redis, a relational database such as MySQL or a cloud solution like AWS SQS ; you can always store failed jobs in a normal database table. This configuration is done in config/queue.php in the last array:

'failed' => [
    'driver' => 'database',
    'database' => 'default', // NOTE: connection, not database!
    'table' => 'jobs_failed',
],

The key is to put in the connection name, not the database name for ‚database‘. If you don’t you will get the error message „Database default not configured“ every time a job fails instead.

Check out my Laravel Job Eloquent Models.


Laravel: Serialization of ‚Closure‘ is not allowed with Jobs and Queues

Posted on 26. Februar 2018 Comments

When dispatching Jobs onto a queue it’s possible to get the following error:

Serialization of ‚Closure‘ is not allowed

This basically means that you are either passing a class that contains anonymous functions („closures“ in PHP) or that in the Job itself there is an anonymous function. This could also be a library you are using. PHP is unable to serialize closures per se. When Jobs get dispatched onto the queue, everything has to be serialized before. If you absolutely must use them this way, look into the Super Closure package by jeremeamia. However, just rewriting the code a little might be easier:

 

Solution

Instead of passing a whole object to the Job like so:

$objOfClassWithClosures = ...;
$job = new Job($objOfClassWithClosures);
dispatch($job);

Pass the Fully Qualified Class Name (that’s what ClassName::class returns) to the job and instantiate the object on the other side, possibly even using Laravels Container:

$job = new Job(ClassWithClosures::class);
dispatch($job);

Inside the job you can then use the constructor to make a new instance of that class:

public function __construct(string $fqcn) {
$objOfClassWithClosures = app()->make($fqcn);
}