Skip to content

Queue: Extending Worker and WorkCommand stops QueueManager container resolution. #637

Closed
@trip-somers

Description

@trip-somers
  • Lumen Version: 5.4.6
  • PHP Version: 7.1.3 (Homestead), 7.1.5 (localhost)
  • Database Driver & Version: not applicable

Description: Unresolvable dependency for $app parameter in Illuminate\Queue\QueueManager constructor

I have two very minor extensions to the Illuminate\Queue Worker and WorkCommand classes (see below for entire classes) in order to implement an exponential backoff for retrying webhooks. When I register my command in the console Kernel class, the application fails with an unresolvable dependency exception for $app in Illuminate\Queue\QueueManager.

I have tried to figure out the proper binding to fix this, but I can't figure it out. Can I just not extend these classes within Lumen?

Steps To Reproduce:

app\Console\Kernel.php

...
    protected $commands = [
        \App\Webhooks\Commands\WebhookWorkCommand::class,
    ];
...

WebhookWorker

<?php

namespace App\Webhooks;

use Illuminate\Queue\Worker;
use Illuminate\Queue\WorkerOptions as IlluminateWorkerOptions;

class WebhookWorker extends Worker
{
    /**
     * {@inheritdoc}
     */
    protected function handleJobException($connectionName, $job, IlluminateWorkerOptions $options, $e)
    {
        try {
            // First, we will go ahead and mark the job as failed if it will exceed the maximum
            // attempts it is allowed to run the next time we process it. If so we will just
            // go ahead and mark it as failed now so we do not have to release this again.
            $this->markJobAsFailedIfWillExceedMaxAttempts(
                $connectionName,
                $job,
                (int) $options->maxTries,
                $e
            );

            $this->raiseExceptionOccurredJobEvent(
                $connectionName,
                $job,
                $e
            );
        } finally {
            // If we catch an exception, we will attempt to release the job back onto the queue
            // so it is not lost entirely. This'll let the job be retried at a later time by
            // another listener (or this same one). We will re-throw this exception after.
            if (! $job->isDeleted() && ! $job->isReleased() && ! $job->hasFailed()) {
                // backoff
                switch ($job->attempts()) {
                    case 1:
                        $job->release(10); // 10 seconds
                        break;

                    case 2:
                        $job->release(30); // 30 seconds
                        break;

                    case 3:
                        $job->release(60); // 1 minute
                        break;

                    case 4:
                        $job->release(180); // 3 minutes
                        break;

                    case 5:
                        $job->release(360); // 6 minutes
                        break;

                    case 6:
                        $job->release(600); // 10 minutes
                        break;

                    default:
                        $job->release(900); // 15 minutes, max for Amazon SQS
                        break;
                }
            }
        }

        throw $e;
    }
}

WebhookWorkCommand

<?php

namespace App\Webhooks\Commands;

use Illuminate\Queue\Console\WorkCommand;

use App\Webhooks\WebhookWorker;

class WebhookWorkCommand extends WorkCommand
{
    /**
     * The console command name.
     *
     * @var string
     */
    protected $signature = 'webhooks:work
                            {connection? : The name of the queue connection to work}
                            {--queue= : The names of the queues to work}
                            {--daemon : Run the worker in daemon mode (Deprecated)}
                            {--once : Only process the next job on the queue}
                            {--delay=0 : Amount of time to delay failed jobs}
                            {--force : Force the worker to run even in maintenance mode}
                            {--memory=128 : The memory limit in megabytes}
                            {--sleep=3 : Number of seconds to sleep when no job is available}
                            {--timeout=60 : The number of seconds a child process can run}
                            {--tries=0 : Number of times to attempt a job before logging it failed}';

    /**
     * The console command description.
     *
     * @var string
     */
    protected $description = 'Start processing webhooks on the queue as a daemon';

    /**
     * The queue worker instance.
     *
     * @var WebhookWorker
     */
    protected $worker;

    /**
     * Create a new queue listen command.
     *
     * @param  WebhookWorker  $worker
     * @return void
     */
    public function __construct(WebhookWorker $worker)
    {
        parent::__construct($worker);
    }
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions