[ Index ]

PHP Cross Reference of Phabricator

title

Body

[close]

/src/applications/harbormaster/worker/ -> HarbormasterTargetWorker.php (source)

   1  <?php
   2  
   3  /**
   4   * Execute a build target.
   5   */
   6  final class HarbormasterTargetWorker extends HarbormasterWorker {
   7  
   8    public function getRequiredLeaseTime() {
   9      // This worker performs actual build work, which may involve a long wait
  10      // on external systems.
  11      return phutil_units('24 hours in seconds');
  12    }
  13  
  14    private function loadBuildTarget() {
  15      $data = $this->getTaskData();
  16      $id = idx($data, 'targetID');
  17  
  18      $target = id(new HarbormasterBuildTargetQuery())
  19        ->withIDs(array($id))
  20        ->setViewer($this->getViewer())
  21        ->executeOne();
  22  
  23      if (!$target) {
  24        throw new PhabricatorWorkerPermanentFailureException(
  25          pht(
  26            'Bad build target ID "%d".',
  27            $id));
  28      }
  29  
  30      return $target;
  31    }
  32  
  33    public function doWork() {
  34      $target = $this->loadBuildTarget();
  35      $build = $target->getBuild();
  36      $viewer = $this->getViewer();
  37  
  38      $target->setDateStarted(time());
  39  
  40      try {
  41        if ($target->getBuildGeneration() !== $build->getBuildGeneration()) {
  42          throw new HarbormasterBuildAbortedException();
  43        }
  44  
  45        $status_pending = HarbormasterBuildTarget::STATUS_PENDING;
  46        if ($target->getTargetStatus() == $status_pending) {
  47          $target->setTargetStatus(HarbormasterBuildTarget::STATUS_BUILDING);
  48          $target->save();
  49        }
  50  
  51        $implementation = $target->getImplementation();
  52        $implementation->execute($build, $target);
  53  
  54        $next_status = HarbormasterBuildTarget::STATUS_PASSED;
  55        if ($implementation->shouldWaitForMessage($target)) {
  56          $next_status = HarbormasterBuildTarget::STATUS_WAITING;
  57        }
  58  
  59        $target->setTargetStatus($next_status);
  60  
  61        if ($target->isComplete()) {
  62          $target->setDateCompleted(time());
  63        }
  64  
  65        $target->save();
  66      } catch (PhabricatorWorkerYieldException $ex) {
  67        // If the target wants to yield, let that escape without further
  68        // processing. We'll resume after the task retries.
  69        throw $ex;
  70      } catch (HarbormasterBuildFailureException $ex) {
  71        // A build step wants to fail explicitly.
  72        $target->setTargetStatus(HarbormasterBuildTarget::STATUS_FAILED);
  73        $target->setDateCompleted(time());
  74        $target->save();
  75      } catch (HarbormasterBuildAbortedException $ex) {
  76        // A build step is aborting because the build has been restarted.
  77        $target->setTargetStatus(HarbormasterBuildTarget::STATUS_ABORTED);
  78        $target->setDateCompleted(time());
  79        $target->save();
  80      } catch (Exception $ex) {
  81        phlog($ex);
  82  
  83        try {
  84          $log = $build->createLog($target, 'core', 'exception');
  85          $start = $log->start();
  86          $log->append((string)$ex);
  87          $log->finalize($start);
  88        } catch (Exception $log_ex) {
  89          phlog($log_ex);
  90        }
  91  
  92        $target->setTargetStatus(HarbormasterBuildTarget::STATUS_FAILED);
  93        $target->setDateCompleted(time());
  94        $target->save();
  95      }
  96  
  97      id(new HarbormasterBuildEngine())
  98        ->setViewer($viewer)
  99        ->setBuild($build)
 100        ->continueBuild();
 101    }
 102  
 103  }


Generated: Sun Nov 30 09:20:46 2014 Cross-referenced by PHPXref 0.7.1