1
0
Fork 0
mirror of https://we.phorge.it/source/phorge.git synced 2024-12-23 22:10:55 +01:00

Allow worker tasks to be cancelled by classname

Summary:
Ref T3554. Makes `bin/worker cancel --class <classname>` work (cancel all tasks with that type).

This is useful in development if your queue is full of a bunch of gunk, and a need has occasionally arisen in production environments (usually "one option is cancel everything and move on").

Test Plan: Ran `bin/worker cancel` to cancel blocks of tasks by class name.

Reviewers: chad

Reviewed By: chad

Maniphest Tasks: T3554

Differential Revision: https://secure.phabricator.com/D16267
This commit is contained in:
epriestley 2016-07-11 07:18:41 -07:00
parent c21be4849f
commit c510c925cf
2 changed files with 52 additions and 13 deletions

View file

@ -11,31 +11,57 @@ abstract class PhabricatorWorkerManagementWorkflow
'repeat' => true, 'repeat' => true,
'help' => pht('Select one or more tasks by ID.'), 'help' => pht('Select one or more tasks by ID.'),
), ),
array(
'name' => 'class',
'param' => 'name',
'help' => pht('Select all tasks of a given class.'),
),
); );
} }
protected function loadTasks(PhutilArgumentParser $args) { protected function loadTasks(PhutilArgumentParser $args) {
$ids = $args->getArg('id'); $ids = $args->getArg('id');
if (!$ids) { $class = $args->getArg('class');
if (!$ids && !$class) {
throw new PhutilArgumentUsageException( throw new PhutilArgumentUsageException(
pht('Use --id to select tasks by ID.')); pht('Use --id or --class to select tasks.'));
} if ($ids && $class) {
throw new PhutilArgumentUsageException(
pht('Use one of --id or --class to select tasks, but not both.'));
} }
$active_tasks = id(new PhabricatorWorkerActiveTask())->loadAllWhere( if ($ids) {
'id IN (%Ls)', $active_tasks = id(new PhabricatorWorkerActiveTask())->loadAllWhere(
$ids); 'id IN (%Ls)',
$archive_tasks = id(new PhabricatorWorkerArchiveTaskQuery()) $ids);
->withIDs($ids) $archive_tasks = id(new PhabricatorWorkerArchiveTaskQuery())
->execute(); ->withIDs($ids)
->execute();
} else {
$active_tasks = id(new PhabricatorWorkerActiveTask())->loadAllWhere(
'taskClass IN (%Ls)',
array($class));
$archive_tasks = id(new PhabricatorWorkerArchiveTaskQuery())
->withClassNames(array($class))
->execute();
}
$tasks = $tasks =
mpull($active_tasks, null, 'getID') + mpull($active_tasks, null, 'getID') +
mpull($archive_tasks, null, 'getID'); mpull($archive_tasks, null, 'getID');
foreach ($ids as $id) { if ($ids) {
if (empty($tasks[$id])) { foreach ($ids as $id) {
if (empty($tasks[$id])) {
throw new PhutilArgumentUsageException(
pht('No task exists with id "%s"!', $id));
}
}
} else {
if (!$tasks) {
throw new PhutilArgumentUsageException( throw new PhutilArgumentUsageException(
pht('No task exists with id "%s"!', $id)); pht('No task exists with class "%s"!', $class));
} }
} }

View file

@ -7,6 +7,7 @@ final class PhabricatorWorkerArchiveTaskQuery
private $dateModifiedSince; private $dateModifiedSince;
private $dateCreatedBefore; private $dateCreatedBefore;
private $objectPHIDs; private $objectPHIDs;
private $classNames;
private $limit; private $limit;
public function withIDs(array $ids) { public function withIDs(array $ids) {
@ -29,6 +30,11 @@ final class PhabricatorWorkerArchiveTaskQuery
return $this; return $this;
} }
public function withClassNames(array $names) {
$this->classNames = $names;
return $this;
}
public function setLimit($limit) { public function setLimit($limit) {
$this->limit = $limit; $this->limit = $limit;
return $this; return $this;
@ -67,20 +73,27 @@ final class PhabricatorWorkerArchiveTaskQuery
$this->objectPHIDs); $this->objectPHIDs);
} }
if ($this->dateModifiedSince) { if ($this->dateModifiedSince !== null) {
$where[] = qsprintf( $where[] = qsprintf(
$conn_r, $conn_r,
'dateModified > %d', 'dateModified > %d',
$this->dateModifiedSince); $this->dateModifiedSince);
} }
if ($this->dateCreatedBefore) { if ($this->dateCreatedBefore !== null) {
$where[] = qsprintf( $where[] = qsprintf(
$conn_r, $conn_r,
'dateCreated < %d', 'dateCreated < %d',
$this->dateCreatedBefore); $this->dateCreatedBefore);
} }
if ($this->classNames !== null) {
$where[] = qsprintf(
$conn_r,
'taskClass IN (%Ls)',
$this->classNames);
}
return $this->formatWhereClause($where); return $this->formatWhereClause($where);
} }