Example #1
0
 /**
  * @param string $alias
  * @param string|null $name
  * @param int $flags
  * @return PromiseInterface
  */
 public function createThread($alias, $name, $flags = Runtime::CREATE_DEFAULT)
 {
     if (isset($this->threads[$alias])) {
         if ($name === null) {
             $name = $this->threads[$alias]->name;
         }
         if ($flags === Runtime::CREATE_FORCE_SOFT) {
             $manager = $this;
             return $this->destroyThread($alias, Runtime::DESTROY_FORCE_SOFT)->then(function () use($manager, $alias, $name) {
                 return $manager->createThread($alias, $name);
             });
         } else {
             if ($flags === Runtime::CREATE_FORCE_HARD) {
                 $manager = $this;
                 return $this->destroyThread($alias, Runtime::DESTROY_FORCE_HARD)->then(function () use($manager, $alias, $name) {
                     return $manager->createThread($alias, $name);
                 });
             } else {
                 if ($flags === Runtime::CREATE_FORCE) {
                     $manager = $this;
                     return $this->destroyThread($alias, Runtime::DESTROY_FORCE)->then(function () use($manager, $alias, $name) {
                         return $manager->createThread($alias, $name);
                     });
                 } else {
                     return Promise::doReject(new ResourceDefinedException('Thread with such alias already exists.'));
                 }
             }
         }
     } else {
         if ($name === null) {
             return Promise::doReject(new InvalidArgumentException('Name of new thread cannot be null.'));
         }
     }
     $controller = new ThreadController();
     $wrapper = new ThreadWrapper($controller, $this->runtime->core()->dataPath(), $this->runtime->alias(), $alias, $name);
     $wrapper->start(PTHREADS_INHERIT_ALL);
     $this->allocateThread($alias, $wrapper);
     $req = new Request($this->channel, $alias, new RuntimeCommand('cmd:ping'));
     return $req->call()->then(function () {
         return 'Thread has been created.';
     }, function ($reason) use($alias) {
         $this->freeThread($alias);
         return $reason;
     }, function ($reason) use($alias) {
         $this->freeThread($alias);
         return $reason;
     });
 }
Example #2
0
 /**
  * @param string $alias
  * @param string|null $name
  * @param int $flags
  * @return PromiseInterface
  */
 public function createProcess($alias, $name, $flags = Runtime::CREATE_DEFAULT)
 {
     if (isset($this->processes[$alias])) {
         if ($name === null || $name === 'null') {
             $name = $this->processes[$alias]['name'];
         }
         if ($flags === Runtime::CREATE_DEFAULT && $this->processes[$alias]['verified'] === false) {
             $manager = $this;
             $req = new Request($this->channel, $alias, new RuntimeCommand('cmd:ping'));
             return $req->call()->then(function ($response) {
                 return 'Process has been created.';
             }, function () use($manager, $alias, $name) {
                 return $manager->createProcess($alias, $name, Runtime::CREATE_FORCE_HARD);
             });
         } else {
             if ($flags === Runtime::CREATE_FORCE_SOFT) {
                 $manager = $this;
                 return $this->destroyProcess($alias, Runtime::DESTROY_FORCE_SOFT)->then(function () use($manager, $alias, $name) {
                     return $manager->createProcess($alias, $name);
                 });
             } else {
                 if ($flags === Runtime::CREATE_FORCE_HARD) {
                     $manager = $this;
                     return $this->destroyProcess($alias, Runtime::DESTROY_FORCE_HARD)->then(function () use($manager, $alias, $name) {
                         return $manager->createProcess($alias, $name);
                     });
                 } else {
                     if ($flags === Runtime::CREATE_FORCE) {
                         $manager = $this;
                         return $this->destroyProcess($alias, Runtime::DESTROY_FORCE)->then(function () use($manager, $alias, $name) {
                             return $manager->createProcess($alias, $name);
                         });
                     } else {
                         return Promise::doReject(new ResourceDefinedException('Process with such alias already exists.'));
                     }
                 }
             }
         }
     } else {
         if ($name === null) {
             return Promise::doReject(new InvalidArgumentException('Name of new process cannot be null.'));
         }
     }
     $pid = $this->system->run($this->phpCommand('surume.process', [$this->runtime->alias(), $alias, $name]));
     if (!$this->system->existsPid($pid)) {
         return Promise::doReject(new ResourceDefinedException('Process could not be created.'));
     }
     if (!$this->allocateProcess($alias, $name, $pid)) {
         return Promise::doReject(new ResourceDefinedException('Process could not be created because of storage failure.'));
     }
     $req = new Request($this->channel, $alias, new RuntimeCommand('cmd:ping'));
     return $req->call()->then(function ($response) {
         return 'Process has been created.';
     }, function ($reason) use($alias) {
         $this->freeProcess($alias);
         throw $reason;
     }, function ($reason) use($alias) {
         $this->freeProcess($alias);
         return $reason;
     });
 }