1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
/** |
6
|
|
|
* File: VarnishUrlPurger.php |
7
|
|
|
* |
8
|
|
|
* @author Maciej Sławik <[email protected]> |
9
|
|
|
* @author Bartosz Kubicki <[email protected]> |
10
|
|
|
* @copyright Copyright (C) 2020 Lizard Media (http://lizardmedia.pl) |
11
|
|
|
*/ |
12
|
|
|
|
13
|
|
|
namespace LizardMedia\VarnishWarmer\Model\QueueHandler; |
14
|
|
|
|
15
|
|
|
use LizardMedia\VarnishWarmer\Api\Config\GeneralConfigProviderInterface; |
16
|
|
|
use LizardMedia\VarnishWarmer\Api\Config\PurgingConfigProviderInterface; |
17
|
|
|
use LizardMedia\VarnishWarmer\Api\ProgressHandler\QueueProgressLoggerInterface; |
18
|
|
|
use LizardMedia\VarnishWarmer\Api\QueueHandler\VarnishUrlPurgerInterface; |
19
|
|
|
use LizardMedia\VarnishWarmer\Model\Adapter\ReactPHP\ClientFactory; |
20
|
|
|
use Psr\Log\LoggerInterface; |
21
|
|
|
use React\EventLoop\Factory; |
22
|
|
|
use React\HttpClient\Response; |
23
|
|
|
use RuntimeException; |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* Class VarnishUrlPurger |
27
|
|
|
* @package LizardMedia\VarnishWarmer\Model\QueueHandler |
28
|
|
|
* @SuppressWarnings(PHPMD.LongVariable) |
29
|
|
|
*/ |
30
|
|
|
class VarnishUrlPurger extends AbstractQueueHandler implements VarnishUrlPurgerInterface |
31
|
|
|
{ |
32
|
|
|
/** |
33
|
|
|
* @var string |
34
|
|
|
*/ |
35
|
|
|
private const PROCESS_TYPE = 'PURGE'; |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* @var PurgingConfigProviderInterface |
39
|
|
|
*/ |
40
|
|
|
private $purgingConfigProvider; |
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* VarnishUrlPurger constructor. |
44
|
|
|
* @param GeneralConfigProviderInterface $configProvider |
45
|
|
|
* @param LoggerInterface $logger |
46
|
|
|
* @param QueueProgressLoggerInterface $queueProgressLogger |
47
|
|
|
* @param Factory $loopFactory |
48
|
|
|
* @param ClientFactory $clientFactory |
49
|
|
|
* @param PurgingConfigProviderInterface $purgingConfigProvider |
50
|
|
|
*/ |
51
|
|
|
public function __construct( |
52
|
|
|
GeneralConfigProviderInterface $configProvider, |
53
|
|
|
LoggerInterface $logger, |
54
|
|
|
QueueProgressLoggerInterface $queueProgressLogger, |
55
|
|
|
Factory $loopFactory, |
56
|
|
|
ClientFactory $clientFactory, |
57
|
|
|
PurgingConfigProviderInterface $purgingConfigProvider |
58
|
|
|
) { |
59
|
|
|
parent::__construct($configProvider, $logger, $queueProgressLogger, $loopFactory, $clientFactory); |
60
|
|
|
$this->purgingConfigProvider = $purgingConfigProvider; |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
/** |
64
|
|
|
* @param string $url |
65
|
|
|
* @return void |
66
|
|
|
*/ |
67
|
|
|
public function addUrlToPurge(string $url): void |
68
|
|
|
{ |
69
|
|
|
$this->urls[] = $url; |
70
|
|
|
$this->total++; |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
/** |
74
|
|
|
* @return void |
75
|
|
|
*/ |
76
|
|
|
public function purge(): void |
77
|
|
|
{ |
78
|
|
|
$this->runQueue(); |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
/** |
82
|
|
|
* @return int |
83
|
|
|
*/ |
84
|
|
|
protected function getMaxNumberOfProcesses(): int |
85
|
|
|
{ |
86
|
|
|
return $this->configProvider->getMaxConcurrentPurgeProcesses(); |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
/** |
90
|
|
|
* @return string |
91
|
|
|
*/ |
92
|
|
|
protected function getQueueProcessType(): string |
93
|
|
|
{ |
94
|
|
|
return self::PROCESS_TYPE; |
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
/** |
98
|
|
|
* |
99
|
|
|
* @param string $url |
100
|
|
|
* @return void |
101
|
|
|
*/ |
102
|
|
|
protected function createRequest(string $url): void |
103
|
|
|
{ |
104
|
|
|
$client = $this->clientFactory->create($this->loop); |
105
|
|
|
$request = $client->request('PURGE', $url, $this->buildHeaders()); |
106
|
|
|
|
107
|
|
|
$request->on('error', function (RuntimeException $exception) use ($url) { |
108
|
|
|
$this->logger->error($exception->getMessage(), [$url]); |
109
|
|
|
}); |
110
|
|
|
|
111
|
|
|
$request->on('response', function (Response $response) use ($url) { |
112
|
|
|
$responseCode = $response->getCode(); |
113
|
|
|
$responseHeaders = $response->getHeaders(); |
114
|
|
|
|
115
|
|
|
$response->on( |
116
|
|
|
'end', |
117
|
|
|
function () use ($url, $responseCode, $responseHeaders) { |
118
|
|
|
$this->counter++; |
119
|
|
|
$this->log($url, $responseCode, $responseHeaders); |
120
|
|
|
$this->logProgress(); |
121
|
|
|
} |
122
|
|
|
); |
123
|
|
|
|
124
|
|
|
$response->on('error', function (RuntimeException $exception) use ($url) { |
125
|
|
|
$this->logger->error( |
126
|
|
|
$exception->getMessage(), |
127
|
|
|
[ |
128
|
|
|
'url' => $url |
129
|
|
|
] |
130
|
|
|
); |
131
|
|
|
}); |
132
|
|
|
}); |
133
|
|
|
|
134
|
|
|
$request->end(); |
135
|
|
|
} |
136
|
|
|
|
137
|
|
|
/** |
138
|
|
|
* @return array |
139
|
|
|
*/ |
140
|
|
|
private function buildHeaders(): array |
141
|
|
|
{ |
142
|
|
|
$headers = []; |
143
|
|
|
$headers['X-Magento-Tags-Pattern'] = '.*'; |
144
|
|
|
if ($this->purgingConfigProvider->isPurgeCustomHostEnabled() |
145
|
|
|
&& $this->purgingConfigProvider->getAdditionalHostForHeader()) { |
146
|
|
|
$headers['Host'] = $this->purgingConfigProvider->getAdditionalHostForHeader(); |
147
|
|
|
} |
148
|
|
|
return $headers; |
149
|
|
|
} |
150
|
|
|
} |
151
|
|
|
|