/**
  * @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();
 }
예제 #2
0
 /**
  * @inheritdoc
  *
  * @param Task $task
  */
 public function handle(Task $task)
 {
     $data = $task->getData();
     if (isset($data["closure"])) {
         $closure = $data["closure"];
         $closure($this, $task);
     }
 }
예제 #3
0
 /**
  * @inheritdoc
  *
  * @param Task $task
  */
 public function handle(Task $task)
 {
     $data = $task->getData();
     if (isset($data["callback"])) {
         $callback = $data["callback"];
         $callback();
     }
 }
예제 #4
0
 /**
  * @inheritdoc
  *
  * @return bool
  */
 public function isCancelled()
 {
     if ($this->task instanceof Cancellable) {
         return $this->task->isCancelled();
     }
     return false;
 }
예제 #5
0
 /**
  * Revoke any background processes attached to this task.
  *
  * @param Task $task
  *
  * @return bool
  */
 private function killTask(Task $task)
 {
     if ($task instanceof Process) {
         $stdout = $this->getStdOut();
         $stderr = $this->getStdErr();
         $this->getShell()->exec("kill -9 %s {$stdout} {$stderr} &", [$task->getId()]);
         return true;
     }
     return false;
 }
예제 #6
0
 /**
  * Revoke any background processes attached to this task.
  *
  * @param Task $task
  *
  * @return bool
  */
 private function killTask(Task $task)
 {
     if ($task instanceof Process) {
         $this->getShell()->exec("kill -9 %s", [$task->getId()]);
         return true;
     }
     return false;
 }
예제 #7
0
 /**
  * Checks whether a running process can be removed (has stopped running).
  *
  * @param Task $task
  *
  * @return bool
  */
 protected function canRemoveTask(Task $task)
 {
     if (!$task instanceof Process) {
         return true;
     }
     $processes = array_filter($this->running, function (Task $task) {
         return $task instanceof Process;
     });
     if (count($processes) < 1) {
         return true;
     }
     $found = false;
     $stats = $this->getStatsForProcesses($processes);
     foreach ($stats as $stat) {
         if ($stat[0] === $task->getId()) {
             $found = true;
         }
     }
     return !$found;
 }
예제 #8
0
 /**
  * Revoke any background processes attached to this task
  *
  * @param Task $task
  * @return bool If the process was killed
  */
 protected function killTask(Task $task)
 {
     if ($task instanceof Process) {
         $this->getShell()->exec("kill -9 %s", array($task->getId()));
         return true;
     }
     return false;
 }
예제 #9
0
 /**
  * Gets all the rules that apply to a task.
  *
  * @param Task $task
  *
  * @return Rule[]
  */
 private function getRulesForTask(Task $task)
 {
     return array_filter($this->rules, function (Rule $rule) use($task) {
         return $rule->getHandler() === null || $rule->getHandler() === $task->getHandler();
     });
 }