1 | <?php |
||
10 | class JobManager extends AbstractJobManager |
||
11 | { |
||
12 | /** @var AMQPChannel */ |
||
13 | protected $channel; |
||
14 | |||
15 | /** @var AbstractConnection */ |
||
16 | protected $connection; |
||
17 | protected $queueArgs; |
||
18 | protected $exchangeArgs; |
||
19 | |||
20 | protected $channelSetup = false; |
||
21 | |||
22 | protected $hostname; |
||
23 | protected $pid; |
||
24 | protected $maxPriority; |
||
25 | |||
26 | public function __construct() |
||
31 | |||
32 | /** |
||
33 | * @param string $exchange |
||
34 | * @param string $type |
||
35 | * @param bool $passive |
||
36 | * @param bool $durable |
||
37 | * @param bool $autoDelete |
||
38 | */ |
||
39 | public function setExchangeArgs($exchange, $type, $passive, $durable, $autoDelete) |
||
43 | |||
44 | /** |
||
45 | * @param string $queue |
||
46 | * @param bool $passive |
||
47 | * @param bool $durable |
||
48 | * @param bool $exclusive |
||
49 | * @param bool $autoDelete |
||
50 | * @param int $maxPriority |
||
51 | */ |
||
52 | public function setQueueArgs($queue, $passive, $durable, $exclusive, $autoDelete, $maxPriority) |
||
65 | |||
66 | public function setAMQPConnection(AbstractConnection $connection) |
||
71 | |||
72 | /** |
||
73 | * @return AMQPChannel |
||
74 | */ |
||
75 | public function getChannel() |
||
79 | |||
80 | 5 | public function setupChannel() |
|
81 | { |
||
82 | 5 | if (empty($this->queueArgs)) { |
|
83 | throw new \Exception(__METHOD__.': queue args need to be set via setQueueArgs(...)'); |
||
84 | } |
||
85 | 5 | if (empty($this->exchangeArgs)) { |
|
86 | throw new \Exception(__METHOD__.': exchange args need to be set via setExchangeArgs(...)'); |
||
87 | } |
||
88 | |||
89 | 5 | if (!$this->channelSetup) { |
|
90 | call_user_func_array([$this->channel, 'exchange_declare'], $this->exchangeArgs); |
||
91 | if ($this->maxPriority) { |
||
92 | array_push($this->queueArgs, false); |
||
93 | array_push($this->queueArgs, ['x-max-priority' => ['I', intval($this->maxPriority)]]); |
||
94 | } |
||
95 | call_user_func_array([$this->channel, 'queue_declare'], $this->queueArgs); |
||
96 | $this->channel->queue_bind($this->queueArgs[0], $this->exchangeArgs[0]); |
||
97 | $this->channelSetup = true; |
||
98 | } |
||
99 | 5 | } |
|
100 | |||
101 | 5 | public function save(\Dtc\QueueBundle\Model\Job $job) |
|
102 | { |
||
103 | 5 | $this->setupChannel(); |
|
104 | 5 | if (!$job->getId()) { |
|
105 | 5 | $job->setId(uniqid($this->hostname.'-'.$this->pid, true)); |
|
106 | 5 | } |
|
107 | |||
108 | 5 | if (null !== ($priority = $job->getPriority()) && !$this->maxPriority) { |
|
109 | throw new \Exception('This queue does not support priorities'); |
||
110 | } |
||
111 | |||
112 | 5 | $msg = new AMQPMessage($job->toMessage()); |
|
|
|||
113 | |||
114 | 5 | if ($this->maxPriority) { |
|
115 | 5 | $priority = null === $priority ? 0 : $this->maxPriority - $priority; |
|
116 | 5 | $msg->set('priority', $priority); |
|
117 | 5 | } |
|
118 | 5 | $this->channel->basic_publish($msg, $this->exchangeArgs[0]); |
|
119 | |||
120 | 5 | return $job; |
|
121 | } |
||
122 | |||
123 | /** |
||
124 | * @param string $workerName |
||
125 | */ |
||
126 | 5 | public function getJob($workerName = null, $methodName = null, $prioritize = true, $runId = null) |
|
155 | |||
156 | // Save History get called upon completion of the job |
||
157 | public function saveHistory(\Dtc\QueueBundle\Model\Job $job) |
||
167 | |||
168 | public function __destruct() |
||
172 | } |
||
173 |
If you implement
__call
and you know which methods are available, you can improve IDE auto-completion and static analysis by adding a @method annotation to the class.This is often the case, when
__call
is implemented by a parent class and only the child class knows which methods exist: