Kraken\Runtime\Container\Manager\ProcessManagerBase::destroyProcess PHP Method

destroyProcess() public method

public destroyProcess ( $alias, $flags = Runtime::DESTROY_FORCE_SOFT, $params = [] )
    public function destroyProcess($alias, $flags = Runtime::DESTROY_FORCE_SOFT, $params = [])
    {
        if (!isset($this->processes[$alias])) {
            return Promise::doResolve("Process [{$alias}] was not needed to be destroyed, because it had not existed.");
        }
        $pid = $this->processes[$alias]['pid'];
        $manager = $this;
        if ($flags === Runtime::DESTROY_KEEP) {
            return Promise::doReject(new ResourceOccupiedException("Process [{$alias}] could not be destroyed with force level=DESTROY_KEEP."));
        } else {
            if ($flags === Runtime::DESTROY_FORCE_SOFT) {
                $req = $this->createRequest($this->channel, $alias, new RuntimeCommand('container:destroy', $params));
                return $req->call()->then(function ($value) use($manager, $alias) {
                    usleep(1000.0);
                    $manager->freeProcess($alias);
                    return $value;
                });
            } else {
                if ($flags === Runtime::DESTROY_FORCE) {
                    $manager = $this;
                    return $manager->destroyProcess($alias, Runtime::DESTROY_FORCE_SOFT, $params)->then(null, function () use($manager, $alias, $params) {
                        return $manager->destroyProcess($alias, Runtime::DESTROY_FORCE_HARD, $params);
                    });
                }
            }
        }
        if (!$this->system->existsPid($pid)) {
            return Promise::doResolve()->then(function () use($manager, $alias) {
                $manager->freeProcess($alias);
            })->then(function () use($pid) {
                return "Process with pid [{$pid}] was not needed to be destroyed, because it had not existed.";
            });
        } else {
            if (!$this->system->kill($pid)) {
                return Promise::doReject(new ResourceOccupiedException("Process with pid [{$pid}] could not be killed forcefully."));
            }
        }
        return Promise::doResolve()->then(function () use($manager, $alias) {
            $manager->freeProcess($alias);
            return "Process has been destroyed!";
        });
    }