3

I've created a simple laravel queue, a controller with a method which dispatches jobs into the queue, and a job for handling logic.

My plan is to have multiple queue workers using Supervisor config. And it works fine.

Supervisor config:

/etc/supervisor/conf.d/worker-node.conf
[program:laravel-worker]

...

autostart=true

autorestart=true

stopasgroup=true

killasgroup=true

user=admin

numprocs=8 //8 concurrent workers

...

Problem is that i want to log whenever a worker takes on a job from the queue and starts processing it. It's actually easy, i can just do some Log::info(...) but i also want to log the worker's unique identifier. It could be process id, worker id, worker number, whatever is possible. I want to do this so i can inspect which worker handled the job. Is such thing possible in laravel ? I know worker processes are daemon, but i think it could be possible to get process id somehow. Expected log output:

laravel.log

[2021-09-28 14:12:54] local.INFO: [worker identifier here] started processing JOB id 156
[2021-09-28 14:12:54] local.INFO: [worker identifier here] started processing JOB id 187
[2021-09-28 14:12:54] local.INFO: [worker identifier here] started processing JOB id 1214

Job class:

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

    protected $fetchLocation;

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

        $this->fetchLocation->execute();
    }

    /**
     * Handle a job failure.
     *
     * @param  \Throwable  $exception
     * @return void
     */
    public function failed(Throwable $exception)
    {
        $jobId=''; //Some JOB ID or PID is needed here 

       Log::error("job id {$jobId} failed: {$exception->getMessage()}" );
    }
}

TL;DR: How can i get worker's PID or any other identifier inside job's handle() method.

Any thought and alternatives are appreciated. Thank you.

Edit #1: Added job class

Skywarth
  • 623
  • 2
  • 9
  • 26
  • In future, please be sure to provide code in your questions – in this case a minimal version of your job class would be helpful to see. – miken32 Sep 28 '21 at 19:38

0 Answers0