Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Debugging Laravel jobs

Tags:

php

laravel

jobs

I am trying to debug jobs on a queue in Laravel but to no success. I want to print output into the console. Such as how you use dd() everywhere else.

<?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);

    }
}
like image 832
Ian Avatar asked Apr 14 '18 14:04

Ian


People also ask

How do I debug a job in Laravel?

You can debug jobs easily by setting the queue driver to sync. Instead of being queued they will run synchronously. Then you can use xDebug.

What is debugging in Laravel?

One of the often-used queries is Laravel Debug. It is an application, which is a part of the Laravel Framework, that helps the developer to keep a tab on the errors and bugs that may appear during the development phase. It is a handy tool since it points out the errors while the application is being developed.

How do I fail a job in Laravel?

Use the InteractsWithQueue trait and call either delete() if you want to delete the job, or fail($exception = null) if you want to fail it. Failing the job means it will be deleted, logged into the failed_jobs table and the JobFailed event is triggered.


1 Answers

1) Try php artisan queue:restart - if you're running queue as a daemon, you need to restart listener every time your code changes as the daemon loads code into memory.

2) var_dump() dd() and Log::info() should be working in queues. Make sure you debug gradually - log at the very beginning of job, then a little lower, lower still, etc. and see what's the last point you get logged out.

3) Check laravel.log under storage/logs - the error should be logged out somewhere.

like image 189
Criss Avatar answered Sep 22 '22 08:09

Criss