[ Index ]

PHP Cross Reference of Phabricator

title

Body

[close]

/src/applications/daemon/query/ -> PhabricatorDaemonLogQuery.php (source)

   1  <?php
   2  
   3  final class PhabricatorDaemonLogQuery
   4    extends PhabricatorCursorPagedPolicyAwareQuery {
   5  
   6    const STATUS_ALL = 'status-all';
   7    const STATUS_ALIVE = 'status-alive';
   8  
   9    private $ids;
  10    private $notIDs;
  11    private $status = self::STATUS_ALL;
  12    private $daemonClasses;
  13    private $allowStatusWrites;
  14  
  15    public static function getTimeUntilUnknown() {
  16      return 3 * PhutilDaemonOverseer::HEARTBEAT_WAIT;
  17    }
  18  
  19    public static function getTimeUntilDead() {
  20      return 30 * PhutilDaemonOverseer::HEARTBEAT_WAIT;
  21    }
  22  
  23    public function withIDs(array $ids) {
  24      $this->ids = $ids;
  25      return $this;
  26    }
  27  
  28    public function withoutIDs(array $ids) {
  29      $this->notIDs = $ids;
  30      return $this;
  31    }
  32  
  33    public function withStatus($status) {
  34      $this->status = $status;
  35      return $this;
  36    }
  37  
  38    public function withDaemonClasses(array $classes) {
  39      $this->daemonClasses = $classes;
  40      return $this;
  41    }
  42  
  43    public function setAllowStatusWrites($allow) {
  44      $this->allowStatusWrites = $allow;
  45      return $this;
  46    }
  47  
  48    public function loadPage() {
  49      $table = new PhabricatorDaemonLog();
  50      $conn_r = $table->establishConnection('r');
  51  
  52      $data = queryfx_all(
  53        $conn_r,
  54        'SELECT * FROM %T %Q %Q %Q',
  55        $table->getTableName(),
  56        $this->buildWhereClause($conn_r),
  57        $this->buildOrderClause($conn_r),
  58        $this->buildLimitClause($conn_r));
  59  
  60      return $table->loadAllFromArray($data);
  61    }
  62  
  63    public function willFilterPage(array $daemons) {
  64      $unknown_delay = PhabricatorDaemonLogQuery::getTimeUntilUnknown();
  65      $dead_delay = PhabricatorDaemonLogQuery::getTimeUntilDead();
  66  
  67      $status_running = PhabricatorDaemonLog::STATUS_RUNNING;
  68      $status_unknown = PhabricatorDaemonLog::STATUS_UNKNOWN;
  69      $status_wait = PhabricatorDaemonLog::STATUS_WAIT;
  70      $status_exiting = PhabricatorDaemonLog::STATUS_EXITING;
  71      $status_exited = PhabricatorDaemonLog::STATUS_EXITED;
  72      $status_dead = PhabricatorDaemonLog::STATUS_DEAD;
  73  
  74      $filter = array_fuse($this->getStatusConstants());
  75  
  76      foreach ($daemons as $key => $daemon) {
  77        $status = $daemon->getStatus();
  78        $seen = $daemon->getDateModified();
  79  
  80        $is_running = ($status == $status_running) ||
  81                      ($status == $status_wait) ||
  82                      ($status == $status_exiting);
  83  
  84        // If we haven't seen the daemon recently, downgrade its status to
  85        // unknown.
  86        $unknown_time = ($seen + $unknown_delay);
  87        if ($is_running && ($unknown_time < time())) {
  88          $status = $status_unknown;
  89        }
  90  
  91        // If the daemon hasn't been seen in quite a while, assume it is dead.
  92        $dead_time = ($seen + $dead_delay);
  93        if (($status == $status_unknown) && ($dead_time < time())) {
  94          $status = $status_dead;
  95        }
  96  
  97        // If we changed the daemon's status, adjust it.
  98        if ($status != $daemon->getStatus()) {
  99          $daemon->setStatus($status);
 100  
 101          // ...and write it, if we're in a context where that's reasonable.
 102          if ($this->allowStatusWrites) {
 103            $guard = AphrontWriteGuard::beginScopedUnguardedWrites();
 104              $daemon->save();
 105            unset($guard);
 106          }
 107        }
 108  
 109        // If the daemon no longer matches the filter, get rid of it.
 110        if ($filter) {
 111          if (empty($filter[$daemon->getStatus()])) {
 112            unset($daemons[$key]);
 113          }
 114        }
 115      }
 116  
 117      return $daemons;
 118    }
 119  
 120    private function buildWhereClause(AphrontDatabaseConnection $conn_r) {
 121      $where = array();
 122  
 123      if ($this->ids) {
 124        $where[] = qsprintf(
 125          $conn_r,
 126          'id IN (%Ld)',
 127          $this->ids);
 128      }
 129  
 130      if ($this->notIDs) {
 131        $where[] = qsprintf(
 132          $conn_r,
 133          'id NOT IN (%Ld)',
 134          $this->notIDs);
 135      }
 136  
 137      if ($this->getStatusConstants()) {
 138        $where[] = qsprintf(
 139          $conn_r,
 140          'status IN (%Ls)',
 141          $this->getStatusConstants());
 142      }
 143  
 144      if ($this->daemonClasses) {
 145        $where[] = qsprintf(
 146          $conn_r,
 147          'daemon IN (%Ls)',
 148          $this->daemonClasses);
 149      }
 150  
 151      $where[] = $this->buildPagingClause($conn_r);
 152      return $this->formatWhereClause($where);
 153    }
 154  
 155    private function getStatusConstants() {
 156      $status = $this->status;
 157      switch ($status) {
 158        case self::STATUS_ALL:
 159          return array();
 160        case self::STATUS_ALIVE:
 161          return array(
 162            PhabricatorDaemonLog::STATUS_UNKNOWN,
 163            PhabricatorDaemonLog::STATUS_RUNNING,
 164            PhabricatorDaemonLog::STATUS_WAIT,
 165            PhabricatorDaemonLog::STATUS_EXITING,
 166          );
 167        default:
 168          throw new Exception("Unknown status '{$status}'!");
 169      }
 170    }
 171  
 172    public function getQueryApplicationClass() {
 173      return 'PhabricatorDaemonsApplication';
 174    }
 175  
 176  }


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