/**
  * @inheritdoc
  *
  * @param Task $task
  */
 public function handle(Task $task)
 {
     $config = $task->getData();
     if ($config["driver"] === "mysql") {
         $config += ["host" => "127.0.0.1", "port" => 3306, "charset" => "utf8", "socket" => null];
     }
     if ($config["remit"]["driver"] === "zeromq") {
         $config["remit"]["server"] += ["host" => "127.0.0.1"];
         $config["remit"]["client"] += ["host" => "127.0.0.1"];
         $server = new ZeroMqServer(new InMemoryLocation($config["remit"]["client"]["host"], $config["remit"]["client"]["port"]));
         $client = new ZeroMqClient(new InMemoryLocation($config["remit"]["server"]["host"], $config["remit"]["server"]["port"]));
     }
     $connection = new ExtendedPdo(new PDO($this->newConnectionString($config), $config["username"], $config["password"]));
     $server->addListener("q", function ($query, $values, $id) use($client, $connection) {
         $client->emit("r", [$connection->fetchAll($query, $values), $id]);
     });
     $server->addListener("d", function () use($connection, $server, $client) {
         $client->emit("dd");
         try {
             $connection->disconnect();
         } catch (Exception $exception) {
             // TODO: find an elegant way to deal with this
         }
         $server->disconnect();
         $client->disconnect();
         Loop\stop();
     });
     Loop\periodic(0, function () use($server) {
         $server->tick();
     });
     Loop\run();
 }
Example #2
0
 /**
  * @test
  */
 public function emitsToListeners()
 {
     $store = 0;
     $listener = function ($add) use(&$store) {
         $store += $add;
     };
     $this->server->addListener("foo", $listener);
     $this->client->emit("foo", array(1));
     $ticks = 0;
     while (true) {
         $this->server->tick();
         // the listener should have incremented this
         if ($store === 1) {
             break;
         }
         if (++$ticks >= 3) {
             $this->fail();
         }
         sleep(1);
     }
     $this->server->removeListener("foo", $listener);
     $this->client->emit("foo", array(1));
     $ticks = 0;
     while (true) {
         $this->server->tick();
         // the listener should not have incremented this, because it's gone!
         if ($store === 1) {
             break;
         }
         if (++$ticks >= 3) {
             $this->fail();
         }
         sleep(1);
     }
 }