-
-
Notifications
You must be signed in to change notification settings - Fork 84
Expand file tree
/
Copy pathEngine.php
More file actions
170 lines (140 loc) · 5.22 KB
/
Engine.php
File metadata and controls
170 lines (140 loc) · 5.22 KB
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
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
<?php
declare(strict_types=1);
/**
* Copyright (c) 2024 Kai Sassnowski
*
* For the full copyright and license information, please view
* the LICENSE file that was distributed with this source code.
*
* @see https://github.com/roach-php/roach
*/
namespace RoachPHP\Core;
use RoachPHP\Downloader\Downloader;
use RoachPHP\Events\RequestDropped;
use RoachPHP\Events\RequestScheduling;
use RoachPHP\Events\RunFinished;
use RoachPHP\Events\RunStarting;
use RoachPHP\Extensions\ScrapedItemCollectorExtension;
use RoachPHP\Http\Request;
use RoachPHP\Http\Response;
use RoachPHP\ItemPipeline\ItemInterface;
use RoachPHP\ItemPipeline\ItemPipelineInterface;
use RoachPHP\Scheduling\RequestSchedulerInterface;
use RoachPHP\Spider\ParseResult;
use RoachPHP\Spider\Processor;
use Symfony\Component\EventDispatcher\EventDispatcherInterface;
final class Engine implements EngineInterface
{
public function __construct(
private RequestSchedulerInterface $scheduler,
private Downloader $downloader,
private ItemPipelineInterface $itemPipeline,
private Processor $responseProcessor,
private EventDispatcherInterface $eventDispatcher,
) {
}
/**
* @return array<int, ItemInterface>
*/
public function collect(Run $run): array
{
$extension = new ScrapedItemCollectorExtension();
$this->eventDispatcher->addSubscriber($extension);
$this->start($run);
$this->eventDispatcher->removeSubscriber($extension);
return $extension->getScrapedItems();
}
public function start(Run $run): void
{
$this->configure($run);
$this->eventDispatcher->dispatch(
new RunStarting($run),
RunStarting::NAME,
);
if ($this->scheduler->empty()) {
foreach ($run->startRequests as $request) {
$this->scheduleRequest($request);
}
}
$this->work($run);
$this->cleanup($run);
}
private function work(Run $run): void
{
while (!$this->scheduler->empty()) {
foreach ($this->scheduler->nextRequests($run->concurrency) as $request) {
$this->downloader->prepare($request);
}
// It's possible that requests were dropped while sending them through the
// downloader middleware pipeline. In this case, we want to keep requesting
// new requests from the scheduler until either:
//
// a) the scheduler is empty
// b) the same number of requests as the run's concurrency option
// got scheduled successfully.
//
// This is to avoid the request delay from affecting dropped requests which
// could slow down the run significantly even though very few request
// actually got sent.
$scheduledRequests = $this->downloader->scheduledRequests();
while ($scheduledRequests < $run->concurrency && !$this->scheduler->empty()) {
$difference = $run->concurrency - $scheduledRequests;
foreach ($this->scheduler->forceNextRequests($difference) as $request) {
$this->downloader->prepare($request);
}
$scheduledRequests = $this->downloader->scheduledRequests();
}
$this->downloader->flush(
fn (Response $response) => $this->onFulfilled($response),
);
}
$this->eventDispatcher->dispatch(
new RunFinished($run),
RunFinished::NAME,
);
}
private function onFulfilled(Response $response): void
{
/** @var \Generator<int, ParseResult> $parseResults */
$parseResults = $this->responseProcessor->handle($response);
foreach ($parseResults as $result) {
$result->apply(
fn (Request $request) => $this->scheduleRequest($request),
/** @phpstan-ignore argument.type */
fn (ItemInterface $item) => $this->itemPipeline->sendItem($item),
);
}
}
private function scheduleRequest(Request $request): void
{
$this->eventDispatcher->dispatch(
new RequestScheduling($request),
RequestScheduling::NAME,
);
if ($request->wasDropped()) {
$this->eventDispatcher->dispatch(
new RequestDropped($request),
RequestDropped::NAME,
);
return;
}
$this->scheduler->schedule($request);
}
private function configure(Run $run): void
{
$this->scheduler->setDelay($run->requestDelay);
$this->scheduler->setNamespace($run->namespace);
$this->itemPipeline->setProcessors(...$run->itemProcessors);
$this->downloader->withMiddleware(...$run->downloaderMiddleware);
$this->responseProcessor->withMiddleware(...$run->responseMiddleware);
foreach ($run->extensions as $extension) {
$this->eventDispatcher->addSubscriber($extension);
}
}
private function cleanup(Run $run): void
{
foreach ($run->extensions as $extension) {
$this->eventDispatcher->removeSubscriber($extension);
}
}
}