/** * @param RuntimeInterface $runtime * @param ChannelBaseInterface $channel * @param EnvironmentInterface $env * @param SystemInterface $system * @param FilesystemInterface $fs * @throws InstantiationException */ public function __construct(RuntimeInterface $runtime, ChannelBaseInterface $channel, EnvironmentInterface $env, SystemInterface $system, FilesystemInterface $fs) { $this->runtime = $runtime; $this->channel = $channel; $this->env = $env; $this->system = $system; $this->fs = $fs; $this->scriptRoot = $runtime->core()->dataPath() . '/autorun'; $this->fsPath = $runtime->core()->dataDir() . '/tmp/process/' . $runtime->alias() . '/manager/processes.json'; $this->processes = []; try { $this->processes = $this->selectFromStorage(); } catch (Error $ex) { throw new InstantiationException('ProcessManagerBase could not be initialized.'); } catch (Exception $ex) { throw new InstantiationException('ProcessManagerBase could not be initialized.'); } }
/** * @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; }); }