Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Simplify usage by supporting new default loop #71

Merged
merged 1 commit into from
Jul 22, 2021
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
29 changes: 14 additions & 15 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -58,14 +58,11 @@ Once [installed](#install), you can use the following code to access the
Docker API of your local docker daemon:

```php
$loop = React\EventLoop\Factory::create();
$client = new Clue\React\Docker\Client($loop);
$client = new Clue\React\Docker\Client();

$client->imageSearch('clue')->then(function (array $images) {
var_dump($images);
});

$loop->run();
```

See also the [examples](examples).
Expand All @@ -75,23 +72,26 @@ See also the [examples](examples).
### Client

The `Client` is responsible for assembling and sending HTTP requests to the Docker Engine API.
It uses an HTTP client bound to the main [`EventLoop`](https://github.com/reactphp/event-loop#usage)
in order to handle async requests:

```php
$loop = React\EventLoop\Factory::create();
$client = new Clue\React\Docker\Client($loop);
$client = new Clue\React\Docker\Client();
```

This class takes an optional `LoopInterface|null $loop` parameter that can be used to
pass the event loop instance to use for this object. You can use a `null` value
here in order to use the [default loop](https://github.com/reactphp/event-loop#loop).
This value SHOULD NOT be given unless you're sure you want to explicitly use a
given event loop instance.

If your Docker Engine API is not accessible using the default `unix:///var/run/docker.sock`
Unix domain socket path, you may optionally pass an explicit URL like this:

```
```php
// explicitly use given UNIX socket path
$client = new Clue\React\Docker\Client($loop, 'unix:///var/run/docker.sock');
$client = new Clue\React\Docker\Client(null, 'unix:///var/run/docker.sock');

// or connect via TCP/IP to a remote Docker Engine API
$client = new Clue\React\Docker\Client($loop, 'http://10.0.0.2:8000/');
$client = new Clue\React\Docker\Client(null, 'http://10.0.0.2:8000/');
```

#### Commands
Expand Down Expand Up @@ -154,13 +154,12 @@ The resulting blocking code could look something like this:
```php
use Clue\React\Block;

$loop = React\EventLoop\Factory::create();
$client = new Clue\React\Docker\Client($loop);
$client = new Clue\React\Docker\Client();

$promise = $client->imageInspect('busybox');

try {
$results = Block\await($promise, $loop);
$results = Block\await($promise, Loop::get());
// resporesults successfully received
} catch (Exception $e) {
// an error occured while performing the request
Expand All @@ -175,7 +174,7 @@ $promises = array(
$client->imageInspect('ubuntu'),
);

$inspections = Block\awaitAll($promises, $loop);
$inspections = Block\awaitAll($promises, Loop::get());
```

Please refer to [clue/reactphp-block](https://github.com/clue/reactphp-block#readme) for more details.
Expand Down
8 changes: 4 additions & 4 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,12 @@
"require": {
"php": ">=5.3",
"clue/json-stream": "^0.1",
"react/event-loop": "^1.0 || ^0.5 || ^0.4 || ^0.3",
"react/http": "^1.0",
"react/event-loop": "^1.2",
"react/http": "^1.4",
"react/promise": "^2.0 || ^1.1",
"react/promise-stream": "^1.0",
"react/socket": "^1.0",
"react/stream": "^1.0",
"react/socket": "^1.8",
"react/stream": "^1.2",
"rize/uri-template": "^0.3"
},
"require-dev": {
Expand Down
5 changes: 1 addition & 4 deletions examples/archive.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,7 @@
$path = isset($argv[2]) ? $argv[2] : '/etc/passwd';
echo 'Container "' . $container . '" dumping "' . $path . '" (pass as arguments to this example)' . PHP_EOL;

$loop = React\EventLoop\Factory::create();
$client = new Client($loop);
$client = new Client();

$stream = $client->containerArchiveStream($container, $path);

Expand All @@ -43,5 +42,3 @@
});

$stream->pipe($tar);

$loop->run();
5 changes: 1 addition & 4 deletions examples/attach-stream.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,7 @@
$container = isset($argv[1]) ? $argv[1] : 'foo';
echo 'Dumping output of container "' . $container . '" (pass as argument to this example)' . PHP_EOL;

$loop = React\EventLoop\Factory::create();
$client = new Client($loop);
$client = new Client();

// use caret notation for any control characters except \t, \r and \n
$caret = new Encoder("\t\r\n");
Expand All @@ -33,5 +32,3 @@
$stream->on('close', function () {
echo 'CLOSED' . PHP_EOL;
});

$loop->run();
10 changes: 4 additions & 6 deletions examples/benchmark-attach.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
// $ docker run -i --rm --log-driver=none busybox dd if=/dev/zero bs=1M count=1000 status=none | dd of=/dev/null

use Clue\React\Docker\Client;
use React\EventLoop\Loop;

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

Expand All @@ -26,8 +27,7 @@
$cmd = array_slice($argv, 2);
}

$loop = React\EventLoop\Factory::create();
$client = new Client($loop);
$client = new Client();

$client->containerCreate(array(
'Image' => $image,
Expand All @@ -38,11 +38,11 @@
'Type' => 'none'
)
)
))->then(function ($container) use ($client, $loop) {
))->then(function ($container) use ($client) {
$stream = $client->containerAttachStream($container['Id'], false, true);

// we're creating the container without a log, so first wait for attach stream before starting
$loop->addTimer(0.1, function () use ($client, $container) {
Loop::addTimer(0.1, function () use ($client, $container) {
$client->containerStart($container['Id'])->then(null, 'printf');
});

Expand All @@ -62,5 +62,3 @@
echo 'Received ' . $bytes . ' bytes in ' . round($time, 1) . 's => ' . round($bytes / $time / 1000000, 1) . ' MB/s' . PHP_EOL;
});
}, 'printf');

$loop->run();
5 changes: 1 addition & 4 deletions examples/benchmark-exec.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,7 @@
$cmd = array_slice($argv, 2);
}

$loop = React\EventLoop\Factory::create();
$client = new Client($loop);
$client = new Client();

$client->execCreate($container, $cmd)->then(function ($info) use ($client) {
$stream = $client->execStartStream($info['Id'], true);
Expand All @@ -50,5 +49,3 @@
echo 'Received ' . $bytes . ' bytes in ' . round($time, 1) . 's => ' . round($bytes / $time / 1000000, 1) . ' MB/s' . PHP_EOL;
});
}, 'printf');

$loop->run();
5 changes: 1 addition & 4 deletions examples/events.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,7 @@

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

$loop = React\EventLoop\Factory::create();
$client = new Client($loop);
$client = new Client();

// get a list of all events that happened up until this point
// expect this list to be limited to the last 64 (or so) events
Expand All @@ -25,5 +24,3 @@
});

$stream->on('error', 'printf');

$loop->run();
5 changes: 1 addition & 4 deletions examples/exec-inspect.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,7 @@
$cmd = array_slice($argv, 2);
}

$loop = React\EventLoop\Factory::create();
$client = new Client($loop);
$client = new Client();

$client->execCreate($container, $cmd)->then(function ($info) use ($client) {
echo 'Created with info: ' . json_encode($info) . PHP_EOL;
Expand All @@ -43,5 +42,3 @@
echo 'Response: ' . $e->getResponse()->getBody() . PHP_EOL;
}
});

$loop->run();
10 changes: 5 additions & 5 deletions examples/exec-stream.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
// displays the streaming output as it happens.

use Clue\React\Docker\Client;
use React\EventLoop\Loop;
use React\Stream\WritableResourceStream;

require __DIR__ . '/../vendor/autoload.php';
Expand All @@ -23,11 +24,10 @@
$cmd = array_slice($argv, 2);
}

$loop = React\EventLoop\Factory::create();
$client = new Client($loop);
$client = new Client();

$out = new WritableResourceStream(STDOUT, $loop);
$stderr = new WritableResourceStream(STDERR, $loop);
$out = new WritableResourceStream(STDOUT);
$stderr = new WritableResourceStream(STDERR);

// unkown exit code by default
$exit = 1;
Expand Down Expand Up @@ -56,6 +56,6 @@
});
}, 'printf');

$loop->run();
Loop::run();

exit($exit);
7 changes: 2 additions & 5 deletions examples/export.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,7 @@
$target = isset($argv[2]) ? $argv[2] : ($container . '.tar');
echo 'Exporting whole container "' . $container . '" to "' . $target .'" (pass as arguments to this example)' . PHP_EOL;

$loop = React\EventLoop\Factory::create();
$client = new Client($loop);
$client = new Client();

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

Expand All @@ -26,7 +25,5 @@
echo 'ERROR requesting stream' . PHP_EOL . $e;
});

$out = new WritableResourceStream(fopen($target, 'w'), $loop);
$out = new WritableResourceStream(fopen($target, 'w'));
$stream->pipe($out);

$loop->run();
5 changes: 1 addition & 4 deletions examples/info.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,8 @@

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

$loop = React\EventLoop\Factory::create();
$client = new Client($loop);
$client = new Client();

$client->info()->then(function ($info) {
echo json_encode($info, JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES) . PHP_EOL;
}, 'printf');

$loop->run();
5 changes: 1 addition & 4 deletions examples/logs-stream.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,7 @@
$container = isset($argv[1]) ? $argv[1] : 'asd';
echo 'Dumping logs (last 100 lines) of container "' . $container . '" (pass as argument to this example)' . PHP_EOL;

$loop = React\EventLoop\Factory::create();
$client = new Client($loop);
$client = new Client();

// use caret notation for any control characters except \t, \r and \n
$caret = new Encoder("\t\r\n");
Expand All @@ -30,5 +29,3 @@
$stream->on('close', function ($e = null) {
echo 'CLOSED' . PHP_EOL . $e;
});

$loop->run();
5 changes: 1 addition & 4 deletions examples/logs.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,7 @@
$container = isset($argv[1]) ? $argv[1] : 'foo';
echo 'Dumping logs (last 100 lines) of container "' . $container . '" (pass as argument to this example)' . PHP_EOL;

$loop = React\EventLoop\Factory::create();
$client = new Client($loop);
$client = new Client();

$client->containerLogs($container, false, true, true, 0, false, 100)->then(
function ($logs) {
Expand All @@ -40,5 +39,3 @@ function ($error) use ($container) {
EOT;
}
);

$loop->run();
5 changes: 1 addition & 4 deletions examples/pull.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,7 @@
$image = isset($argv[1]) ? $argv[1] : 'clue/redis-benchmark';
echo 'Pulling image "' . $image . '" (pass as argument to this example)' . PHP_EOL;

$loop = React\EventLoop\Factory::create();
$client = new Client($loop);
$client = new Client();

$stream = $client->imageCreateStream($image);

Expand All @@ -22,5 +21,3 @@
$stream->on('close', function () {
echo 'stream closed' . PHP_EOL;
});

$loop->run();
5 changes: 1 addition & 4 deletions examples/push.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,8 @@
$auth = json_decode('{"username": "string", "password": "string", "email": "string", "serveraddress" : "string", "auth": ""}');
echo 'Pushing image "' . $image . '" (pass as argument to this example)' . PHP_EOL;

$loop = React\EventLoop\Factory::create();
$client = new Client($loop);
$client = new Client();

$client->imagePush($image, null, null, $auth)->then(function ($result) {
echo json_encode($result, JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES) . PHP_EOL;
}, 'printf');

$loop->run();
6 changes: 1 addition & 5 deletions examples/resize.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,7 @@

$container = isset($argv[1]) ? $argv[1] : 'asd';

$loop = React\EventLoop\Factory::create();
$client = new Client($loop);
$client = new Client();

$client->containerInspect($container)->then(function ($info) use ($client, $container) {
$size = $info['HostConfig']['ConsoleSize'];
Expand All @@ -21,6 +20,3 @@
})->then(function () use ($client) {
echo 'Successfully set' . PHP_EOL;
}, 'printf');


$loop->run();
5 changes: 1 addition & 4 deletions examples/stats.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,7 @@
$container = isset($argv[1]) ? $argv[1] : 'asd';
echo 'Monitoring "' . $container . '" (pass as argument to this example)' . PHP_EOL;

$loop = React\EventLoop\Factory::create();
$client = new Client($loop);
$client = new Client();

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

Expand All @@ -31,5 +30,3 @@
$stream->on('close', function () {
echo 'stream closed' . PHP_EOL;
});

$loop->run();
Loading