From c60191f33a88f91685634b157b4dc515381fccd1 Mon Sep 17 00:00:00 2001 From: Michael Dyrynda Date: Fri, 2 Jun 2023 04:17:36 +0930 Subject: [PATCH] [10.x] Display queue runtime in human readable format (#47227) * 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 --- src/Illuminate/Queue/Console/WorkCommand.php | 18 +++++++++++++++++- 1 file changed, 17 insertions(+), 1 deletion(-) diff --git a/src/Illuminate/Queue/Console/WorkCommand.php b/src/Illuminate/Queue/Console/WorkCommand.php index 2cf44161b8d5..b4ed14443f62 100644 --- a/src/Illuminate/Queue/Console/WorkCommand.php +++ b/src/Illuminate/Queue/Console/WorkCommand.php @@ -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; @@ -216,7 +217,7 @@ protected function writeOutput(Job $job, $status) return $this->output->writeln(' 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 @@ -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. *