-
Notifications
You must be signed in to change notification settings - Fork 1
/
WorkCommand.php
56 lines (45 loc) · 1.6 KB
/
WorkCommand.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
<?php
namespace App\Console\Commands;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Console\Output\OutputInterface;
use Illuminate\Events\Dispatcher;
use Illuminate\Queue\Worker;
use App\Console\Exceptions\Handler;
use Illuminate\Queue\WorkerOptions;
/**
* derived from https://gist.github.com/h-collector/626c1035a0ddffef28fce4b3bc247e70
*/
class WorkCommand extends Command
{
protected function configure()
{
$this
->setName('queue:work')
->setDescription('Run the Queue Worker instance');
}
protected function execute(InputInterface $input, OutputInterface $output)
{
global $queue;
$worker = new Worker($queue->getQueueManager(), new Dispatcher(), new Handler());
$connection = 'default';
$queue = 'default';
while (true) {
try {
$result = $worker->runNextJob($connection, $queue, new WorkerOptions(0));
// Job processed
if ($result['job']) {
$output->writeln("Job processed: %s, result: %s\n",
$result['job']->getJobId(),
$result['failed'] ? 'Failed' : 'Success');
}
} catch (Exception $e) {
$output->writeln($e->getTraceAsString());
} catch (Throwable $e) {
$output->writeln($e->getTraceAsString());
}
}
}
}