PluginProbe ʕ •ᴥ•ʔ
10Web Booster – Website speed optimization, Cache & Page Speed optimizer / trunk
10Web Booster – Website speed optimization, Cache & Page Speed optimizer vtrunk
2.33.0 2.30.5 2.30.7 2.30.9 2.31.10 2.31.8 2.32.11 2.32.21 2.32.3 2.32.4 2.32.7 2.6.31 2.6.40 2.6.42 2.6.7 2.7.37 2.7.44 2.7.47 2.8.18 2.8.19 2.8.32 2.8.34 2.8.35 2.9.23 2.9.24 2.9.25 2.9.27 v2.27.4 trunk 2.0.10 2.0.11 2.0.12 2.0.13 2.0.14 2.0.15 2.0.17 2.0.18 2.0.21 2.0.22 2.0.25 2.0.26 2.0.27 2.0.3 2.0.7 2.0.9 2.10.46 2.10.65 2.10.66 2.10.68 2.11.41 2.11.42 2.11.43 2.12.15 2.12.21 2.12.22 2.12.23 2.12.26 2.13.37 2.13.40 2.13.41 2.13.42 2.13.44 2.13.45 2.13.47 2.14.49 2.14.50 2.15.18 2.17.21 2.17.23 2.18.17 2.19.44 2.19.45 2.19.46 2.19.49 2.2.12 2.2.15 2.2.16 2.2.18 2.2.8 2.20.31 2.20.32 2.20.33 2.21.11 2.21.12 2.21.16 2.21.25 2.22.32 2.23.13 2.23.15 2.23.16 2.23.18 2.24.12 2.24.14 2.24.18 2.25.14 2.26.6 2.28.10 2.28.13 2.28.14 2.28.7 2.29.1 2.29.2 2.29.3 2.3.0 2.3.1 2.3.2 2.3.3 2.30.18
tenweb-speed-optimizer / vendor / 10web-utils / 10web-queue / src / TenWebQueue / QueueContext.php
tenweb-speed-optimizer / vendor / 10web-utils / 10web-queue / src / TenWebQueue Last commit date
Abstracts 4 years ago DTO 4 years ago Examples 4 years ago Exceptions 4 years ago Interfaces 4 years ago Traits 3 years ago QueueContext.php 4 years ago QueueOrchestrator.php 4 years ago
QueueContext.php
245 lines
1 <?php
2 namespace TenWebQueue;
3
4 use Enqueue\Fs\FsConnectionFactory;
5 use Interop\Queue\Context;
6 use Interop\Queue\Queue;
7 use TenWebQueue\DTO\QueueConfigDTO;
8 use TenWebQueue\DTO\QueueDataDTO;
9 use TenWebQueue\Exceptions\QueueException;
10 use TenWebQueue\Interfaces\QueueConsumerInterface;
11 use TenWebQueue\Interfaces\QueueProducerInterface;
12 use TenWebQueue\Traits\QueueRestart;
13
14
15 class QueueContext
16 {
17 use QueueRestart;
18
19 /**
20 * @var Context $context
21 */
22 private $context = null;
23 /**
24 * @var Queue $queue
25 */
26 private $queue;
27 /**
28 * @var QueueProducerInterface $producer
29 */
30 private $producer;
31
32 /**
33 * @var QueueConsumerInterface $consumer
34 */
35 private $consumer;
36
37 /**
38 * @var QueueConfigDTO $config
39 */
40 private $config;
41
42 /**
43 * @var string[]
44 */
45 private $connectionTypes = ['file'];
46
47
48 /**
49 * @param QueueConfigDTO $config
50 *
51 * @throws QueueException
52 */
53 private function __construct($config)
54 {
55 $this->config = $config;
56 $this->setStartTime(microtime(true));
57 $this->purgeQueue();
58 $this->createContext();
59 }
60
61 /**
62 * @param $producerClass
63 * @param $consumerClass
64 *
65 * @return void
66 */
67 public function setUpQueue($producerClass, $consumerClass)
68 {
69 $this->createQueue();
70
71 $this->createProducer($producerClass);
72 $this->createConsumer($consumerClass);
73 }
74
75 /**
76 * @return void
77 */
78 public function unSetQueue()
79 {
80 $this->queue = null;
81 $this->producer = null;
82 $this->consumer = null;
83 }
84
85
86 /**
87 * @param QueueConfigDTO $config
88 * @param $producerClass
89 * @param $consumerClass
90 *
91 * @return QueueContext
92 * @throws QueueException
93 */
94 public static function getInstance($config, $producerClass, $consumerClass)
95 {
96 $instance = self::readObjectFromFile($config->restartFilePath);
97 if (!($instance instanceof self) || $config->force) {
98 $instance = new self($config);
99 }
100 $instance->setUpQueue($producerClass, $consumerClass);
101
102 return $instance;
103 }
104
105 /**
106 * @return void
107 * @throws QueueException
108 */
109 public function createContext()
110 {
111 if (!in_array($this->config->connectionType, $this->connectionTypes)) {
112 throw new QueueException('Unknown connection type');
113 }
114 $connectionFactory = null;
115 if ($this->config->connectionType === 'file') {
116 if (!is_dir($this->config->fileConnectionPath)) {
117 throw new QueueException('Incorrect connection file path');
118 }
119 $connectionFactory = new FsConnectionFactory($this->config->fileConnectionPath);
120 }
121
122 if ($connectionFactory) {
123 $this->context = $connectionFactory->createContext();
124 }
125
126 }
127
128 /**
129 * @return void
130 */
131 public function createQueue()
132 {
133 $this->queue = $this->context->createQueue($this->config->queueData->name);
134 }
135
136 /**
137 * @return void
138 */
139 public function purgeQueue()
140 {
141 if (file_exists($this->config->fileConnectionPath . '/' . $this->config->queueData->name)) {
142 unlink($this->config->fileConnectionPath . '/' . $this->config->queueData->name);
143 }
144 if (file_exists($this->config->restartFilePath)) {
145 unlink($this->config->restartFilePath);
146 }
147 }
148
149 /**
150 * @param $producerClass
151 *
152 * @return void
153 */
154 public function createProducer($producerClass)
155 {
156 $this->producer = new $producerClass($this);
157 }
158
159 /**
160 * @param $consumerClass
161 *
162 * @return void
163 */
164 public function createConsumer($consumerClass)
165 {
166 $this->consumer = new $consumerClass($this);
167 }
168
169 /**
170 * @param array $data
171 *
172 * @return void
173 */
174 public function enqueue($data)
175 {
176 if ($this->config->queueData->multipleItems) {
177 $this->producer->enqueueMany($data);
178 } else {
179 $this->producer->enqueueOne($data);
180 }
181 }
182
183 /**
184 * @return void
185 */
186 public function dequeue($single = false)
187 {
188 if ($single) {
189 $this->consumer->run();
190 } else {
191 $count = 1;
192 while ($this->consumer->run()) {
193 if ($this->ifRestartNeeded($count, $this->config->itemsCountForRestart)) {
194 $this->unSetQueue();
195 self::writeObjectInfile($this, $this->config->restartFilePath);
196 $this->restart($this->config->restartRoute, $this->config->restartBody);
197 }
198 $count++;
199 }
200 $this->purgeQueue();
201 $this->consumer->finish();
202 }
203 }
204
205 /**
206 * @return Queue
207 */
208 public function getQueue()
209 {
210 return $this->queue;
211 }
212
213 /**
214 * @return QueueProducerInterface
215 */
216 public function getProducer()
217 {
218 return $this->producer;
219 }
220
221 /**
222 * @return QueueConsumerInterface
223 */
224 public function getConsumer()
225 {
226 return $this->consumer;
227 }
228
229 /**
230 * @return QueueDataDTO
231 */
232 public function getQueueData()
233 {
234 return $this->config->queueData;
235 }
236
237 /**
238 * @return Context
239 */
240 public function getContext()
241 {
242 return $this->context;
243 }
244
245 }