Fixes the queue for beta 14 (#2363)

- rewrite the queue handling for illuminate 6+
- implement missing maintenance mode callable for queue Worker
- Ensure we resolve append the queue commands once the queue bindings are loaded
- Override WorkCommand because it needs the maintenance flag. It tries to use
the isDownForMaintenance method from the Container assuming it is a Laravel
Application. Circumvented this issue by resolving our Config from IOC instead.
This commit is contained in:
Daniël Klabbers 2020-10-09 22:06:28 +02:00 committed by GitHub
parent 32dc54becc
commit 84ce740489
3 changed files with 43 additions and 70 deletions

View File

@ -0,0 +1,27 @@
<?php
/*
* This file is part of Flarum.
*
* For detailed copyright and license information, please view the
* LICENSE file that was distributed with this source code.
*/
namespace Flarum\Queue\Console;
use Flarum\Foundation\Config;
class WorkCommand extends \Illuminate\Queue\Console\WorkCommand
{
protected function downForMaintenance()
{
if ($this->option('force')) {
return false;
}
/** @var Config $config */
$config = $this->laravel->make(Config::class);
return $config->inMaintenanceMode();
}
}

View File

@ -1,61 +0,0 @@
<?php
/*
* This file is part of Flarum.
*
* For detailed copyright and license information, please view the
* LICENSE file that was distributed with this source code.
*/
namespace Flarum\Queue;
use Illuminate\Contracts\Queue\Factory;
use Illuminate\Queue\QueueManager;
/**
* A hacky workaround to avoid injecting an entire QueueManager (which we don't
* want to build) into Laravel's queue worker class.
*
* Laravel 6.0 will clean this up; once we upgrade, we can remove this hack and
* directly inject the factory.
*/
class HackyManagerForWorker extends QueueManager implements Factory
{
/**
* @var Factory
*/
private $factory;
/**
* HackyManagerForWorker constructor.
*
* Needs a real connection factory to delegate to.
*
* @param Factory $factory
*/
public function __construct(Factory $factory)
{
$this->factory = $factory;
}
/**
* Resolve a queue connection instance.
*
* @param string $name
* @return \Illuminate\Contracts\Queue\Queue
*/
public function connection($name = null)
{
return $this->factory->connection($name);
}
/**
* Determine if the application is in maintenance mode.
*
* @return bool
*/
public function isDownForMaintenance()
{
return false;
}
}

View File

@ -9,8 +9,8 @@
namespace Flarum\Queue;
use Flarum\Console\Event\Configuring;
use Flarum\Foundation\AbstractServiceProvider;
use Flarum\Foundation\Config;
use Flarum\Foundation\ErrorHandling\Registry;
use Flarum\Foundation\ErrorHandling\Reporter;
use Flarum\Foundation\Paths;
@ -22,6 +22,7 @@ use Illuminate\Queue\Console as Commands;
use Illuminate\Queue\Events\JobFailed;
use Illuminate\Queue\Failed\NullFailedJobProvider;
use Illuminate\Queue\Listener as QueueListener;
use Illuminate\Queue\QueueManager;
use Illuminate\Queue\SyncQueue;
use Illuminate\Queue\Worker;
@ -34,7 +35,7 @@ class QueueServiceProvider extends AbstractServiceProvider
Commands\ListFailedCommand::class,
Commands\RestartCommand::class,
Commands\RetryCommand::class,
Commands\WorkCommand::class,
Console\WorkCommand::class,
];
public function register()
@ -61,10 +62,16 @@ class QueueServiceProvider extends AbstractServiceProvider
});
$this->app->singleton(Worker::class, function ($app) {
/** @var Config $config */
$config = $app->make(Config::class);
return new Worker(
new HackyManagerForWorker($app[Factory::class]),
new QueueManager($app),
$app['events'],
$app[ExceptionHandling::class]
$app[ExceptionHandling::class],
function () use ($config) {
return $config->inMaintenanceMode();
}
);
});
@ -110,17 +117,17 @@ class QueueServiceProvider extends AbstractServiceProvider
protected function registerCommands()
{
$this->app['events']->listen(Configuring::class, function (Configuring $event) {
$this->app->extend('flarum.console.commands', function ($commands) {
$queue = $this->app->make(Queue::class);
// There is no need to have the queue commands when using the sync driver.
if ($queue instanceof SyncQueue) {
return;
return $commands;
}
foreach ($this->commands as $command) {
$event->addCommand($command);
}
// Otherwise add our commands, while allowing them to be overridden by those
// already added through the container.
return array_merge($this->commands, $commands);
});
}