diff --git a/src/applications/harbormaster/engine/HarbormasterBuildEngine.php b/src/applications/harbormaster/engine/HarbormasterBuildEngine.php index 6974dd74e3..9c531e1419 100644 --- a/src/applications/harbormaster/engine/HarbormasterBuildEngine.php +++ b/src/applications/harbormaster/engine/HarbormasterBuildEngine.php @@ -72,49 +72,24 @@ final class HarbormasterBuildEngine extends Phobject { } private function updateBuild(HarbormasterBuild $build) { - - $should_stop = false; - $should_resume = false; - $should_restart = false; - foreach ($build->getUnprocessedCommands() as $command) { - switch ($command->getCommand()) { - case HarbormasterBuildCommand::COMMAND_STOP: - $should_stop = true; - $should_resume = false; - break; - case HarbormasterBuildCommand::COMMAND_RESUME: - $should_resume = true; - $should_stop = false; - break; - case HarbormasterBuildCommand::COMMAND_RESTART: - $should_restart = true; - $should_resume = true; - $should_stop = false; - break; - } - } - if (($build->getBuildStatus() == HarbormasterBuild::STATUS_PENDING) || - ($should_restart)) { + ($build->isRestarting())) { $this->destroyBuildTargets($build); $build->setBuildStatus(HarbormasterBuild::STATUS_BUILDING); $build->save(); } - if ($should_resume) { + if ($build->isResuming()) { $build->setBuildStatus(HarbormasterBuild::STATUS_BUILDING); $build->save(); } - if ($should_stop && !$build->isComplete()) { + if ($build->isStopping() && !$build->isComplete()) { $build->setBuildStatus(HarbormasterBuild::STATUS_STOPPED); $build->save(); } - foreach ($build->getUnprocessedCommands() as $command) { - $command->delete(); - } - $build->attachUnprocessedCommands(array()); + $build->deleteUnprocessedCommands(); if ($build->getBuildStatus() == HarbormasterBuild::STATUS_BUILDING) { $this->updateBuildSteps($build); diff --git a/src/applications/harbormaster/storage/build/HarbormasterBuild.php b/src/applications/harbormaster/storage/build/HarbormasterBuild.php index f83f1d9cb9..9a9ff0c92d 100644 --- a/src/applications/harbormaster/storage/build/HarbormasterBuild.php +++ b/src/applications/harbormaster/storage/build/HarbormasterBuild.php @@ -56,6 +56,15 @@ final class HarbormasterBuild extends HarbormasterDAO ->setBuildStatus(self::STATUS_INACTIVE); } + public function delete() { + $this->openTransaction(); + $this->deleteUnprocessedCommands(); + $result = parent::delete(); + $this->saveTransaction(); + + return $result; + } + public function getConfiguration() { return array( self::CONFIG_AUX_PHID => true, @@ -212,7 +221,7 @@ final class HarbormasterBuild extends HarbormasterDAO /* -( Build Commands )----------------------------------------------------- */ - public function getUnprocessedCommands() { + private function getUnprocessedCommands() { return $this->assertAttached($this->unprocessedCommands); } @@ -221,15 +230,6 @@ final class HarbormasterBuild extends HarbormasterDAO return $this; } - public function hasWaitingCommand($command_name) { - foreach ($this->getUnprocessedCommands() as $command_object) { - if ($command_object->getCommand() == $command_name) { - return true; - } - } - return false; - } - public function canRestartBuild() { return !$this->isRestarting(); } @@ -246,15 +246,62 @@ final class HarbormasterBuild extends HarbormasterDAO } public function isStopping() { - return $this->hasWaitingCommand(HarbormasterBuildCommand::COMMAND_STOP); + $is_stopping = false; + foreach ($this->getUnprocessedCommands() as $command_object) { + $command = $command_object->getCommand(); + switch ($command) { + case HarbormasterBuildCommand::COMMAND_STOP: + $is_stopping = true; + break; + case HarbormasterBuildCommand::COMMAND_RESUME: + case HarbormasterBuildCommand::COMMAND_RESTART: + $is_stopping = false; + break; + } + } + + return $is_stopping; } public function isResuming() { - return $this->hasWaitingCommand(HarbormasterBuildCommand::COMMAND_RESUME); + $is_resuming = false; + foreach ($this->getUnprocessedCommands() as $command_object) { + $command = $command_object->getCommand(); + switch ($command) { + case HarbormasterBuildCommand::COMMAND_RESTART: + case HarbormasterBuildCommand::COMMAND_RESUME: + $is_resuming = true; + break; + case HarbormasterBuildCommand::COMMAND_STOP: + $is_resuming = false; + break; + } + } + + return $is_resuming; } public function isRestarting() { - return $this->hasWaitingCommand(HarbormasterBuildCommand::COMMAND_RESTART); + $is_restarting = false; + foreach ($this->getUnprocessedCommands() as $command_object) { + $command = $command_object->getCommand(); + switch ($command) { + case HarbormasterBuildCommand::COMMAND_RESTART: + $is_restarting = true; + break; + } + } + + return $is_restarting; + } + + public function deleteUnprocessedCommands() { + foreach ($this->getUnprocessedCommands() as $key => $command_object) { + $command_object->delete(); + unset($this->unprocessedCommands[$key]); + } + + return $this; }