0

In my Laravel app, I want to add the job execution time to each job's log. To do this I am subtracting LARAVEL_START from the current microtime() like this:

$data['meta']['requestDuration'] = round((microtime(true) - LARAVEL_START) * 1000, 2);

While working on it, I only ran one queue worker with php artisan queue:listen, which gave me the expected result of about 450ms.

Running the same code in production with, for example, five queue workers results in times stored, such as 19517ms. I also noticed that depending on how long the workers run, the time is increased as well.

Karl Hill
  • 12,937
  • 5
  • 58
  • 95
  • 1
    The queue worker starts once and processes many jobs. Use another way to find the runtime. – apokryfos Jul 16 '23 at 07:45
  • @apokryfos but why is `php artisan queue:listen` working then? – Stanley Richter Jul 16 '23 at 07:55
  • 1
    What is `LARAVEL_START`? What does it contain? – brombeer Jul 16 '23 at 08:01
  • 1
    @brombeer It's a constant defined by Laravel itself at boot containg `microtime()`. – Stanley Richter Jul 16 '23 at 08:10
  • 1
    `queue:listen` is different to `queue:work` in that `queue:listen` is used mainly for local testing and restarts after each task is processed. Differences are touched on [here](https://laravel.com/docs/10.x/queues#running-the-queue-worker) – apokryfos Jul 16 '23 at 09:13
  • @apokryfos Since I moved the "execution start" into the job itself now, you should make your first comment an answer as it basically reflects what I did. – Stanley Richter Jul 16 '23 at 09:24
  • 3
    @StanleyRichter my comment suggested you seek another way to do it. You are the one who actually did that so I think you should share the code you used. I think my comment is a bit too broad to make it an answer – apokryfos Jul 16 '23 at 10:10

0 Answers0