Skip to content

Instantly share code, notes, and snippets.

@nei
Last active October 3, 2023 15:53
Show Gist options
  • Select an option

  • Save nei/4ffd278c627890d3b7cf1ff66b170457 to your computer and use it in GitHub Desktop.

Select an option

Save nei/4ffd278c627890d3b7cf1ff66b170457 to your computer and use it in GitHub Desktop.

Revisions

  1. nei renamed this gist Oct 3, 2023. 1 changed file with 0 additions and 0 deletions.
    File renamed without changes.
  2. nei revised this gist Oct 3, 2023. 1 changed file with 2 additions and 1 deletion.
    3 changes: 2 additions & 1 deletion resend.php
    Original 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(16, null, function($url) use ($browser) {
    $queue = new Queue($concurrency, null, function($url) use ($browser) {
    return $browser->get($url, [
    'Content-Type' => 'application/json'
    ]);
  3. nei revised this gist Oct 3, 2023. 1 changed file with 8 additions and 0 deletions.
    8 changes: 8 additions & 0 deletions composer.json
    Original 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"
    }
    }
  4. nei created this gist Oct 3, 2023.
    3 changes: 3 additions & 0 deletions list-of-skus.txt
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,3 @@
    sku1
    sku2
    sku3
    58 changes: 58 additions & 0 deletions resend.php
    Original 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();