Skip to content
Merged
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
28 changes: 23 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,8 @@ $client = new Clue\React\Docker\Client();

$client->imageSearch('clue')->then(function (array $images) {
var_dump($images);
}, function (Exception $e) {
echo 'Error: ' . $e->getMessage() . PHP_EOL;
});
```

Expand Down Expand Up @@ -131,15 +133,17 @@ Please see the following section about [promises](#promises) for more details.

Sending requests is async (non-blocking), so you can actually send multiple requests in parallel.
Docker will respond to each request with a response message, the order is not guaranteed.
Sending requests uses a [Promise](https://github.com/reactphp/promise)-based interface that makes it easy to react to when a request is fulfilled (i.e. either successfully resolved or rejected with an error):
Sending requests uses a [Promise](https://github.com/reactphp/promise)-based
interface that makes it easy to react to when a command is completed
(i.e. either successfully fulfilled or rejected with an error):

```php
$client->version()->then(
function ($result) {
var_dump('Result received', $result);
},
function (Exception $error) {
var_dump('There was an error', $error->getMessage());
function (Exception $e) {
echo 'Error: ' . $e->getMessage() . PHP_EOL;
}
});
```
Expand Down Expand Up @@ -214,10 +218,16 @@ the normal stream events:

```php
$stream = $client->execStartStream($exec, $tty);

$stream->on('data', function ($data) {
// data will be emitted in multiple chunk
echo $data;
});

$stream->on('error', function (Exception $e) {
echo 'Error: ' . $e->getMessage() . PHP_EOL;
});

$stream->on('close', function () {
// the stream just ended, this could(?) be a good thing
echo 'Ended' . PHP_EOL;
Expand All @@ -233,9 +243,11 @@ Also note that this option has no effect if you execute with a TTY.

```php
$stream = $client->execStartStream($exec, $tty, 'stderr');

$stream->on('data', function ($data) {
echo 'STDOUT data: ' . $data;
});

$stream->on('stderr', function ($data) {
echo 'STDERR data: ' . $data;
});
Expand Down Expand Up @@ -319,9 +331,9 @@ $client->imageCreate('clue/streamripper')->then(
// $data is an array of *all* elements in the JSON stream
var_dump($data);
},
function (Exception $error) {
function (Exception $e) {
// an error occurred (possibly after receiving *some* elements)
echo 'Error: ' . $error->getMessage() . PHP_EOL;
echo 'Error: ' . $e->getMessage() . PHP_EOL;
}
);
```
Expand Down Expand Up @@ -355,10 +367,16 @@ The resulting stream will emit the following events:

```php
$stream = $client->imageCreateStream('clue/redis-benchmark');

$stream->on('data', function (array $data) {
// data will be emitted for *each* complete element in the JSON stream
echo $data['status'] . PHP_EOL;
});

$stream->on('error', function (Exception $e) {
echo 'Error: ' . $e->getMessage() . PHP_EOL;
});

$stream->on('close', function () {
// the JSON stream just ended, this could(?) be a good thing
echo 'Ended' . PHP_EOL;
Expand Down
9 changes: 5 additions & 4 deletions examples/archive.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,13 +27,14 @@
});
});

$tar->on('error', function ($e = null) {
$tar->on('error', function (Exception $e) {
// should not be invoked, unless the stream is somehow interrupted
echo 'ERROR processing tar stream' . PHP_EOL . $e;
echo 'Error in TAR stream: ' . $e->getMessage() . PHP_EOL;
});
$stream->on('error', function ($e = null) {

$stream->on('error', function (Exception $e) {
// will be called if either parameter is invalid
echo 'ERROR requesting stream' . PHP_EOL . $e;
echo 'Error: ' . $e->getMessage() . PHP_EOL;
});

$stream->pipe($tar);
3 changes: 2 additions & 1 deletion examples/attach-stream.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,14 @@
$caret = new Clue\CaretNotation\Encoder("\t\r\n");

$stream = $client->containerAttachStream($container, true, true);

$stream->on('data', function ($data) use ($caret) {
echo $caret->encode($data);
});

$stream->on('error', function (Exception $e) {
// will be called if either parameter is invalid
echo 'ERROR: ' . $e->getMessage() . PHP_EOL;
echo 'Error: ' . $e->getMessage() . PHP_EOL;
});

$stream->on('close', function () {
Expand Down
10 changes: 7 additions & 3 deletions examples/benchmark-attach.php
Original file line number Diff line number Diff line change
Expand Up @@ -45,19 +45,23 @@
$client->containerStart($container['Id'])->then(null, 'printf');
});

$start = microtime(true);
$bytes = 0;
$stream->on('data', function ($chunk) use (&$bytes) {
$bytes += strlen($chunk);
});

$stream->on('error', 'printf');
$stream->on('error', function (Exception $e) {
echo 'Error: ' . $e->getMessage() . PHP_EOL;
});

// show stats when stream ends
$start = microtime(true);
$stream->on('close', function () use ($client, &$bytes, $start, $container) {
$time = microtime(true) - $start;
$client->containerRemove($container['Id'])->then(null, 'printf');

echo 'Received ' . $bytes . ' bytes in ' . round($time, 1) . 's => ' . round($bytes / $time / 1000000, 1) . ' MB/s' . PHP_EOL;
});
}, 'printf');
}, function (Exception $e) {
echo 'Error: ' . $e->getMessage() . PHP_EOL;
});
11 changes: 7 additions & 4 deletions examples/benchmark-exec.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@
//
// $ docker exec foo dd if=/dev/zero bs=1M count=1000 | dd of=/dev/null


require __DIR__ . '/../vendor/autoload.php';

if (extension_loaded('xdebug')) {
Expand All @@ -33,18 +32,22 @@
$client->execCreate($container, $cmd)->then(function ($info) use ($client) {
$stream = $client->execStartStream($info['Id'], true);

$start = microtime(true);
$bytes = 0;
$stream->on('data', function ($chunk) use (&$bytes) {
$bytes += strlen($chunk);
});

$stream->on('error', 'printf');
$stream->on('error', function (Exception $e) {
echo 'Error: ' . $e->getMessage() . PHP_EOL;
});

// show stats when stream ends
$start = microtime(true);
$stream->on('close', function () use ($client, &$bytes, $start) {
$time = microtime(true) - $start;

echo 'Received ' . $bytes . ' bytes in ' . round($time, 1) . 's => ' . round($bytes / $time / 1000000, 1) . ' MB/s' . PHP_EOL;
});
}, 'printf');
}, function (Exception $e) {
echo 'Error: ' . $e->getMessage() . PHP_EOL;
});
5 changes: 3 additions & 2 deletions examples/events.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
// this simple example displays all docker events that happen in the next 10s.
// try starting / removing a container in the meantime to see some output.


require __DIR__ . '/../vendor/autoload.php';

$client = new Clue\React\Docker\Client();
Expand All @@ -22,4 +21,6 @@
echo json_encode($event) . PHP_EOL;
});

$stream->on('error', 'printf');
$stream->on('error', function (Exception $e) {
echo 'Error: ' . $e->getMessage() . PHP_EOL;
});
6 changes: 1 addition & 5 deletions examples/exec-inspect.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,9 +33,5 @@
})->then(function ($info) {
echo 'Inspected after execution: ' . json_encode($info, JSON_PRETTY_PRINT) . PHP_EOL;
}, function (Exception $e) {
echo 'ERROR: ' . $e->getMessage() . PHP_EOL;

if ($e instanceof React\Http\Message\ResponseException) {
echo 'Response: ' . $e->getResponse()->getBody() . PHP_EOL;
}
echo 'Error: ' . $e->getMessage() . PHP_EOL;
});
12 changes: 9 additions & 3 deletions examples/exec-stream.php
Original file line number Diff line number Diff line change
Expand Up @@ -44,15 +44,21 @@
}
});

$stream->on('error', 'printf');
$stream->on('error', function (Exception $e) {
echo 'Error: ' . $e->getMessage() . PHP_EOL;
});

// remember exit code of executed command once it closes
$stream->on('close', function () use ($client, $info, &$exit) {
$client->execInspect($info['Id'])->then(function ($info) use (&$exit) {
$exit = $info['ExitCode'];
}, 'printf');
}, function (Exception $e) {
echo 'Error: ' . $e->getMessage() . PHP_EOL;
});
});
}, 'printf');
}, function (Exception $e) {
echo 'Error: ' . $e->getMessage() . PHP_EOL;
});

Loop::run();

Expand Down
6 changes: 3 additions & 3 deletions examples/export.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,10 @@

$stream = $client->containerExportStream($container);

$stream->on('error', function ($e = null) {
$stream->on('error', function (Exception $e) {
// will be called if the container is invalid/does not exist
echo 'ERROR requesting stream' . PHP_EOL . $e;
echo 'Error: ' . $e->getMessage() . PHP_EOL;
});

$out = new React\Stream\WritableResourceStream(fopen($target, 'w'));
$out = new React\Stream\WritableResourceStream(fopen($target, 'w'));
$stream->pipe($out);
4 changes: 3 additions & 1 deletion examples/info.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,6 @@

$client->info()->then(function ($info) {
echo json_encode($info, JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES) . PHP_EOL;
}, 'printf');
}, function (Exception $e) {
echo 'Error: ' . $e->getMessage() . PHP_EOL;
});
5 changes: 3 additions & 2 deletions examples/logs-stream.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,14 @@
$caret = new Clue\CaretNotation\Encoder("\t\r\n");

$stream = $client->containerLogsStream($container, true, true, true, 0, false, 100);

$stream->on('data', function ($data) use ($caret) {
echo $caret->encode($data);
});

$stream->on('error', function ($e = null) {
$stream->on('error', function (Exception $e) {
// will be called if either parameter is invalid
echo 'ERROR requesting stream' . PHP_EOL . $e;
echo 'Error: ' . $e->getMessage() . PHP_EOL;
});

$stream->on('close', function ($e = null) {
Expand Down
5 changes: 3 additions & 2 deletions examples/logs.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ function ($logs) {
$caret = new Clue\CaretNotation\Encoder("\t\r\n");
echo $caret->encode($logs);
},
function ($error) use ($container) {
function (Exception $e) use ($container) {
echo <<<EOT
An error occured while trying to access the logs.

Expand All @@ -35,7 +35,8 @@ function ($error) use ($container) {

Here's the error log:

$error
{$e->getMessage()}

EOT;
}
);
4 changes: 4 additions & 0 deletions examples/pull.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,10 @@
echo 'progress: '. json_encode($progress) . PHP_EOL;
});

$stream->on('error', function (Exception $e) {
echo 'Error: ' . $e->getMessage() . PHP_EOL;
});

$stream->on('close', function () {
echo 'stream closed' . PHP_EOL;
});
4 changes: 3 additions & 1 deletion examples/push.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,4 +13,6 @@

$client->imagePush($image, null, null, $auth)->then(function ($result) {
echo json_encode($result, JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES) . PHP_EOL;
}, 'printf');
}, function (Exception $e) {
echo 'Error: ' . $e->getMessage() . PHP_EOL;
});
4 changes: 3 additions & 1 deletion examples/resize.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,4 +17,6 @@
return $client->containerResize($container, $size[0] + 10, $size[1] + 10);
})->then(function () use ($client) {
echo 'Successfully set' . PHP_EOL;
}, 'printf');
}, function (Exception $e) {
echo 'Error: ' . $e->getMessage() . PHP_EOL;
});
6 changes: 5 additions & 1 deletion examples/stats.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,11 @@

echo round($memory, 3) . '% memory and ' . $received . '/' . $sent . ' bytes network I/O' . PHP_EOL;
});
$stream->on('error', 'printf');

$stream->on('error', function (Exception $e) {
echo 'Error: ' . $e->getMessage() . PHP_EOL;
});

$stream->on('close', function () {
echo 'stream closed' . PHP_EOL;
});