Changeset View
Changeset View
Standalone View
Standalone View
src/applications/harbormaster/storage/build/HarbormasterBuild.php
| <?php | <?php | ||||
| final class HarbormasterBuild extends HarbormasterDAO | final class HarbormasterBuild extends HarbormasterDAO | ||||
| implements PhabricatorPolicyInterface { | implements PhabricatorPolicyInterface { | ||||
| protected $buildablePHID; | protected $buildablePHID; | ||||
| protected $buildPlanPHID; | protected $buildPlanPHID; | ||||
| protected $buildStatus; | protected $buildStatus; | ||||
| protected $cancelRequested; | |||||
| private $buildable = self::ATTACHABLE; | private $buildable = self::ATTACHABLE; | ||||
| private $buildPlan = self::ATTACHABLE; | private $buildPlan = self::ATTACHABLE; | ||||
| private $unprocessedCommands = self::ATTACHABLE; | |||||
| /** | /** | ||||
| * Not currently being built. | * Not currently being built. | ||||
| */ | */ | ||||
| const STATUS_INACTIVE = 'inactive'; | const STATUS_INACTIVE = 'inactive'; | ||||
| /** | /** | ||||
| * Pending pick up by the Harbormaster daemon. | * Pending pick up by the Harbormaster daemon. | ||||
| Show All 21 Lines | final class HarbormasterBuild extends HarbormasterDAO | ||||
| const STATUS_FAILED = 'failed'; | const STATUS_FAILED = 'failed'; | ||||
| /** | /** | ||||
| * The build encountered an unexpected error. | * The build encountered an unexpected error. | ||||
| */ | */ | ||||
| const STATUS_ERROR = 'error'; | const STATUS_ERROR = 'error'; | ||||
| /** | /** | ||||
| * The build has been cancelled. | * The build has been stopped. | ||||
| */ | */ | ||||
| const STATUS_CANCELLED = 'cancelled'; | const STATUS_STOPPED = 'stopped'; | ||||
| public static function initializeNewBuild(PhabricatorUser $actor) { | public static function initializeNewBuild(PhabricatorUser $actor) { | ||||
| return id(new HarbormasterBuild()) | return id(new HarbormasterBuild()) | ||||
| ->setBuildStatus(self::STATUS_INACTIVE) | ->setBuildStatus(self::STATUS_INACTIVE); | ||||
| ->setCancelRequested(0); | |||||
| } | } | ||||
| public function getConfiguration() { | public function getConfiguration() { | ||||
| return array( | return array( | ||||
| self::CONFIG_AUX_PHID => true, | self::CONFIG_AUX_PHID => true, | ||||
| ) + parent::getConfiguration(); | ) + parent::getConfiguration(); | ||||
| } | } | ||||
| Show All 26 Lines | final class HarbormasterBuild extends HarbormasterDAO | ||||
| public function getBuildPlan() { | public function getBuildPlan() { | ||||
| return $this->assertAttached($this->buildPlan); | return $this->assertAttached($this->buildPlan); | ||||
| } | } | ||||
| public function isBuilding() { | public function isBuilding() { | ||||
| return $this->getBuildStatus() === self::STATUS_PENDING || | return $this->getBuildStatus() === self::STATUS_PENDING || | ||||
| $this->getBuildStatus() === self::STATUS_WAITING || | $this->getBuildStatus() === self::STATUS_WAITING || | ||||
| $this->getBuildStatus() === self::STATUS_BUILDING || | $this->getBuildStatus() === self::STATUS_BUILDING; | ||||
| $this->getCancelRequested(); | |||||
| } | } | ||||
| public function createLog( | public function createLog( | ||||
| HarbormasterBuildTarget $build_target, | HarbormasterBuildTarget $build_target, | ||||
| $log_source, | $log_source, | ||||
| $log_type) { | $log_type) { | ||||
| $log = HarbormasterBuildLog::initializeNewBuildLog($build_target); | $log = HarbormasterBuildLog::initializeNewBuildLog($build_target) | ||||
| $log->setLogSource($log_source); | ->setLogSource($log_source) | ||||
| $log->setLogType($log_type); | ->setLogType($log_type) | ||||
| $log->save(); | ->save(); | ||||
| return $log; | return $log; | ||||
| } | } | ||||
| public function createArtifact( | public function createArtifact( | ||||
| HarbormasterBuildTarget $build_target, | HarbormasterBuildTarget $build_target, | ||||
| $artifact_key, | $artifact_key, | ||||
| $artifact_type) { | $artifact_type) { | ||||
| Show All 13 Lines | $artifact = id(new HarbormasterBuildArtifactQuery()) | ||||
| array($name)) | array($name)) | ||||
| ->executeOne(); | ->executeOne(); | ||||
| if ($artifact === null) { | if ($artifact === null) { | ||||
| throw new Exception("Artifact not found!"); | throw new Exception("Artifact not found!"); | ||||
| } | } | ||||
| return $artifact; | return $artifact; | ||||
| } | } | ||||
| /** | |||||
| * Checks for and handles build cancellation. If this method returns | |||||
| * true, the caller should stop any current operations and return control | |||||
| * as quickly as possible. | |||||
| */ | |||||
| public function checkForCancellation() { | |||||
| // Here we load a copy of the current build and check whether | |||||
| // the user requested cancellation. We can't do `reload()` here | |||||
| // in case there are changes that have not yet been saved. | |||||
| $copy = id(new HarbormasterBuild())->load($this->getID()); | |||||
| if ($copy->getCancelRequested()) { | |||||
| $this->setBuildStatus(HarbormasterBuild::STATUS_CANCELLED); | |||||
| $this->setCancelRequested(0); | |||||
| $this->save(); | |||||
| return true; | |||||
| } | |||||
| return false; | |||||
| } | |||||
| public function retrieveVariablesFromBuild() { | public function retrieveVariablesFromBuild() { | ||||
| $results = array( | $results = array( | ||||
| 'buildable.diff' => null, | 'buildable.diff' => null, | ||||
| 'buildable.revision' => null, | 'buildable.revision' => null, | ||||
| 'buildable.commit' => null, | 'buildable.commit' => null, | ||||
| 'repository.callsign' => null, | 'repository.callsign' => null, | ||||
| 'repository.vcs' => null, | 'repository.vcs' => null, | ||||
| 'repository.uri' => null, | 'repository.uri' => null, | ||||
| Show All 38 Lines | return array( | ||||
| 'repository.vcs' => | 'repository.vcs' => | ||||
| pht('The version control system, either "svn", "hg" or "git".'), | pht('The version control system, either "svn", "hg" or "git".'), | ||||
| 'repository.uri' => | 'repository.uri' => | ||||
| pht('The URI to clone or checkout the repository from.'), | pht('The URI to clone or checkout the repository from.'), | ||||
| 'step.timestamp' => pht('The current UNIX timestamp.'), | 'step.timestamp' => pht('The current UNIX timestamp.'), | ||||
| 'build.id' => pht('The ID of the current build.')); | 'build.id' => pht('The ID of the current build.')); | ||||
| } | } | ||||
| public function isComplete() { | |||||
| switch ($this->getBuildStatus()) { | |||||
| case self::STATUS_PASSED: | |||||
| case self::STATUS_FAILED: | |||||
| case self::STATUS_ERROR: | |||||
| case self::STATUS_STOPPED: | |||||
| return true; | |||||
| } | |||||
| return false; | |||||
| } | |||||
| public function isStopped() { | |||||
| return ($this->getBuildStatus() == self::STATUS_STOPPED); | |||||
| } | |||||
| /* -( Build Commands )----------------------------------------------------- */ | |||||
| public function getUnprocessedCommands() { | |||||
| return $this->assertAttached($this->unprocessedCommands); | |||||
| } | |||||
| public function attachUnprocessedCommands(array $commands) { | |||||
| $this->unprocessedCommands = $commands; | |||||
| 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(); | |||||
| } | |||||
| public function canStopBuild() { | |||||
| return !$this->isComplete() && | |||||
| !$this->isStopped() && | |||||
| !$this->isStopping(); | |||||
| } | |||||
| public function canResumeBuild() { | |||||
| return $this->isStopped() && | |||||
| !$this->isResuming(); | |||||
| } | |||||
| public function isStopping() { | |||||
| return $this->hasWaitingCommand(HarbormasterBuildCommand::COMMAND_STOP); | |||||
| } | |||||
| public function isResuming() { | |||||
| return $this->hasWaitingCommand(HarbormasterBuildCommand::COMMAND_RESUME); | |||||
| } | |||||
| public function isRestarting() { | |||||
| return $this->hasWaitingCommand(HarbormasterBuildCommand::COMMAND_RESTART); | |||||
| } | |||||
| /* -( PhabricatorPolicyInterface )----------------------------------------- */ | /* -( PhabricatorPolicyInterface )----------------------------------------- */ | ||||
| public function getCapabilities() { | public function getCapabilities() { | ||||
| return array( | return array( | ||||
| PhabricatorPolicyCapability::CAN_VIEW, | PhabricatorPolicyCapability::CAN_VIEW, | ||||
| ); | ); | ||||
| Show All 18 Lines | |||||