Skip to content

Commit d085c0b

Browse files
authored
[9.x] Improves queue:work command output (#44971)
* Improves command work output * Removes non needed code * Avoids non-necessary variable
1 parent 761c35d commit d085c0b

File tree

2 files changed

+26
-23
lines changed

2 files changed

+26
-23
lines changed

src/Illuminate/Queue/Console/WorkCommand.php

Lines changed: 25 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
use Illuminate\Queue\WorkerOptions;
1414
use Illuminate\Support\Carbon;
1515
use Symfony\Component\Console\Attribute\AsCommand;
16+
use Symfony\Component\Console\Terminal;
1617
use function Termwind\terminal;
1718

1819
#[AsCommand(name: 'queue:work')]
@@ -80,13 +81,6 @@ class WorkCommand extends Command
8081
*/
8182
protected $latestStartedAt;
8283

83-
/**
84-
* Holds the status of the last processed job, if any.
85-
*
86-
* @var string|null
87-
*/
88-
protected $latestStatus;
89-
9084
/**
9185
* Create a new queue work command.
9286
*
@@ -126,9 +120,11 @@ public function handle()
126120
// connection being run for the queue operation currently being executed.
127121
$queue = $this->getQueue($connection);
128122

129-
$this->components->info(
130-
sprintf('Processing jobs from the [%s] %s.', $queue, str('queue')->plural(explode(',', $queue)))
131-
);
123+
if (Terminal::hasSttyAvailable()) {
124+
$this->components->info(
125+
sprintf('Processing jobs from the [%s] %s.', $queue, str('queue')->plural(explode(',', $queue)))
126+
);
127+
}
132128

133129
return $this->runWorker(
134130
$connection, $queue
@@ -208,28 +204,37 @@ protected function listenForEvents()
208204
*/
209205
protected function writeOutput(Job $job, $status)
210206
{
207+
$this->output->write(sprintf(
208+
' <fg=gray>%s</> %s%s',
209+
Carbon::now()->format('Y-m-d H:i:s'),
210+
$job->resolveName(),
211+
$this->output->isVerbose()
212+
? sprintf(' <fg=gray>%s</>', $job->getJobId())
213+
: ''
214+
));
215+
211216
if ($status == 'starting') {
212217
$this->latestStartedAt = microtime(true);
213-
$this->latestStatus = $status;
214218

215-
$formattedStartedAt = Carbon::now()->format('Y-m-d H:i:s');
219+
$dots = max(terminal()->width() - mb_strlen($job->resolveName()) - (
220+
$this->output->isVerbose() ? (mb_strlen($job->getJobId()) + 1) : 0
221+
) - 33, 0);
216222

217-
return $this->output->write(" <fg=gray>{$formattedStartedAt}</> {$job->resolveName()}");
218-
}
223+
$this->output->write(' '.str_repeat('<fg=gray>.</>', $dots));
219224

220-
if ($this->latestStatus && $this->latestStatus != 'starting') {
221-
$formattedStartedAt = Carbon::createFromTimestamp($this->latestStartedAt)->format('Y-m-d H:i:s');
222-
223-
$this->output->write(" <fg=gray>{$formattedStartedAt}</> {$job->resolveName()}");
225+
return $this->output->writeln(' <fg=yellow;options=bold>RUNNING</>');
224226
}
225227

226228
$runTime = number_format((microtime(true) - $this->latestStartedAt) * 1000, 2).'ms';
227-
$dots = max(terminal()->width() - mb_strlen($job->resolveName()) - mb_strlen($runTime) - 31, 0);
229+
230+
$dots = max(terminal()->width() - mb_strlen($job->resolveName()) - (
231+
$this->output->isVerbose() ? (mb_strlen($job->getJobId()) + 1) : 0
232+
) - mb_strlen($runTime) - 31, 0);
228233

229234
$this->output->write(' '.str_repeat('<fg=gray>.</>', $dots));
230235
$this->output->write(" <fg=gray>$runTime</>");
231236

232-
$this->output->writeln(match ($this->latestStatus = $status) {
237+
$this->output->writeln(match ($status) {
233238
'success' => ' <fg=green;options=bold>DONE</>',
234239
'released_after_exception' => ' <fg=yellow;options=bold>FAIL</>',
235240
default => ' <fg=red;options=bold>FAIL</>',

src/Illuminate/Queue/Listener.php

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -173,9 +173,7 @@ protected function createCommand($connection, $queue, ListenerOptions $options)
173173
public function runProcess(Process $process, $memory)
174174
{
175175
$process->run(function ($type, $line) {
176-
if (! str($line)->contains('Processing jobs from the')) {
177-
$this->handleWorkerOutput($type, $line);
178-
}
176+
$this->handleWorkerOutput($type, $line);
179177
});
180178

181179
// Once we have run the job we'll go check if the memory limit has been exceeded

0 commit comments

Comments
 (0)