public function observeStackActivity($pollInterval = 20) { $eventTable = new StackEventsTable($this->output); $lastStatus = Poller::poll(function () use($eventTable) { try { try { $this->stack = $this->stackFactory->getStack($this->stack->getName(), true); // load fresh instance for updated status $this->output->writeln("-> Polling... (Stack Status: {$this->stack->getStatus()})"); $eventTable->renderEvents($this->stack->getEvents()); } catch (CloudFormationException $exception) { throw Exception::refineException($exception); } } catch (StackNotFoundException $exception) { $this->output->writeln("-> Stack gone."); return Stack::STATUS_STACK_GONE; // this is != false and will stop the poller } return $this->stack->isInProgress() ? false : $this->stack->getStatus(); }, $pollInterval, 1000); $this->printStatus($lastStatus); $this->printResources(); $this->printOutputs(); return $lastStatus; }
/** * @test */ public function returnValueFromCallback() { $function = function () { if (!isset($this->counters[__METHOD__])) { $this->counters[__METHOD__] = 0; } return $this->counters[__METHOD__]++ > 5 ? "HELLO WORLD" : false; }; $result = \StackFormation\Poller::poll($function, 0); $this->assertEquals("HELLO WORLD", $result); }
/** * @return \Aws\Result * @throws \Exception */ public function getChangeSet() { $arguments = $this->prepareArguments(); try { $this->executeBeforeScript(); if (isset($arguments['StackPolicyBody'])) { unset($arguments['StackPolicyBody']); } $arguments['ChangeSetName'] = 'stackformation' . time(); $res = $this->getCfnClient()->createChangeSet($arguments); $changeSetId = $res->get('Id'); $result = Poller::poll(function () use($changeSetId) { $result = $this->getCfnClient()->describeChangeSet(['ChangeSetName' => $changeSetId]); if ($this->output && !$this->output->isQuiet()) { $this->output->writeln("Status: {$result['Status']}"); } if ($result['Status'] == 'FAILED') { throw new \Exception($result['StatusReason']); } return $result['Status'] != 'CREATE_COMPLETE' ? false : $result; }); } catch (CloudFormationException $e) { throw Exception::refineException($e); // will try to create a StackNotFoundException } return $result; }