【问题标题】:Laravel 5 Queued CommandLaravel 5 排队命令
【发布时间】:2015-06-30 12:36:26
【问题描述】:

我正在使用 Laravel 5 制作应用程序,目前我遇到了一个小问题(我希望如此)。 所以我编写了一个像这样实现 ShouldBeQueued 的命令:

class ImportDataCommand extends Command implements SelfHandling,ShouldBeQueued {


    use InteractsWithQueue, SerializesModels;

    public $filePath;

    /**
     * Create a new command instance.
     *
     * @return void
     */
    public function __construct($filePath)
    {
        $this->filePath = $filePath;
        Log::info('queued shit from command contructor and i have this destinationPath => '.$filePath);//working
        echo "contructor ".$this->filePath;

    }

    /**
     * Execute the command.
     *
     * @return void
     */
    public function handle()
    {


        $destinationPath = storage_path().'/app/gtfs/';


        $zip = new ZipArchive;
        echo "handle ".$this->filePath;
        /*$res = $zip->open($destinationPath.$this->filePath);
        if ($res === TRUE) {
            $zip->extractTo($destinationPath.'extracted/');
            $zip->close();
            echo 'done';

        } else {

            echo 'fail';

        }*/
    }

}

我这样称呼它:

$bus->dispatch(new ImportDataCommand($fileName));

我的问题是,当命令在队列中执行后(顺便说一句,我正在使用 beanstalkd)它会抛出异常,因为 filePath 未设置,我想我已经理解为什么会发生这种情况,当我使用我的变量将命令发送到busDispatcher 上的队列时,命令被实例化,但是当在队列端调用handle() 时,它不会发送该命令。有什么建议吗?

【问题讨论】:

    标签: php laravel command queue laravel-5


    【解决方案1】:

    您在构造函数中分配$this->filePath,然后在handle() 方法中覆盖它。

    【讨论】:

    • 很抱歉,这是一个错字已修正
    • 如果我完全按照现在的方式运行它,我会得到 [Symfony\Component\Debug\Exception\FatalErrorException] 无法访问空属性
    • 当我告诉 artisan 运行排队的作业时 php artisan queue:work
    • echo "handle".$this->filePath;
    • 如果在echo "handle ".$this->filePath;之前赋值,是否也会报错?
    【解决方案2】:

    最后我找到了兔子:我完全忘记了每次尝试刷新所有作业,所以我不明白为什么但它与命令混淆(我已经重新启动了 Vagrant 机器,但我认为这没有影响任何东西)。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2015-07-04
      • 2021-09-07
      • 2015-08-06
      • 1970-01-01
      • 1970-01-01
      • 2021-05-06
      • 2015-07-25
      相关资源
      最近更新 更多