/** * @override * @inheritDoc */ public function createProcess($alias, $name, $flags = Runtime::CREATE_DEFAULT, $params = []) { if (isset($this->processes[$alias])) { if ($name === null || $name === 'null') { $name = $this->processes[$alias]['name']; } $manager = $this; if ($flags === Runtime::CREATE_DEFAULT && $this->processes[$alias]['verified'] === false) { $req = $this->createRequest($this->channel, $alias, new RuntimeCommand('cmd:ping', $params)); return $req->call()->then(function () { return 'Process has been created.'; }, function () use($manager, $alias, $name, $params) { return $manager->createProcess($alias, $name, Runtime::CREATE_FORCE_HARD, $params); }); } else { if ($flags === Runtime::CREATE_FORCE_SOFT) { return $this->destroyProcess($alias, Runtime::DESTROY_FORCE_SOFT, $params)->then(function () use($manager, $alias, $name, $params) { return $manager->createProcess($alias, $name, $params); }); } else { if ($flags === Runtime::CREATE_FORCE_HARD) { return $this->destroyProcess($alias, Runtime::DESTROY_FORCE_HARD, $params)->then(function () use($manager, $alias, $name, $params) { return $manager->createProcess($alias, $name, $params); }); } else { if ($flags === Runtime::CREATE_FORCE) { return $this->destroyProcess($alias, Runtime::DESTROY_FORCE, $params)->then(function () use($manager, $alias, $name, $params) { return $manager->createProcess($alias, $name, $params); }); } else { return Promise::doReject(new ResourceOccupiedException('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('kraken.process', [$this->runtime->getAlias(), $alias, $name], $this->context)); if (!$this->system->existsPid($pid)) { return Promise::doReject(new ResourceOccupiedException('Process could not be created.')); } if (!$this->allocateProcess($alias, $name, $pid)) { return Promise::doReject(new ResourceOccupiedException('Process could not be created because of storage failure.')); } $req = $this->createRequest($this->channel, $alias, new RuntimeCommand('cmd:ping', $params)); return $req->call()->then(function () { return 'Process has been created.'; }, function ($reason) use($alias) { $this->freeProcess($alias); throw $reason; }, function ($reason) use($alias) { $this->freeProcess($alias); return $reason; }); }
/** * @override * @inheritDoc */ public function createProject($flags = Runtime::CREATE_DEFAULT) { $manager = $this; $alias = $this->projectRoot; $name = $this->projectName; if (isset($this->data['pid'])) { if ($flags === Runtime::CREATE_FORCE_SOFT) { return $this->destroyProject(Runtime::DESTROY_FORCE_SOFT)->then(function () use($manager) { return $manager->createProject(); }); } else { if ($flags === Runtime::CREATE_FORCE_HARD) { return $this->destroyProject(Runtime::DESTROY_FORCE_HARD)->then(function () use($manager) { return $manager->createProject(); }); } else { if ($flags === Runtime::CREATE_FORCE) { return $this->destroyProject(Runtime::DESTROY_FORCE)->then(function () use($manager) { return $manager->createProject(); }); } else { return Promise::doReject(new ResourceOccupiedException('Project already exists.')); } } } } $pid = $this->system->run($this->phpCommand('kraken.process', ['undefined', $alias, $name])); if (!$this->system->existsPid($pid)) { return Promise::doReject(new ResourceOccupiedException('Project could not be created.')); } if (!$this->allocateProject($alias, $name, $pid)) { return Promise::doReject(new ResourceOccupiedException('Project could not be created because of storage failure.')); } $req = $this->createRequest($this->channel, $alias, new RuntimeCommand('cmd:ping')); return $req->call()->then(function () { return 'Project has been created.'; }, function ($reason) { $this->freeProject(); throw $reason; }, function ($reason) { $this->freeProject(); return $reason; }); }