Path: blob/master/src/applications/daemon/query/PhabricatorDaemonLogQuery.php
12262 views
<?php12final class PhabricatorDaemonLogQuery3extends PhabricatorCursorPagedPolicyAwareQuery {45const STATUS_ALL = 'status-all';6const STATUS_ALIVE = 'status-alive';7const STATUS_RUNNING = 'status-running';89private $ids;10private $notIDs;11private $status = self::STATUS_ALL;12private $daemonClasses;13private $allowStatusWrites;14private $daemonIDs;1516public static function getTimeUntilUnknown() {17return 3 * PhutilDaemonHandle::getHeartbeatEventFrequency();18}1920public static function getTimeUntilDead() {21return 30 * PhutilDaemonHandle::getHeartbeatEventFrequency();22}2324public function withIDs(array $ids) {25$this->ids = $ids;26return $this;27}2829public function withoutIDs(array $ids) {30$this->notIDs = $ids;31return $this;32}3334public function withStatus($status) {35$this->status = $status;36return $this;37}3839public function withDaemonClasses(array $classes) {40$this->daemonClasses = $classes;41return $this;42}4344public function setAllowStatusWrites($allow) {45$this->allowStatusWrites = $allow;46return $this;47}4849public function withDaemonIDs(array $daemon_ids) {50$this->daemonIDs = $daemon_ids;51return $this;52}5354protected function loadPage() {55$table = new PhabricatorDaemonLog();56$conn_r = $table->establishConnection('r');5758$data = queryfx_all(59$conn_r,60'SELECT * FROM %T %Q %Q %Q',61$table->getTableName(),62$this->buildWhereClause($conn_r),63$this->buildOrderClause($conn_r),64$this->buildLimitClause($conn_r));6566return $table->loadAllFromArray($data);67}6869protected function willFilterPage(array $daemons) {70$unknown_delay = self::getTimeUntilUnknown();71$dead_delay = self::getTimeUntilDead();7273$status_running = PhabricatorDaemonLog::STATUS_RUNNING;74$status_unknown = PhabricatorDaemonLog::STATUS_UNKNOWN;75$status_wait = PhabricatorDaemonLog::STATUS_WAIT;76$status_exiting = PhabricatorDaemonLog::STATUS_EXITING;77$status_exited = PhabricatorDaemonLog::STATUS_EXITED;78$status_dead = PhabricatorDaemonLog::STATUS_DEAD;7980$filter = array_fuse($this->getStatusConstants());8182foreach ($daemons as $key => $daemon) {83$status = $daemon->getStatus();84$seen = $daemon->getDateModified();8586$is_running = ($status == $status_running) ||87($status == $status_wait) ||88($status == $status_exiting);8990// If we haven't seen the daemon recently, downgrade its status to91// unknown.92$unknown_time = ($seen + $unknown_delay);93if ($is_running && ($unknown_time < time())) {94$status = $status_unknown;95}9697// If the daemon hasn't been seen in quite a while, assume it is dead.98$dead_time = ($seen + $dead_delay);99if (($status == $status_unknown) && ($dead_time < time())) {100$status = $status_dead;101}102103// If we changed the daemon's status, adjust it.104if ($status != $daemon->getStatus()) {105$daemon->setStatus($status);106107// ...and write it, if we're in a context where that's reasonable.108if ($this->allowStatusWrites) {109$guard = AphrontWriteGuard::beginScopedUnguardedWrites();110$daemon->save();111unset($guard);112}113}114115// If the daemon no longer matches the filter, get rid of it.116if ($filter) {117if (empty($filter[$daemon->getStatus()])) {118unset($daemons[$key]);119}120}121}122123return $daemons;124}125126protected function buildWhereClause(AphrontDatabaseConnection $conn) {127$where = array();128129if ($this->ids !== null) {130$where[] = qsprintf(131$conn,132'id IN (%Ld)',133$this->ids);134}135136if ($this->notIDs !== null) {137$where[] = qsprintf(138$conn,139'id NOT IN (%Ld)',140$this->notIDs);141}142143if ($this->getStatusConstants()) {144$where[] = qsprintf(145$conn,146'status IN (%Ls)',147$this->getStatusConstants());148}149150if ($this->daemonClasses !== null) {151$where[] = qsprintf(152$conn,153'daemon IN (%Ls)',154$this->daemonClasses);155}156157if ($this->daemonIDs !== null) {158$where[] = qsprintf(159$conn,160'daemonID IN (%Ls)',161$this->daemonIDs);162}163164$where[] = $this->buildPagingClause($conn);165166return $this->formatWhereClause($conn, $where);167}168169private function getStatusConstants() {170$status = $this->status;171switch ($status) {172case self::STATUS_ALL:173return array();174case self::STATUS_RUNNING:175return array(176PhabricatorDaemonLog::STATUS_RUNNING,177);178case self::STATUS_ALIVE:179return array(180PhabricatorDaemonLog::STATUS_UNKNOWN,181PhabricatorDaemonLog::STATUS_RUNNING,182PhabricatorDaemonLog::STATUS_WAIT,183PhabricatorDaemonLog::STATUS_EXITING,184);185default:186throw new Exception(pht('Unknown status "%s"!', $status));187}188}189190public function getQueryApplicationClass() {191return 'PhabricatorDaemonsApplication';192}193194}195196197