首页 文章

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

提问于
浏览
5

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

  • 我已经设置了一个cron作业,每分钟运行'php artisan queue:work' .

  • 我正在使用数据库驱动程序

  • 这些工作很好地进入了工作表

  • 如果我使用的是同步驱动程序,它可以在我的共享主机上运行在我的开发人员计算机上,它同时适用于同步和数据库驱动程序 .

  • 我没有下降Laravel Queue: How to use on shared hosting提供了足够的答案,因为我有cron的可能性,因此我想让它使用数据库驱动程序

  • php是来自cli和web界面的5.5版本 .

php artisan队列:在我的共享主机上工作(通过cron)返回

[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;
}

那是失败的傻瓜 .

我怎样才能让它发挥作用?

2 回答

  • 0

    Comyn在freenode的#laravel回答

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

    php -d register_argc_argv=On artisan queue:work
    
  • 10

    从Laravel 5.7开始,有一个新的队列命令在空时停止工作:

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

    由于这只是针对大多数电子邮件或少量工作,我把它放在cronjob上运行每分钟 . 对于我说的每分钟20多个工作,这不是真正的解决方案,但适用于我的电子邮件 . 这将每分钟运行大约5秒钟,仅发送电子邮件,具体取决于电子邮件的数量 .

    步骤

    • 创建新命令: $ php artisan make:command SendContactEmails

    • SendContactEmails.php 中,更改: protected $signature = 'emails:work';

    • handle() 方法中,添加:

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

    Source

    处理所有排队作业然后退出--stop-when-empty选项可用于指示工作人员处理所有作业,然后正常退出 . 如果您希望在队列为空后关闭容器,则在Docker容器中运行Laravel队列时此选项很有用:php artisan queue:work --stop-when-empty

相关问题