namespace Bunny; use Bunny\Async\Client; use React\Promise; require_once __DIR__ . "/../vendor/autoload.php"; $c = new Client(); $c->connect()->then(function (Client $c) { return $c->channel(); })->then(function (Channel $ch) { return Promise\all([$ch, $ch->queueDeclare("bench_queue"), $ch->exchangeDeclare("bench_exchange"), $ch->queueBind("bench_queue", "bench_exchange")]); })->then(function ($r) { /** @var Channel $ch */ $ch = $r[0]; $t = null; $count = 0; return $ch->consume(function (Message $msg, Channel $ch, Client $c) use(&$t, &$count) { if ($t === null) { $t = microtime(true); } if ($msg->content === "quit") { printf("Pid: %s, Count: %s, Time: %.4f\n", getmypid(), $count, microtime(true) - $t); $c->disconnect()->then(function () use($c) { $c->stop(); }); } else { ++$count; } }, "bench_queue", "", false, true); }); $c->run();
protected function execute(InputInterface $input, OutputInterface $output) { $loop = Factory::create(); $nope = function (\Exception $e) use($output) { $output->writeln("<bg=red>{$e->getMessage()}</>"); }; $c = new Client($loop, ['host' => $input->getOption('host'), 'port' => (int) $input->getOption('port'), 'user' => $input->getOption('user'), 'pass' => $input->getOption('pass'), 'vhost' => $input->getOption('vhost')]); $conn = $c->connect(); $conn->then(function () use($output) { $output->writeln('<bg=green>Connected...</>'); }); if (null !== ($destination = $input->getOption('pipe'))) { $conn->then(function (Client $c) { return $c->channel(); })->then(function (Channel $ch) use($loop, $destination, $input) { $stdin = new Stream(fopen('php://stdin', 'r+'), $loop); $bindings = new Bindings($destination); $dataParser = function ($data) use($input) { return '' !== $input->getOption('delimiter') ? explode($input->getOption('delimiter'), trim($data)) : [trim($data)]; }; $once = $input->getOption('once') ? function () use($ch) { $ch->close()->then(function () use($ch) { $ch->getClient()->disconnect(); }); } : function () { }; $stdin->on('data', function ($data) use($ch, $bindings, $dataParser, $once) { Promise\all(array_map(function ($message) use($ch, $bindings) { return $ch->publish($message, [], $bindings->exchange, $bindings->routingKey); }, array_filter($dataParser($data), function ($message) { return '' !== $message; })))->then($once); }); }, $nope); if ($this->defaultBinding === $input->getOption('bind')) { $input->setOption('bind', []); } } $consume = function (Message $msg) use($input, $output) { $this->consume($msg, $input, $output); }; if ([] !== $input->getOption('consume')) { $conn->then(function (Client $c) { return $c->channel(); })->then(function (Channel $ch) use($input, $consume) { return Promise\all(array_map(function ($queue) use($ch, $consume) { return $ch->consume($consume, $queue, '', false, true, true); }, $input->getOption('consume'))); }, $nope); if ($this->defaultBinding === $input->getOption('bind')) { $input->setOption('bind', []); } } if ([] !== $input->getOption('bind')) { $conn->then(function (Client $c) { return $c->channel(); })->then(function (Channel $ch) { return Promise\all([$ch, $ch->queueDeclare('', false, false, true, true)]); })->then(function ($r) use($input) { list($ch, $qr) = $r; return Promise\all(array_merge([$ch, $qr], array_map(function ($exKey) use($ch, $qr) { $bindings = new Bindings($exKey); return $ch->queueBind($qr->queue, $bindings->exchange, $bindings->routingKey, false, $bindings->headers); }, $input->getOption('bind')))); })->then(function ($r) use($consume) { return $r[0]->consume($consume, $r[1]->queue, '', false, true, true); }, $nope); } try { $c->run(); } catch (\Exception $e) { $nope($e); } }