diff --git a/src/__phutil_library_map__.php b/src/__phutil_library_map__.php index 21c9ab27cc..8ce914c374 100644 --- a/src/__phutil_library_map__.php +++ b/src/__phutil_library_map__.php @@ -2536,6 +2536,7 @@ phutil_register_library_map(array( 'PhabricatorWorker' => 'infrastructure/daemon/workers/PhabricatorWorker.php', 'PhabricatorWorkerActiveTask' => 'infrastructure/daemon/workers/storage/PhabricatorWorkerActiveTask.php', 'PhabricatorWorkerArchiveTask' => 'infrastructure/daemon/workers/storage/PhabricatorWorkerArchiveTask.php', + 'PhabricatorWorkerArchiveTaskQuery' => 'infrastructure/daemon/workers/query/PhabricatorWorkerArchiveTaskQuery.php', 'PhabricatorWorkerDAO' => 'infrastructure/daemon/workers/storage/PhabricatorWorkerDAO.php', 'PhabricatorWorkerLeaseQuery' => 'infrastructure/daemon/workers/query/PhabricatorWorkerLeaseQuery.php', 'PhabricatorWorkerManagementCancelWorkflow' => 'infrastructure/daemon/workers/management/PhabricatorWorkerManagementCancelWorkflow.php', @@ -5750,6 +5751,7 @@ phutil_register_library_map(array( 'PhabricatorWordPressAuthProvider' => 'PhabricatorOAuth2AuthProvider', 'PhabricatorWorkerActiveTask' => 'PhabricatorWorkerTask', 'PhabricatorWorkerArchiveTask' => 'PhabricatorWorkerTask', + 'PhabricatorWorkerArchiveTaskQuery' => 'PhabricatorQuery', 'PhabricatorWorkerDAO' => 'PhabricatorLiskDAO', 'PhabricatorWorkerLeaseQuery' => 'PhabricatorQuery', 'PhabricatorWorkerManagementCancelWorkflow' => 'PhabricatorWorkerManagementWorkflow', diff --git a/src/applications/daemon/controller/PhabricatorDaemonConsoleController.php b/src/applications/daemon/controller/PhabricatorDaemonConsoleController.php index f51d558de1..1c7a4cf27a 100644 --- a/src/applications/daemon/controller/PhabricatorDaemonConsoleController.php +++ b/src/applications/daemon/controller/PhabricatorDaemonConsoleController.php @@ -14,9 +14,9 @@ final class PhabricatorDaemonConsoleController // but we'd rather show that utilization is too high than too low. $lease_overhead = 0.250; - $completed = id(new PhabricatorWorkerArchiveTask())->loadAllWhere( - 'dateModified > %d', - $window_start); + $completed = id(new PhabricatorWorkerArchiveTaskQuery()) + ->withDateModifiedSince($window_start) + ->execute(); $failed = id(new PhabricatorWorkerActiveTask())->loadAllWhere( 'failureTime > %d', diff --git a/src/applications/daemon/controller/PhabricatorWorkerTaskDetailController.php b/src/applications/daemon/controller/PhabricatorWorkerTaskDetailController.php index 1e567d4c49..f562522b68 100644 --- a/src/applications/daemon/controller/PhabricatorWorkerTaskDetailController.php +++ b/src/applications/daemon/controller/PhabricatorWorkerTaskDetailController.php @@ -15,7 +15,10 @@ final class PhabricatorWorkerTaskDetailController $task = id(new PhabricatorWorkerActiveTask())->load($this->id); if (!$task) { - $task = id(new PhabricatorWorkerArchiveTask())->load($this->id); + $tasks = id(new PhabricatorWorkerArchiveTaskQuery()) + ->withIDs(array($this->id)) + ->execute(); + $task = reset($tasks); } if (!$task) { diff --git a/src/applications/daemon/garbagecollector/PhabricatorDaemonTaskGarbageCollector.php b/src/applications/daemon/garbagecollector/PhabricatorDaemonTaskGarbageCollector.php index 331462323d..9f320289ab 100644 --- a/src/applications/daemon/garbagecollector/PhabricatorDaemonTaskGarbageCollector.php +++ b/src/applications/daemon/garbagecollector/PhabricatorDaemonTaskGarbageCollector.php @@ -14,18 +14,16 @@ final class PhabricatorDaemonTaskGarbageCollector $data_table = new PhabricatorWorkerTaskData(); $conn_w = $table->establishConnection('w'); - $rows = queryfx_all( - $conn_w, - 'SELECT id, dataID FROM %T WHERE dateCreated < %d LIMIT 100', - $table->getTableName(), - time() - $ttl); + $tasks = id(new PhabricatorWorkerArchiveTaskQuery()) + ->withDateCreatedBefore(time() - $ttl) + ->execute(); - if (!$rows) { + if (!$tasks) { return false; } - $data_ids = array_filter(ipull($rows, 'dataID')); - $task_ids = ipull($rows, 'id'); + $data_ids = array_filter(mpull($tasks, 'getDataID')); + $task_ids = mpull($tasks, 'getID'); $table->openTransaction(); if ($data_ids) { diff --git a/src/infrastructure/daemon/workers/PhabricatorWorker.php b/src/infrastructure/daemon/workers/PhabricatorWorker.php index 6d3fcb52e9..0903afa53b 100644 --- a/src/infrastructure/daemon/workers/PhabricatorWorker.php +++ b/src/infrastructure/daemon/workers/PhabricatorWorker.php @@ -194,9 +194,8 @@ abstract class PhabricatorWorker { } } - $tasks = id(new PhabricatorWorkerArchiveTask())->loadAllWhere( - 'id IN (%Ld)', - $task_ids); + $tasks = id(new PhabricatorWorkerArchiveTaskQuery()) + ->withIDs($task_ids); foreach ($tasks as $task) { if ($task->getResult() != PhabricatorWorkerArchiveTask::RESULT_SUCCESS) { diff --git a/src/infrastructure/daemon/workers/management/PhabricatorWorkerManagementWorkflow.php b/src/infrastructure/daemon/workers/management/PhabricatorWorkerManagementWorkflow.php index 96a47eafac..9189489208 100644 --- a/src/infrastructure/daemon/workers/management/PhabricatorWorkerManagementWorkflow.php +++ b/src/infrastructure/daemon/workers/management/PhabricatorWorkerManagementWorkflow.php @@ -24,9 +24,9 @@ abstract class PhabricatorWorkerManagementWorkflow $active_tasks = id(new PhabricatorWorkerActiveTask())->loadAllWhere( 'id IN (%Ls)', $ids); - $archive_tasks = id(new PhabricatorWorkerArchiveTask())->loadAllWhere( - 'id IN (%Ls)', - $ids); + $archive_tasks = id(new PhabricatorWorkerArchiveTaskQuery()) + ->withIDs($ids) + ->execute(); $tasks = mpull($active_tasks, null, 'getID') + diff --git a/src/infrastructure/daemon/workers/query/PhabricatorWorkerArchiveTaskQuery.php b/src/infrastructure/daemon/workers/query/PhabricatorWorkerArchiveTaskQuery.php new file mode 100644 index 0000000000..f026e13456 --- /dev/null +++ b/src/infrastructure/daemon/workers/query/PhabricatorWorkerArchiveTaskQuery.php @@ -0,0 +1,82 @@ +ids = $ids; + return $this; + } + + public function withDateModifiedSince($timestamp) { + $this->dateModifiedSince = $timestamp; + return $this; + } + + public function withDateCreatedBefore($timestamp) { + $this->dateCreatedBefore = $timestamp; + return $this; + } + + public function setLimit($limit) { + $this->limit = $limit; + return $this; + } + + public function execute() { + + $task_table = new PhabricatorWorkerArchiveTask(); + + $conn_r = $task_table->establishConnection('r'); + + $rows = queryfx_all( + $conn_r, + 'SELECT * FROM %T %Q %Q', + $task_table->getTableName(), + $this->buildWhereClause($conn_r), + $this->buildLimitClause($conn_r)); + + return $task_table->loadAllFromArray($rows); + } + + private function buildWhereClause(AphrontDatabaseConnection $conn_r) { + $where = array(); + + if ($this->ids !== null) { + $where[] = qsprintf( + $conn_r, + 'ids in (%Ld)', + $this->ids); + } + + if ($this->dateModifiedSince) { + $where[] = qsprintf( + $conn_r, + 'dateModified > %d', + $this->dateModifiedSince); + } + + if ($this->dateCreatedBefore) { + $where[] = qsprintf( + $conn_r, + 'dateCreated < %d', + $this->dateCreatedBefore); + } + + return $this->formatWhereClause($where); + } + + private function buildLimitClause(AphrontDatabaseConnection $conn_r) { + $clause = ''; + if ($this->limit) { + $clause = qsprintf($conn_r, 'LIMIT %d', $this->limit); + } + return $clause; + } + +}