在我的共享主机解决方案上使用 laravel 队列无法 运行

Using laravel queue fails to run on my shared hosting solution

正在尝试让队列在我的共享主机上工作,

php artisan queue:work 在我的共享主机上(通过 cron)returns

[ErrorException] 
Invalid argument supplied for foreach()

在我的日志文件中写了以下内容。

[2015-04-12 18:59:01] production.ERROR: exception 'ErrorException' with message 'Invalid argument supplied for foreach()' in /home/a109/vendor/symfony/console/Symfony/Component/Console/Input/ArgvInput.php:287 Stack trace:
#0 /home/a109/vendor/symfony/console/Symfony/Component/Console/Input/ArgvInput.php(287): Illuminate\Foundation\Bootstrap\HandleExceptions->handleError(2, 'Invalid argumen...', '/home/a109/vend...', 287, Array)
#1 /home/a109/vendor/symfony/console/Symfony/Component/Console/Application.php(823): Symfony\Component\Console\Input\ArgvInput->hasParameterOption(Array)
#2 /home/a109/vendor/symfony/console/Symfony/Component/Console/Application.php(123): Symfony\Component\Console\Application->configureIO(Object(Symfony\Component\Console\Input\ArgvInput), Object(Symfony\Component\Console\Output\ConsoleOutput))
#3 /home/a109/vendor/laravel/framework/src/Illuminate/Foundation/Console/Kernel.php(94): Symfony\Component\Console\Application->run(Object(Symfony\Component\Console\Input\ArgvInput), Object(Symfony\Component\Console\Output\ConsoleOutput))
#4 /home/a109/artisan(35): Illuminate\Foundation\Console\Kernel->handle(Object(Symfony\Component\Console\Input\ArgvInput), Object(Symfony\Component\Console\Output\ConsoleOutput))
#5 {main}

在 vendor/symfony/console/Symfony/Component/Console/Input/ArgvInput.php 的第 287 行有这个函数

  /**
 * Returns true if the raw parameters (not parsed) contain a value.
 *
 * This method is to be used to introspect the input parameters
 * before they have been validated. It must be used carefully.
 *
 * @param string|array $values The value(s) to look for in the raw parameters (can be an array)
 *
 * @return bool true if the value is contained in the raw parameters
 */
public function hasParameterOption($values)
{
    $values = (array) $values;

    foreach ($this->tokens as $token) {
        foreach ($values as $value) {
            if ($token === $value || 0 === strpos($token, $value.'=')) {
                return true;
            }
        }
    }

    return false;
}

那是失败的傻瓜。

我怎样才能让它工作?

Comyn 在 freenode

的#laravel 中回答

我不得不将 cron 命令更改为:

php -d register_argc_argv=On artisan queue:work

自 Laravel 5.7 以来,有一个新的队列命令在空时停止工作:

php artisan queue:work --stop-when-empty

由于这主要用于处理电子邮件或少量工作,因此我将其置于每分钟 运行 的 cronjob 中。我会说,这并不是每分钟超过 20 个作业的解决方案,但适用于我的电子邮件。这将 运行 每分钟大约 5 秒用于发送电子邮件,具体取决于电子邮件的数量。

步骤

  1. 创建新命令:$ php artisan make:command SendContactEmails
  2. SendContactEmails.php 中,更改:protected $signature = 'emails:work';
  3. handle()方法中,添加:
return $this->call('queue:work', [
    '--queue' => 'emails', // remove this if queue is default
    '--stop-when-empty' => null,
]);
  1. app/Console/Kernal.php 文件中,将命令添加到您的命令列表中:
protected $commands = [
    \App\Console\Commands\SendContactEmails::class
];
  1. 每分钟安排一次命令:
protected function schedule(Schedule $schedule)
{
    $schedule->command('emails:work')->everyMinute();
    // you can add ->withoutOverlapping(); if you think it won't finish in 1 minute
}
  1. 更新您的 cronjobs:
* * * * * /usr/local/bin/php /home/username/project/artisan schedule:run > /dev/null 2>&1

Source

Processing All Queued Jobs & Then Exiting

The --stop-when-empty option may be used to instruct the worker to process all jobs and then exit gracefully. This option can be useful when working Laravel queues within a Docker container if you wish to shutdown the container after the queue is empty:

php artisan queue:work --stop-when-empty