Skip to content

Commit

Permalink
[10.x] Display queue runtime in human readable format (#47227)
Browse files Browse the repository at this point in the history
* convert milliseconds to human readable timestamps using Carbon

* Correct runtime check for formatting with Carbon

* separate calculation of runtime from formatting, otherwise we run into type issues with string / int comparison

* formatting

* Update WorkCommand.php

---------

Co-authored-by: Taylor Otwell <taylor@laravel.com>
  • Loading branch information
michaeldyrynda and taylorotwell committed Jun 1, 2023
1 parent 4de531b commit c60191f
Showing 1 changed file with 17 additions and 1 deletion.
18 changes: 17 additions & 1 deletion src/Illuminate/Queue/Console/WorkCommand.php
Expand Up @@ -2,6 +2,7 @@

namespace Illuminate\Queue\Console;

use Carbon\CarbonInterval;
use Illuminate\Console\Command;
use Illuminate\Contracts\Cache\Repository as Cache;
use Illuminate\Contracts\Queue\Job;
Expand Down Expand Up @@ -216,7 +217,7 @@ protected function writeOutput(Job $job, $status)
return $this->output->writeln(' <fg=yellow;options=bold>RUNNING</>');
}

$runTime = number_format((microtime(true) - $this->latestStartedAt) * 1000, 2).'ms';
$runTime = $this->formatRunTime($this->latestStartedAt);

$dots = max(terminal()->width() - mb_strlen($job->resolveName()) - (
$this->output->isVerbose() ? (mb_strlen($job->getJobId()) + 1) : 0
Expand Down Expand Up @@ -249,6 +250,21 @@ protected function now()
return Carbon::now();
}

/**
* Given a start time, format the total run time for human readability.
*
* @param float $startTime
* @return string
*/
protected function formatRunTime($startTime)
{
$runTime = (microtime(true) - $startTime) * 1000;

return $runTime > 1000
? CarbonInterval::milliseconds($runTime)->cascade()->forHumans(short: true)
: number_format($runTime, 2).'ms';
}

/**
* Store a failed job event.
*
Expand Down

0 comments on commit c60191f

Please sign in to comment.