Skip to content

Commit ace7f04

Browse files
committed
Use events, duh.
1 parent 466ffb6 commit ace7f04

File tree

1 file changed

+23
-7
lines changed

1 file changed

+23
-7
lines changed

src/Illuminate/Queue/Console/WorkCommand.php

Lines changed: 23 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@
66
use Illuminate\Queue\Worker;
77
use Illuminate\Console\Command;
88
use Illuminate\Contracts\Queue\Job;
9+
use Illuminate\Queue\Events\JobFailed;
10+
use Illuminate\Queue\Events\JobProcessed;
911
use Symfony\Component\Console\Input\InputOption;
1012
use Symfony\Component\Console\Input\InputArgument;
1113

@@ -56,6 +58,11 @@ public function fire()
5658
return $this->worker->sleep($this->option('sleep'));
5759
}
5860

61+
// We'll listen to the processed and failed events so we can write information
62+
// to the console as jobs are processed, which will let the developer watch
63+
// which jobs are coming through a queue and be informed on its progress.
64+
$this->listenForEvents();
65+
5966
$queue = $this->option('queue');
6067

6168
$delay = $this->option('delay');
@@ -67,16 +74,25 @@ public function fire()
6774

6875
$connection = $this->argument('connection');
6976

70-
$response = $this->runWorker(
77+
$this->runWorker(
7178
$connection, $queue, $delay, $memory, $this->option('daemon')
7279
);
80+
}
7381

74-
// If a job was fired by the worker, we'll write the output out to the console
75-
// so that the developer can watch live while the queue runs in the console
76-
// window, which will also of get logged if stdout is logged out to disk.
77-
if (! is_null($response['job'])) {
78-
$this->writeOutput($response['job'], $response['failed']);
79-
}
82+
/**
83+
* Listen for the queue events in order to update the console output.
84+
*
85+
* @return void
86+
*/
87+
protected function listenForEvents()
88+
{
89+
$this->laravel['events']->listen(JobProcessed::class, function ($event) {
90+
$this->writeOutput($event->job, false);
91+
});
92+
93+
$this->laravel['events']->listen(JobFailed::class, function ($event) {
94+
$this->writeOutput($event->job, true);
95+
});
8096
}
8197

8298
/**

0 commit comments

Comments
 (0)