Last active
October 3, 2023 15:53
-
-
Save nei/4ffd278c627890d3b7cf1ff66b170457 to your computer and use it in GitHub Desktop.
Revisions
-
nei renamed this gist
Oct 3, 2023 . 1 changed file with 0 additions and 0 deletions.There are no files selected for viewing
File renamed without changes. -
nei revised this gist
Oct 3, 2023 . 1 changed file with 2 additions and 1 deletion.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -11,10 +11,11 @@ $filename = 'list-of-skus.txt'; $endpoint = 'https://example.com?productCode='; $concurrency = 16; // each job should use the browser to GET a certain URL // limit number of concurrent jobs here $queue = new Queue($concurrency, null, function($url) use ($browser) { return $browser->get($url, [ 'Content-Type' => 'application/json' ]); -
nei revised this gist
Oct 3, 2023 . 1 changed file with 8 additions and 0 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,8 @@ { "require": { "react/stream": "1.x-dev", "react/event-loop": "1.x-dev", "react/http": "1.x-dev", "clue/mq-react": "^1.6" } } -
nei created this gist
Oct 3, 2023 .There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,3 @@ sku1 sku2 sku3 This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,58 @@ <?php require_once __DIR__ . '/vendor/autoload.php'; use Clue\React\Mq\Queue; use Psr\Http\Message\ResponseInterface; use React\Stream\ReadableResourceStream; $loop = React\EventLoop\Loop::get(); $browser = new React\Http\Browser($loop); $filename = 'list-of-skus.txt'; $endpoint = 'https://example.com?productCode='; // each job should use the browser to GET a certain URL // limit number of concurrent jobs here $queue = new Queue(16, null, function($url) use ($browser) { return $browser->get($url, [ 'Content-Type' => 'application/json' ]); }); $stream = new ReadableResourceStream(fopen($filename, 'r'), $loop); $stream->on('data', function ($data) use ($browser, $queue, &$count, $endpoint) { // Process each line (data) received from the stream $lines = explode("\n", $data); foreach ($lines as $line) { // Do something with each line echo $line . PHP_EOL; $queue($endpoint.$line) ->then( function (ResponseInterface $response) use ($line) { $result = json_decode((string) $response->getBody(), true); echo 'product '.($result['Content']['ProductCode']).' exist'.PHP_EOL; }, function (Exception $e) use ($line) { echo 'missing '.$line.PHP_EOL; } ); } }); // Handle errors $stream->on('error', function (Exception $e) { echo 'Error: ' . $e->getMessage() . PHP_EOL; }); // Handle end of file (EOF) $stream->on('end', function () use ($loop) { echo 'finished raising requests'; // Close the event loop when the stream ends (EOF) }); // Start the event loop $loop->run();