Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions bin/crawl.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
$urls = extract_urls_recursively($url, $depth, 'extract_unique_urls');
break;
case 'worker':
predis()->del('visited');
$urls = extract_urls_with_worker($url, $depth, uniqid());
break;
case 'promise':
Expand Down
2 changes: 1 addition & 1 deletion bin/worker.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ function blpop(string $key): string {
// upravit kód tak, aby nedocházelo ke ztrátě dat
// if (rand(0,1)) exit(1);

foreach (extract_urls($url) as $u) {
foreach (worker_extract_unique_urls($url) as $u) {
predis()->rpush($results, json_encode($u));

if ($depth > 0) {
Expand Down
9 changes: 9 additions & 0 deletions src/functions.php
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,15 @@ function predis(): Predis\Client {
return $client ?? $client = new Predis\Client('tcp://redis');
}

// extrahuje pouze unikátní URL ze zadané adresy
function worker_extract_unique_urls(string $url): Generator {
foreach (extract_urls($url) as $u) {
if (predis()->hsetnx('visited', md5($u), 1)) {
yield $u;
}
}
}

// přidá zadanou adresu do fronty pro zpracování workerem
function extract_urls_with_worker(string $url, int $depth, string $results): Generator {
predis()->rpush('queue', json_encode([$url, $depth, $results]));
Expand Down