31

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

    }
}
Ian
  • 3,539
  • 4
  • 27
  • 48
  • If you want to debug the code you will need to assemble your command string that starts artisan accordingly like so - php -dxdebug.remote_autostart=1 artisan queue:work (source: https://medium.com/@kebing.yu/make-xdebug-work-with-laravel-queue-b5e73e44a36b) – Daniel Protopopov Apr 14 '18 at 15:06
  • use Laravel telescope – Rachid Loukili Sep 16 '21 at 11:30

6 Answers6

21

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.

Criss
  • 952
  • 5
  • 17
9

If you want to debug Laravel Job with PHP Storm edit .env file:

QUEUE_DRIVER=sync

Put a breakpoint in the handle() function.

yesnik
  • 4,085
  • 2
  • 30
  • 25
5

you don't need to print it on the console. full description of fail job is store in the exception column.(table name fail_jobs).
enter image description here

Syscall
  • 19,327
  • 10
  • 37
  • 52
FAZAL HAKIM
  • 89
  • 1
  • 5
  • Mine was empty despite having spent days of purposely failing and debugging the jobs, so this doesn't seem to work. – Hashim Aziz Feb 27 '22 at 21:08
3

What I do is I Log information that needs to be seen, like in your case:

\Log::info('Making new directory');

Or

\Log::info('this is new image: ', [$standard]);

and so on. just open the log info and see where the code breaks or the condition that should be working didn't work.

Muhammad Nauman
  • 1,249
  • 8
  • 10
0

I did it in two ways:

  1. Using Cache facade with method put(). And next, you can get this with Cache::get($cacheKey);. Here I can pass any data type: Collections, arrays, integer, string, etc.

  2. Using Log facade with method debug(). The First parameter is the message and the second is context only with array type.

The first variant is easier for debugging various data, but the second is easier for getting debug info. I prefer the first variant :)

Maxim Paladi
  • 91
  • 1
  • 7
0

I use print_r() which can do the job.

Thomas Lo
  • 76
  • 1
  • 6