AsyncPHP\Doorman\Manager\ProcessManager::addRule PHP Method

addRule() public method

public addRule ( AsyncPHP\Doorman\Rule $rule )
$rule AsyncPHP\Doorman\Rule
    public function addRule(Rule $rule)
    {
        $this->getRules()->addRule($rule);
        return $this;
    }

Usage Example

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::addRule