/
ListenCommand.php
executable file
·124 lines (110 loc) · 3.63 KB
/
ListenCommand.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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
<?php
namespace Illuminate\Queue\Console;
use Illuminate\Console\Command;
use Illuminate\Queue\Listener;
use Illuminate\Queue\ListenerOptions;
class ListenCommand extends Command
{
/**
* The console command name.
*
* @var string
*/
protected $signature = 'queue:listen
{connection? : The name of connection}
{--name=default : The name of the worker}
{--delay=0 : The number of seconds to delay failed jobs (Deprecated)}
{--backoff=0 : The number of seconds to wait before retrying a job that encountered an uncaught exception}
{--force : Force the worker to run even in maintenance mode}
{--memory=128 : The memory limit in megabytes}
{--queue= : The queue to listen on}
{--sleep=3 : Number of seconds to sleep when no job is available}
{--timeout=60 : The number of seconds a child process can run}
{--tries=1 : Number of times to attempt a job before logging it failed}';
/**
* The console command description.
*
* @var string
*/
protected $description = 'Listen to a given queue';
/**
* The queue listener instance.
*
* @var \Illuminate\Queue\Listener
*/
protected $listener;
/**
* Create a new queue listen command.
*
* @param \Illuminate\Queue\Listener $listener
* @return void
*/
public function __construct(Listener $listener)
{
parent::__construct();
$this->setOutputHandler($this->listener = $listener);
}
/**
* Execute the console command.
*
* @return void
*/
public function handle()
{
// We need to get the right queue for the connection which is set in the queue
// configuration file for the application. We will pull it based on the set
// connection being run for the queue operation currently being executed.
$queue = $this->getQueue(
$connection = $this->input->getArgument('connection')
);
$this->listener->listen(
$connection, $queue, $this->gatherOptions()
);
}
/**
* Get the name of the queue connection to listen on.
*
* @param string $connection
* @return string
*/
protected function getQueue($connection)
{
$connection = $connection ?: $this->laravel['config']['queue.default'];
return $this->input->getOption('queue') ?: $this->laravel['config']->get(
"queue.connections.{$connection}.queue", 'default'
);
}
/**
* Get the listener options for the command.
*
* @return \Illuminate\Queue\ListenerOptions
*/
protected function gatherOptions()
{
$backoff = $this->hasOption('backoff')
? $this->option('backoff')
: $this->option('delay');
return new ListenerOptions(
$this->option('name'),
$this->option('env'),
$backoff,
$this->option('memory'),
$this->option('timeout'),
$this->option('sleep'),
$this->option('tries'),
$this->option('force')
);
}
/**
* Set the options on the queue listener.
*
* @param \Illuminate\Queue\Listener $listener
* @return void
*/
protected function setOutputHandler(Listener $listener)
{
$listener->setOutputHandler(function ($type, $line) {
$this->output->write($line);
});
}
}