调试拉拉维尔作业

2022-08-30 18:32:43

我正在尝试调试Laravel中队列中的作业,但没有成功。我想将输出打印到控制台中。例如您如何在其他任何地方使用。dd()

<?php

namespace App\Jobs;

use App\Image;
use Illuminate\Bus\Queueable;
use Illuminate\Queue\SerializesModels;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Foundation\Bus\Dispatchable;

class ProcessImage implements ShouldQueue
{
    use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;

    protected $image;

    /**
     * Attempt the job a maximum of twice
     *
     * @var int
     */
    public $tries = 2;

    /**
     * Create a new job instance.
     *
     * @return void
     */
    public function __construct(Image $image)
    {

        $this->image = $image;

    }

    /**
     * Execute the job.
     *
     * @return void
     */
    public function handle()
    {
        // set paths for standard and thumbnail size images

        $image          = public_path("assets" . DIRECTORY_SEPARATOR . $this->image->original);
        $product_id     = $this->image->id;
        $product_path   = public_path("assets" . DIRECTORY_SEPARATOR . "images" . DIRECTORY_SEPARATOR .
            "products" . DIRECTORY_SEPARATOR . $product_id);


        $thumbnail_path = $product_path . DIRECTORY_SEPARATOR . "thumbnail";

        if(!is_dir($product_path))
            mkdir($product_path);

        if(!is_dir($thumbnail_path))
            mkdir($thumbnail_path);

        // Resize and save the standard image

        $standard = \Image::make($image)->resize(450, 450, function($constraint)
        {
            $constraint->aspectRatio();
        })->save($product_path);

        dd($standard);

    }
}

答案 1

1) 尝试 - 如果您将队列作为守护程序运行,则每次代码更改时,当守护程序将代码加载到内存中时,都需要重新启动侦听器。php artisan queue:restart

2)并且应该在队列中工作。确保逐步调试 - 在作业开始时记录,然后稍微降低,仍然降低,等等,看看你注销的最后一点是什么。var_dump()dd()Log::info()

3)检查存储/日志下的laravel.log - 错误应该在某个地方注销。


答案 2

如果你想用PHP Storm编辑文件调试Laravel Job:.env

QUEUE_DRIVER=sync

在函数中放置一个断点。handle()


推荐