phplaravellaravel-jobs

How I can check whether my job calls the handle method once being dispatched?


I have the following controller:

namespace App\Controllers;

use App\Jobs\MyJob;

class MyController
{
  public function dispatchJob(int $user_id)
  {
     MyJob::dispatch($user_id);
  }

}

And I have the following job:

namespace App\Jobs;

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


use App\Model\Etable\User;

class MyJob implements ShouldQueue
{
    use Dispatchable;
    use InteractsWithQueue;
    use Queueable;

    /**
     * @var int
     */
    private $user_id;


    /**
     * @param User   $user          The user that has opted or deopted for newsletter consent
     */
    public function __construct(int $user_id)
    {
        $this->user_id       = $user_id;
    }

    public function handle(): void
    {
        //Logic Here
    }
}

And durirt development I look whether visiting the route indicating into dispatchJob controller method will dispatch the job. But I notice that my job is keep on dispatching:

[2020-03-26 17:18:04][17834] Processing: App\Jobs\MyJob
[2020-03-26 17:18:04][17835] Processing: App\Jobs\MyJob
[2020-03-26 17:18:05][17836] Processing: App\Jobs\MyJob
[2020-03-26 17:18:05][17837] Processing: App\Jobs\MyJob
[2020-03-26 17:18:06][17838] Processing: App\Jobs\MyJob
[2020-03-26 17:18:07][17839] Processing: App\Jobs\MyJob
[2020-03-26 17:18:07][17840] Processing: App\Jobs\MyJob
[2020-03-26 17:18:08][17841] Processing: App\Jobs\MyJob
[2020-03-26 17:18:08][17842] Processing: App\Jobs\MyJob
[2020-03-26 17:18:09][17843] Processing: App\Jobs\MyJob
[2020-03-26 17:18:09][17844] Processing: App\Jobs\MyJob
[2020-03-26 17:18:10][17845] Processing: App\Jobs\MyJob
[2020-03-26 17:18:11][17846] Processing: App\Jobs\MyJob
[2020-03-26 17:18:11][17847] Processing: App\Jobs\MyJob
[2020-03-26 17:18:12][17848] Processing: App\Jobs\MyJob
[2020-03-26 17:18:13][17849] Processing: App\Jobs\MyJob
[2020-03-26 17:18:13][17850] Processing: App\Jobs\MyJob
[2020-03-26 17:18:14][17851] Processing: App\Jobs\MyJob
[2020-03-26 17:18:14][17852] Processing: App\Jobs\MyJob
[2020-03-26 17:18:15][17853] Processing: App\Jobs\MyJob

The command used is the php artisan queue:listen

But no job seems to finish either sucessfully or with failure. So I want to debug the reason why. So I want to ask the following:

  1. How I can make my code to break into a breakpoint into my job so I can use Xdebug in order to debug it? I tried to launch the runner via command XDEBUG_CONFIG="idekey=VSCODE" php artisan queue:listen still my IDE fails to break into my breakpoint.

  2. Also how I can log the information of dump or of dd functions so I can see the result into my screen/console running the artisan command? I tried either looking on larave's default log: ./storage/logs/laravel-2020-03-26.log still neither on my console or on the log see any output regarding dump or dd

I tried the methods above but neither I can see the output of dd or breaking into a breakpoint.

Furthermore I tried to use dedicated log channel:

        'jobs' => [
            'driver' => 'daily',
            'path'   => storage_path('logs/jobs.log'),
            'level'  => 'debug',
            'days'   => 7,
        ],

And then logging any message with the following approach:

namespace App\Jobs;

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


use App\Model\Etable\User;

class MyJob implements ShouldQueue
{
    use Dispatchable;
    use InteractsWithQueue;
    use Queueable;

    /**
     * @var int
     */
    private $user_id;


    /**
     * @param User   $user          The user that has opted or deopted for newsletter consent
     */
    public function __construct(int $user_id)
    {
        $this->user_id       = $user_id;
    }

    public function handle(): void
    {
        Log::channel('jobs')->debug("hello");
        Log::channel('jobs')->debug(var_export($this->user,true));
        //Logic Here
    }
}

But no info has been logged into ./storage/logs/jobs-2020-03-26.log. Do you know how I can get debug info on what is being executed once I dispatch my job? What I want to achieve is to log, have a visual indication that method handle is being called.


Solution

  • A quick and easy way to check is to just log a string like that:

     public function handle(): void
     {
        Log::channel('jobs')->debug("Job Started");
        //Logic Here
        Log::channel('jobs')->debug("Job Ended");
     }
    

    Then after each line of code place Log::channel('jobs')->debug("Job ..."); commands to check where your code breaks.