laravel-queue-rabbitmq
laravel-queue-rabbitmq copied to clipboard
Job not executing but message is being consumed and ack'ed
- Lumen version: 9.0.1
- RabbitMQ version: 3.9.14
- Package version: 12.0.0
"require": { "php": "^8.0", "ext-mongodb": "*", "jenssegers/mongodb": "^3.9", "laravel/lumen-framework": "^9.0", "php-amqplib/php-amqplib": "^3.2", "sentry/sdk": "^3.1", "squizlabs/php_codesniffer": "^3", "vladimir-yuldashev/laravel-queue-rabbitmq": "^12.0" },
Describe the bug
The described job is not being executed.
Steps To Reproduce
Generate config as in guide, with custom job. Insert a message into rabbitmq The message is ack'ed The custom job/command is not executed
My config:
queue.php
<?php
use App\Queue\Jobs\RabbitMQJob;
return [
/*
|--------------------------------------------------------------------------
| Default Queue Connection Name
|--------------------------------------------------------------------------
|
| Laravel's queue API supports an assortment of back-ends via a single
| API, giving you convenient access to each back-end using the same
| syntax for every one. Here you may define a default connection.
|
*/
'default' => env('QUEUE_CONNECTION', 'rabbitmq'),
/*
|--------------------------------------------------------------------------
| Queue Connections
|--------------------------------------------------------------------------
|
| Here you may configure the connection information for each server that
| is used by your application. A default configuration has been added
| for each back-end shipped with Laravel. You are free to add more.
|
| Drivers: "sync", "database", "beanstalkd", "sqs", "redis", "null"
|
*/
'connections' => [
'rabbitmq' => [
'driver' => 'rabbitmq',
'queue' => env('RABBITMQ_QUEUE', 'messages'),
'connection' => PhpAmqpLib\Connection\AMQPLazyConnection::class,
'hosts' => [
[
'host' => env('RABBITMQ_HOST', '127.0.0.1'),
'port' => env('RABBITMQ_PORT', 5672),
'user' => env('RABBITMQ_USER', 'guest'),
'password' => env('RABBITMQ_PASSWORD', 'guest'),
'vhost' => env('RABBITMQ_VHOST', '/'),
],
],
'options' => [
'ssl_options' => [
'cafile' => env('RABBITMQ_SSL_CAFILE', null),
'local_cert' => env('RABBITMQ_SSL_LOCALCERT', null),
'local_key' => env('RABBITMQ_SSL_LOCALKEY', null),
'verify_peer' => env('RABBITMQ_SSL_VERIFY_PEER', true),
'passphrase' => env('RABBITMQ_SSL_PASSPHRASE', null),
],
'queue' => [
'job' => RabbitMQJob::class,
],
],
/*
* Set to "horizon" if you wish to use Laravel Horizon.
*/
'worker' => env('RABBITMQ_WORKER', 'default'),
],
'sync' => [
'driver' => 'sync',
],
'database' => [
'driver' => 'database',
'table' => 'jobs',
'queue' => 'default',
'retry_after' => 90,
'after_commit' => false,
],
'beanstalkd' => [
'driver' => 'beanstalkd',
'host' => 'localhost',
'queue' => 'default',
'retry_after' => 90,
'block_for' => 0,
'after_commit' => false,
],
'sqs' => [
'driver' => 'sqs',
'key' => env('AWS_ACCESS_KEY_ID'),
'secret' => env('AWS_SECRET_ACCESS_KEY'),
'prefix' => env('SQS_PREFIX', 'https://sqs.us-east-1.amazonaws.com/your-account-id'),
'queue' => env('SQS_QUEUE', 'default'),
'suffix' => env('SQS_SUFFIX'),
'region' => env('AWS_DEFAULT_REGION', 'us-east-1'),
'after_commit' => false,
],
'redis' => [
'driver' => 'redis',
'connection' => 'default',
'queue' => env('REDIS_QUEUE', 'default'),
'retry_after' => 90,
'block_for' => null,
'after_commit' => false,
],
],
/*
|--------------------------------------------------------------------------
| Failed Queue Jobs
|--------------------------------------------------------------------------
|
| These options configure the behavior of failed queue job logging so you
| can control which database and table are used to store the jobs that
| have failed. You may change them to any database / table you wish.
|
*/
'failed' => [
'driver' => env('QUEUE_FAILED_DRIVER', 'database-uuids'),
'database' => env('DB_CONNECTION', 'mysql'),
'table' => 'failed_jobs',
],
];
RabbitMQJob.php
<?php
namespace App\Queue\Jobs;
use App\Jobs\CreateNotificationJob;
use VladimirYuldashev\LaravelQueueRabbitMQ\Queue\Jobs\RabbitMQJob as BaseJob;
class RabbitMQJob extends BaseJob
{
/**
* Fire the job.
*
* @return void
*/
public function fire()
{
echo "Creating notification2";
$payload = $this->payload();
$class = CreateNotificationJob::class;
$method = 'handle';
($this->instance = $this->resolve($class))->{$method}($this, $payload);
$this->delete();
}
}
Note that I added echo "Creating notification2";
but this doesn't get executed. Screen is empty.
Looks like I need this mandatory, now I'm getting another error
public function payload()
{
return [
'job' => CreateNotificationJob::class . '@handle',
'data' => json_decode($this->getRawBody(), true)
];
}
New error:
"\n\033[1m\033[31mIlluminate\\Contracts\\Container\\BindingResolutionException\033[0m: Unresolvable dependency resolving [Parameter #0 [ <required> array $data ]] in class App\\Jobs\\CreateNotificationJob\033[22m in \033[31m/application/vendor/illuminate/container/Container.php\033[0m on line \033[32m1104\033[0m\033[22m\n\n\033[1mCall Stack:\033[22m\n 0.0004 389528 1. {main}() /application/artisan:0\n 0.0393 7724512 2. Laravel\\Lumen\\Console\\Kernel->handle($input = class Symfony\\Component"
This is how my Queue Job ended up to solve the issue
namespace App\Queue\Jobs;
use App\Jobs\CreateNotificationJob;
use VladimirYuldashev\LaravelQueueRabbitMQ\Queue\Jobs\RabbitMQJob as BaseJob;
class RabbitMQJob extends BaseJob
{
/**
* Fire the job.
*
* @return void
*/
public function fire()
{
$payload = $this->payload();
$method = 'handle';
($this->instance = $this->resolve(CreateNotificationJob::class))->{$method}($payload['data']);
$this->delete();
}
public function payload()
{
return [
'job' => CreateNotificationJob::class . '@handle',
'data' => json_decode($this->getRawBody(), true)
];
}
}
This is how my Queue Job ended up to solve the issue
namespace App\Queue\Jobs; use App\Jobs\CreateNotificationJob; use VladimirYuldashev\LaravelQueueRabbitMQ\Queue\Jobs\RabbitMQJob as BaseJob; class RabbitMQJob extends BaseJob { /** * Fire the job. * * @return void */ public function fire() { $payload = $this->payload(); $method = 'handle'; ($this->instance = $this->resolve(CreateNotificationJob::class))->{$method}($payload['data']); $this->delete(); } public function payload() { return [ 'job' => CreateNotificationJob::class . '@handle', 'data' => json_decode($this->getRawBody(), true) ]; } }
How did you make the call to send this way?
Looks like I need this mandatory, now I'm getting another error
public function payload() { return [ 'job' => CreateNotificationJob::class . '@handle', 'data' => json_decode($this->getRawBody(), true) ]; }
New error:
"\n\033[1m\033[31mIlluminate\\Contracts\\Container\\BindingResolutionException\033[0m: Unresolvable dependency resolving [Parameter #0 [ <required> array $data ]] in class App\\Jobs\\CreateNotificationJob\033[22m in \033[31m/application/vendor/illuminate/container/Container.php\033[0m on line \033[32m1104\033[0m\033[22m\n\n\033[1mCall Stack:\033[22m\n 0.0004 389528 1. {main}() /application/artisan:0\n 0.0393 7724512 2. Laravel\\Lumen\\Console\\Kernel->handle($input = class Symfony\\Component"
I agree with you it is mandatory.
Looks like this was resolved.