AsyncPHP\Doorman\Manager\ProcessManager::tick PHP 메소드

tick() 공개 메소드

public tick ( ) : boolean
리턴 boolean
    public function tick()
    {
        if (!$this->timings instanceof SplObjectStorage) {
            $this->timings = new SplObjectStorage();
        }
        $waiting = [];
        $running = [];
        foreach ($this->waiting as $task) {
            if ($this->isTaskCancelled($task)) {
                continue;
            }
            if (!$this->canRunTask($task)) {
                $waiting[] = $task;
                continue;
            }
            if ($task->stopsSiblings()) {
                $this->stopSiblingTasks($task);
            }
            $binary = $this->getBinary();
            $worker = $this->getWorker();
            $stdout = $this->getStdOut();
            $stderr = $this->getStdErr();
            if ($task instanceof Expires) {
                $this->timings[$task] = time();
            }
            $output = $this->getShell()->exec("{$binary} {$worker} %s {$stdout} {$stderr} & echo \$!", [$this->getTaskString($task)]);
            if ($task instanceof Process) {
                $task->setId($output[0]);
            }
            $this->running[] = $task;
        }
        foreach ($this->running as $task) {
            if (!$this->canRemoveTask($task)) {
                $running[] = $task;
            }
        }
        $this->waiting = $waiting;
        $this->running = $running;
        return !empty($waiting) || !empty($running);
    }

Usage Example

예제 #1
0
 /**
  * @test
  */
 public function basicRulesAndTasksWork()
 {
     $task1 = new ProcessCallbackTask(function () {
         touch(__DIR__ . "/task1.temp");
         for ($i = 0; $i < 10; $i++) {
             usleep(50000);
         }
         unlink(__DIR__ . "/task1.temp");
     });
     $task2 = new ProcessCallbackTask(function () {
         touch(__DIR__ . "/task2.temp");
         for ($i = 0; $i < 10; $i++) {
             usleep(50000);
         }
         unlink(__DIR__ . "/task2.temp");
     });
     $rule = new InMemoryRule();
     $rule->setProcesses(1);
     $rule->setMinimumProcessorUsage(0);
     $rule->setMaximumProcessorUsage(100);
     $added = false;
     $this->manager->addRule($rule);
     $this->manager->addTask($task1);
     while ($this->manager->tick()) {
         usleep(50000);
         if (!$added) {
             $this->manager->addTask($task2);
             $added = true;
         }
         if (file_exists(__DIR__ . "/task1.temp") && file_exists(__DIR__ . "/task2.temp")) {
             $this->fail("Tasks should not be run concurrently");
         }
     }
     $this->manager->removeRule($rule);
     $this->manager->addTask($task1);
     $this->manager->addTask($task2);
     if ($this->manager->tick()) {
         usleep(50000);
         if (!file_exists(__DIR__ . "/task1.temp") || !file_exists(__DIR__ . "/task2.temp")) {
             $this->fail("Tasks should be run concurrently");
         }
     }
 }
All Usage Examples Of AsyncPHP\Doorman\Manager\ProcessManager::tick