Skip to content

Commit

Permalink
Fixes documentation code for pass through example
Browse files Browse the repository at this point in the history
  • Loading branch information
hollodotme committed Sep 25, 2017
1 parent 20ed5fa commit 0c34f0d
Showing 1 changed file with 32 additions and 31 deletions.
63 changes: 32 additions & 31 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -440,27 +440,32 @@ while ( $client->hasUnhandledResponses() )
1
```

### Reading request as it goes back piece by piece (pass-through)
### Reading output buffer from worker script using pass through callbacks

It may be useful to see the progression of a request, such as a batch. Say you have a PHP script like:
It may be useful to see the progression of a requested script by having access to the flushed output of that script.
The php.ini default output buffering for php-fpm is 4096 bytes and is (hard-coded) disabled for CLI mode. ([See documentation](http://php.net/manual/en/outcontrol.configuration.php#ini.output-buffering))
Calling `ob_implicit_flush()` causes every call to `echo` or `print` to immediately be flushed.

The callee script could look like this:
```php
<?php
ob_implicit_flush(); // needed so that every echo() is flushed when length of echo'ed text is over ini:output_buffering's value
function show($string)
<?php declare(strict_types=1);

ob_implicit_flush();

function show( string $string )
{
echo str_repeat("\033[0m", max(1, ceil((4096 - strlen($string) % 4096) / 4))).$string.PHP_EOL; // replace 4096 with your output_buffering's value if different
flush();
echo $string . str_repeat( "\r", 4096 - strlen( $string ) ) . PHP_EOL;
sleep( 1 );
}
show('Very long script to run');
// one long task
show('First long task ended');
// another long task
show('Second long task ended');

echo 'End of script'; // no need of show() here, as the script will end and send the final text.
show( 'One' );
show( 'Two' );
show( 'Three' );

echo 'End';
```

The `caller.php` file which is run from the console:
The caller than could look like this:
```php
<?php declare(strict_types=1);

Expand All @@ -470,35 +475,31 @@ use hollodotme\FastCGI\Client;
use hollodotme\FastCGI\Requests\GetRequest;
use hollodotme\FastCGI\SocketConnections\NetworkSocket;

ob_implicit_flush(); // needed here too

$client = new Client( new NetworkSocket( '127.0.0.1', 9000 ) );

$passThroughCallback = function( $packet )
$passThroughCallback = function( string $buffer )
{
echo 'Batch script: ', $packet;
echo 'Buffer: ' . $buffer;
};

$request = new GetRequest('/path/to/target/batch/script.php', http_build_query(['key' => '1', 'sleep' => 3]));
$request->addPassThroughCallbacks($passThroughCallback);
$request = new GetRequest('/path/to/target/script.php', '');
$request->addPassThroughCallbacks( $passThroughCallback );

$client->sendAsyncRequest($request);
$client->waitForResponses();
```

Call the batch script in your console:
```
php /path/to/batch/caller.php
```

```
# prints immediately
Very long script to run
# after first task is done
First long task ended
# after second task is done
Second long task ended
End of script
Buffer: Content-type: text/html; charset=UTF-8
One
# sleeps 1 sec
Buffer: Two
# sleeps 1 sec
Buffer: Three
# sleeps 1 sec
Buffer: End
```

----
Expand Down

0 comments on commit 0c34f0d

Please sign in to comment.