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
|
|
|
*/ |
29
|
|
|
class VarnishUrlPurger extends AbstractQueueHandler implements VarnishUrlPurgerInterface |
30
|
|
|
{ |
31
|
|
|
/** |
32
|
|
|
* @var string |
33
|
|
|
*/ |
34
|
|
|
private const PROCESS_TYPE = 'PURGE'; |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* @var PurgingConfigProviderInterface |
38
|
|
|
*/ |
39
|
|
|
private $purgingConfigProvider; |
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* VarnishUrlPurger constructor. |
43
|
|
|
* @param GeneralConfigProviderInterface $configProvider |
44
|
|
|
* @param LoggerInterface $logger |
45
|
|
|
* @param QueueProgressLoggerInterface $queueProgressLogger |
46
|
|
|
* @param Factory $loopFactory |
47
|
|
|
* @param ClientFactory $clientFactory |
48
|
|
|
* @param PurgingConfigProviderInterface $purgingConfigProvider |
49
|
|
|
*/ |
50
|
|
|
public function __construct( |
51
|
|
|
GeneralConfigProviderInterface $configProvider, |
52
|
|
|
LoggerInterface $logger, |
53
|
|
|
QueueProgressLoggerInterface $queueProgressLogger, |
54
|
|
|
Factory $loopFactory, |
55
|
|
|
ClientFactory $clientFactory, |
56
|
|
|
PurgingConfigProviderInterface $purgingConfigProvider |
57
|
|
|
) { |
58
|
|
|
parent::__construct($configProvider, $logger, $queueProgressLogger, $loopFactory, $clientFactory); |
59
|
|
|
$this->purgingConfigProvider = $purgingConfigProvider; |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
/** |
63
|
|
|
* @param string $url |
64
|
|
|
* @return void |
65
|
|
|
*/ |
66
|
|
|
public function addUrlToPurge(string $url): void |
67
|
|
|
{ |
68
|
|
|
$this->urls[] = $url; |
69
|
|
|
$this->total++; |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
/** |
73
|
|
|
* @return void |
74
|
|
|
*/ |
75
|
|
|
public function purge(): void |
76
|
|
|
{ |
77
|
|
|
$this->runQueue(); |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
/** |
81
|
|
|
* @return int |
82
|
|
|
*/ |
83
|
|
|
protected function getMaxNumberOfProcesses(): int |
84
|
|
|
{ |
85
|
|
|
return $this->configProvider->getMaxConcurrentPurgeProcesses(); |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
/** |
89
|
|
|
* @return string |
90
|
|
|
*/ |
91
|
|
|
protected function getQueueProcessType(): string |
92
|
|
|
{ |
93
|
|
|
return self::PROCESS_TYPE; |
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
/** |
97
|
|
|
* |
98
|
|
|
* @param string $url |
99
|
|
|
* @return void |
100
|
|
|
*/ |
101
|
|
|
protected function createRequest(string $url): void |
102
|
|
|
{ |
103
|
|
|
$client = $this->clientFactory->create($this->loop); |
104
|
|
|
$request = $client->request('PURGE', $url, $this->buildHeaders()); |
105
|
|
|
|
106
|
|
|
$request->on('error', function (RuntimeException $exception) use ($url) { |
107
|
|
|
$this->logger->error($exception->getMessage(), [$url]); |
108
|
|
|
}); |
109
|
|
|
|
110
|
|
|
$request->on('response', function (Response $response) use ($url) { |
111
|
|
|
$response->on( |
112
|
|
|
'end', |
113
|
|
|
function () use ($url) { |
114
|
|
|
$this->counter++; |
115
|
|
|
$this->log($url); |
116
|
|
|
$this->logProgress(); |
117
|
|
|
} |
118
|
|
|
); |
119
|
|
|
|
120
|
|
|
$response->on('error', function (RuntimeException $exception) use ($url) { |
121
|
|
|
$this->logger->error( |
122
|
|
|
$exception->getMessage(), |
123
|
|
|
[ |
124
|
|
|
'url' => $url |
125
|
|
|
] |
126
|
|
|
); |
127
|
|
|
}); |
128
|
|
|
}); |
129
|
|
|
|
130
|
|
|
$request->end(); |
131
|
|
|
} |
132
|
|
|
|
133
|
|
|
/** |
134
|
|
|
* @return array |
135
|
|
|
*/ |
136
|
|
|
private function buildHeaders(): array |
137
|
|
|
{ |
138
|
|
|
$headers = []; |
139
|
|
|
$headers[] = 'X-Magento-Tags-Pattern: .*'; |
140
|
|
|
if ($this->purgingConfigProvider->isPurgeCustomHostEnabled() |
141
|
|
|
&& $this->purgingConfigProvider->getAdditionalHostForHeader()) { |
142
|
|
|
$headers[] = "Host: {$this->purgingConfigProvider->getAdditionalHostForHeader()}"; |
143
|
|
|
} |
144
|
|
|
return $headers; |
145
|
|
|
} |
146
|
|
|
} |
147
|
|
|
|