Page Menu
Home
Phabricator
Search
Configure Global Search
Log In
Files
F15429786
D11089.id26626.diff
No One
Temporary
Actions
View File
Edit File
Delete File
View Transforms
Subscribe
Mute Notifications
Award Token
Flag For Later
Size
7 KB
Referenced Files
None
Subscribers
None
D11089.id26626.diff
View Options
diff --git a/src/applications/daemon/controller/PhabricatorDaemonConsoleController.php b/src/applications/daemon/controller/PhabricatorDaemonConsoleController.php
--- a/src/applications/daemon/controller/PhabricatorDaemonConsoleController.php
+++ b/src/applications/daemon/controller/PhabricatorDaemonConsoleController.php
@@ -131,8 +131,11 @@
$daemon_panel->appendChild($daemon_table);
- $tasks = id(new PhabricatorWorkerActiveTask())->loadAllWhere(
- 'leaseOwner IS NOT NULL');
+ $tasks = id(new PhabricatorWorkerLeaseQuery())
+ ->setSkipLease(true)
+ ->withLeasedTasks(true)
+ ->setLimit(100)
+ ->execute();
$tasks_table = id(new PhabricatorDaemonTasksTableView())
->setTasks($tasks)
diff --git a/src/infrastructure/daemon/workers/query/PhabricatorWorkerArchiveTaskQuery.php b/src/infrastructure/daemon/workers/query/PhabricatorWorkerArchiveTaskQuery.php
--- a/src/infrastructure/daemon/workers/query/PhabricatorWorkerArchiveTaskQuery.php
+++ b/src/infrastructure/daemon/workers/query/PhabricatorWorkerArchiveTaskQuery.php
@@ -6,6 +6,7 @@
private $ids;
private $dateModifiedSince;
private $dateCreatedBefore;
+ private $objectPHIDs;
private $limit;
public function withIDs(array $ids) {
@@ -23,20 +24,24 @@
return $this;
}
+ public function withObjectPHIDs(array $phids) {
+ $this->objectPHIDs = $phids;
+ 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',
+ 'SELECT * FROM %T %Q ORDER BY id DESC %Q',
$task_table->getTableName(),
$this->buildWhereClause($conn_r),
$this->buildLimitClause($conn_r));
@@ -54,6 +59,13 @@
$this->ids);
}
+ if ($this->objectPHIDs !== null) {
+ $where[] = qsprintf(
+ $conn_r,
+ 'objectPHID IN (%Ls)',
+ $this->objectPHIDs);
+ }
+
if ($this->dateModifiedSince) {
$where[] = qsprintf(
$conn_r,
diff --git a/src/infrastructure/daemon/workers/query/PhabricatorWorkerLeaseQuery.php b/src/infrastructure/daemon/workers/query/PhabricatorWorkerLeaseQuery.php
--- a/src/infrastructure/daemon/workers/query/PhabricatorWorkerLeaseQuery.php
+++ b/src/infrastructure/daemon/workers/query/PhabricatorWorkerLeaseQuery.php
@@ -6,12 +6,14 @@
final class PhabricatorWorkerLeaseQuery extends PhabricatorQuery {
const PHASE_UNLEASED = 'unleased';
+ const PHASE_LEASED = 'leased';
const PHASE_EXPIRED = 'expired';
private $ids;
private $objectPHIDs;
private $limit;
private $skipLease;
+ private $leased = false;
public static function getDefaultWaitBeforeRetry() {
return phutil_units('5 minutes in seconds');
@@ -45,6 +47,26 @@
return $this;
}
+ /**
+ * Select only leased tasks, only unleased tasks, or both types of task.
+ *
+ * By default, queries select only unleased tasks (equivalent to passing
+ * `false` to this method). You can pass `true` to select only leased tasks,
+ * or `null` to ignore the lease status of tasks.
+ *
+ * If your result set potentially includes leased tasks, you must disable
+ * leasing using @{method:setSkipLease}. These options are intended for use
+ * when displaying task status information.
+ *
+ * @param mixed `true` to select only leased tasks, `false` to select only
+ * unleased tasks (default), or `null` to select both.
+ * @return this
+ */
+ public function withLeasedTasks($leased) {
+ $this->leased = $leased;
+ return $this;
+ }
+
public function setLimit($limit) {
$this->limit = $limit;
return $this;
@@ -52,7 +74,17 @@
public function execute() {
if (!$this->limit) {
- throw new Exception('You must setLimit() when leasing tasks.');
+ throw new Exception(
+ pht('You must setLimit() when leasing tasks.'));
+ }
+
+ if ($this->leased !== false) {
+ if (!$this->skipLease) {
+ throw new Exception(
+ pht(
+ 'If you potentially select leased tasks using withLeasedTasks(), '.
+ 'you MUST disable lease acquisition by calling setSkipLease().'));
+ }
}
$task_table = new PhabricatorWorkerActiveTask();
@@ -65,10 +97,17 @@
// find enough tasks, try tasks with expired leases (i.e., tasks which have
// previously failed).
- $phases = array(
- self::PHASE_UNLEASED,
- self::PHASE_EXPIRED,
- );
+ // If we're selecting leased tasks, look for them in between the two other
+ // groups.
+
+ $phases = array();
+ if ($this->leased !== false) {
+ $phases[] = self::PHASE_LEASED;
+ }
+ if ($this->leased !== true) {
+ $phases[] = self::PHASE_UNLEASED;
+ $phases[] = self::PHASE_EXPIRED;
+ }
$limit = $this->limit;
$leased = 0;
@@ -170,6 +209,10 @@
case self::PHASE_UNLEASED:
$where[] = 'leaseOwner IS NULL';
break;
+ case self::PHASE_LEASED:
+ $where[] = 'leaseOwner IS NOT NULL';
+ $where[] = 'leaseExpires >= UNIX_TIMESTAMP()';
+ break;
case self::PHASE_EXPIRED:
$where[] = 'leaseExpires < UNIX_TIMESTAMP()';
break;
@@ -177,7 +220,7 @@
throw new Exception("Unknown phase '{$phase}'!");
}
- if ($this->ids) {
+ if ($this->ids !== null) {
$where[] = qsprintf($conn_w, 'id IN (%Ld)', $this->ids);
}
@@ -203,6 +246,11 @@
$where[] = qsprintf($conn_w, 'leaseOwner IS NULL');
$where[] = qsprintf($conn_w, 'id IN (%Ld)', ipull($rows, 'id'));
break;
+ case self::PHASE_LEASED:
+ throw new Exception(
+ pht(
+ 'Trying to lease tasks selected in the leased phase! This is '.
+ 'intended to be imposssible.'));
case self::PHASE_EXPIRED:
$in = array();
foreach ($rows as $row) {
@@ -227,6 +275,10 @@
// When selecting new tasks, we want to consume them in order of
// increasing priority (and then FIFO).
return qsprintf($conn_w, 'ORDER BY priority ASC, id ASC');
+ case self::PHASE_LEASED:
+ // Ideally we'd probably order these by lease acquisition time, but
+ // we don't have that handy and this is a good approximation.
+ return qsprintf($conn_w, 'ORDER BY priority ASC, id ASC');
case self::PHASE_EXPIRED:
// When selecting failed tasks, we want to consume them in roughly
// FIFO order of their failures, which is not necessarily their original
diff --git a/src/infrastructure/daemon/workers/storage/PhabricatorWorkerActiveTask.php b/src/infrastructure/daemon/workers/storage/PhabricatorWorkerActiveTask.php
--- a/src/infrastructure/daemon/workers/storage/PhabricatorWorkerActiveTask.php
+++ b/src/infrastructure/daemon/workers/storage/PhabricatorWorkerActiveTask.php
@@ -119,6 +119,7 @@
->setFailureCount($this->getFailureCount())
->setDataID($this->getDataID())
->setPriority($this->getPriority())
+ ->setObjectPHID($this->getObjectPHID())
->setResult($result)
->setDuration($duration);
diff --git a/src/infrastructure/daemon/workers/storage/PhabricatorWorkerArchiveTask.php b/src/infrastructure/daemon/workers/storage/PhabricatorWorkerArchiveTask.php
--- a/src/infrastructure/daemon/workers/storage/PhabricatorWorkerArchiveTask.php
+++ b/src/infrastructure/daemon/workers/storage/PhabricatorWorkerArchiveTask.php
@@ -84,6 +84,7 @@
->setFailureCount(0)
->setDataID($this->getDataID())
->setPriority($this->getPriority())
+ ->setObjectPHID($this->getObjectPHID())
->insert();
$this->setDataID(null);
File Metadata
Details
Attached
Mime Type
text/plain
Expires
Tue, Mar 25, 4:17 AM (1 w, 4 d ago)
Storage Engine
blob
Storage Format
Encrypted (AES-256-CBC)
Storage Handle
7704277
Default Alt Text
D11089.id26626.diff (7 KB)
Attached To
Mode
D11089: Allow archived tasks to be queried by object PHID and order by id
Attached
Detach File
Event Timeline
Log In to Comment