@recaptime-dev's working patches + fork for Phorge, a community fork of Phabricator. (Upstream dev and stable branches are at upstream/main and upstream/stable respectively.)
hq.recaptime.dev/wiki/Phorge
phorge
phabricator
1<?php
2
3abstract class PhabricatorWorkerTaskQuery
4 extends PhabricatorQuery {
5
6 private $ids;
7 private $dateModifiedSince;
8 private $dateCreatedBefore;
9 private $objectPHIDs;
10 private $containerPHIDs;
11 private $classNames;
12 private $limit;
13 private $minFailureCount;
14 private $maxFailureCount;
15 private $minPriority;
16 private $maxPriority;
17
18 public function withIDs(array $ids) {
19 $this->ids = $ids;
20 return $this;
21 }
22
23 public function withDateModifiedSince($timestamp) {
24 $this->dateModifiedSince = $timestamp;
25 return $this;
26 }
27
28 public function withDateCreatedBefore($timestamp) {
29 $this->dateCreatedBefore = $timestamp;
30 return $this;
31 }
32
33 public function withObjectPHIDs(array $phids) {
34 $this->objectPHIDs = $phids;
35 return $this;
36 }
37
38 public function withContainerPHIDs(array $phids) {
39 $this->containerPHIDs = $phids;
40 return $this;
41 }
42
43 public function withClassNames(array $names) {
44 $this->classNames = $names;
45 return $this;
46 }
47
48 public function withFailureCountBetween($min, $max) {
49 $this->minFailureCount = $min;
50 $this->maxFailureCount = $max;
51 return $this;
52 }
53
54 public function withPriorityBetween($min, $max) {
55 $this->minPriority = $min;
56 $this->maxPriority = $max;
57 return $this;
58 }
59
60 public function setLimit($limit) {
61 $this->limit = $limit;
62 return $this;
63 }
64
65 protected function buildWhereClause(AphrontDatabaseConnection $conn) {
66 $where = array();
67
68 if ($this->ids !== null) {
69 $where[] = qsprintf(
70 $conn,
71 'id in (%Ld)',
72 $this->ids);
73 }
74
75 if ($this->objectPHIDs !== null) {
76 $where[] = qsprintf(
77 $conn,
78 'objectPHID IN (%Ls)',
79 $this->objectPHIDs);
80 }
81
82 if ($this->containerPHIDs !== null) {
83 $where[] = qsprintf(
84 $conn,
85 'containerPHID IN (%Ls)',
86 $this->containerPHIDs);
87 }
88
89 if ($this->dateModifiedSince !== null) {
90 $where[] = qsprintf(
91 $conn,
92 'dateModified > %d',
93 $this->dateModifiedSince);
94 }
95
96 if ($this->dateCreatedBefore !== null) {
97 $where[] = qsprintf(
98 $conn,
99 'dateCreated < %d',
100 $this->dateCreatedBefore);
101 }
102
103 if ($this->classNames !== null) {
104 $where[] = qsprintf(
105 $conn,
106 'taskClass IN (%Ls)',
107 $this->classNames);
108 }
109
110 if ($this->minFailureCount !== null) {
111 $where[] = qsprintf(
112 $conn,
113 'failureCount >= %d',
114 $this->minFailureCount);
115 }
116
117 if ($this->maxFailureCount !== null) {
118 $where[] = qsprintf(
119 $conn,
120 'failureCount <= %d',
121 $this->maxFailureCount);
122 }
123
124 if ($this->minPriority !== null) {
125 $where[] = qsprintf(
126 $conn,
127 'priority >= %d',
128 $this->minPriority);
129 }
130
131 if ($this->maxPriority !== null) {
132 $where[] = qsprintf(
133 $conn,
134 'priority <= %d',
135 $this->maxPriority);
136 }
137
138 return $this->formatWhereClause($conn, $where);
139 }
140
141 protected function buildOrderClause(AphrontDatabaseConnection $conn) {
142 // NOTE: The garbage collector executes this query with a date constraint,
143 // and the query is inefficient if we don't use the same key for ordering.
144 // See T9808 for discussion.
145
146 if ($this->dateCreatedBefore) {
147 return qsprintf($conn, 'ORDER BY dateCreated DESC, id DESC');
148 } else if ($this->dateModifiedSince) {
149 return qsprintf($conn, 'ORDER BY dateModified DESC, id DESC');
150 } else {
151 return qsprintf($conn, 'ORDER BY id DESC');
152 }
153 }
154
155 protected function buildLimitClause(AphrontDatabaseConnection $conn) {
156 if ($this->limit) {
157 return qsprintf($conn, 'LIMIT %d', $this->limit);
158 } else {
159 return qsprintf($conn, '');
160 }
161 }
162
163}