Differential D21534 Diff 51255 src/infrastructure/daemon/workers/management/PhabricatorWorkerManagementExecuteWorkflow.php
Changeset View
Changeset View
Standalone View
Standalone View
src/infrastructure/daemon/workers/management/PhabricatorWorkerManagementExecuteWorkflow.php
| <?php | <?php | ||||
| final class PhabricatorWorkerManagementExecuteWorkflow | final class PhabricatorWorkerManagementExecuteWorkflow | ||||
| extends PhabricatorWorkerManagementWorkflow { | extends PhabricatorWorkerManagementWorkflow { | ||||
| protected function didConstruct() { | protected function didConstruct() { | ||||
| $this | $this | ||||
| ->setName('execute') | ->setName('execute') | ||||
| ->setExamples('**execute** --id __id__') | ->setExamples('**execute** __selectors__') | ||||
| ->setSynopsis( | ->setSynopsis( | ||||
| pht( | pht( | ||||
| 'Execute a task explicitly. This command ignores leases, is '. | 'Execute a task explicitly. This command ignores leases, is '. | ||||
| 'dangerous, and may cause work to be performed twice.')) | 'dangerous, and may cause work to be performed twice.')) | ||||
| ->setArguments( | ->setArguments( | ||||
| array_merge( | array_merge( | ||||
| array( | array( | ||||
| array( | array( | ||||
| 'name' => 'retry', | 'name' => 'retry', | ||||
| 'help' => pht('Retry archived tasks.'), | 'help' => pht('Retry archived tasks.'), | ||||
| ), | ), | ||||
| array( | array( | ||||
| 'name' => 'repeat', | 'name' => 'repeat', | ||||
| 'help' => pht('Repeat archived, successful tasks.'), | 'help' => pht('Repeat archived, successful tasks.'), | ||||
| ), | ), | ||||
| ), | ), | ||||
| $this->getTaskSelectionArguments())); | $this->getTaskSelectionArguments())); | ||||
| } | } | ||||
| public function execute(PhutilArgumentParser $args) { | public function execute(PhutilArgumentParser $args) { | ||||
| $console = PhutilConsole::getConsole(); | |||||
| $tasks = $this->loadTasks($args); | |||||
| $is_retry = $args->getArg('retry'); | $is_retry = $args->getArg('retry'); | ||||
| $is_repeat = $args->getArg('repeat'); | $is_repeat = $args->getArg('repeat'); | ||||
| $tasks = $this->loadTasks($args); | |||||
| if (!$tasks) { | |||||
| $this->logWarn( | |||||
| pht('NO TASKS'), | |||||
| pht('No tasks selected to execute.')); | |||||
| return 0; | |||||
| } | |||||
| $execute_count = 0; | |||||
| foreach ($tasks as $task) { | foreach ($tasks as $task) { | ||||
| $can_execute = !$task->isArchived(); | $can_execute = !$task->isArchived(); | ||||
| if (!$can_execute) { | if (!$can_execute) { | ||||
| if (!$is_retry) { | if (!$is_retry) { | ||||
| $console->writeOut( | $this->logWarn( | ||||
| "**<bg:yellow> %s </bg>** %s\n", | |||||
| pht('ARCHIVED'), | pht('ARCHIVED'), | ||||
| pht( | pht( | ||||
| '%s is already archived, and will not be executed. '. | '%s is already archived, and will not be executed. '. | ||||
| 'Use "--retry" to execute archived tasks.', | 'Use "--retry" to execute archived tasks.', | ||||
| $this->describeTask($task))); | $this->describeTask($task))); | ||||
| continue; | continue; | ||||
| } | } | ||||
| $result_success = PhabricatorWorkerArchiveTask::RESULT_SUCCESS; | $result_success = PhabricatorWorkerArchiveTask::RESULT_SUCCESS; | ||||
| if ($task->getResult() == $result_success) { | if ($task->getResult() == $result_success) { | ||||
| if (!$is_repeat) { | if (!$is_repeat) { | ||||
| $console->writeOut( | $this->logWarn( | ||||
| "**<bg:yellow> %s </bg>** %s\n", | |||||
| pht('SUCCEEDED'), | pht('SUCCEEDED'), | ||||
| pht( | pht( | ||||
| '%s has already succeeded, and will not be retried. '. | '%s has already succeeded, and will not be retried. '. | ||||
| 'Use "--repeat" to repeat successful tasks.', | 'Use "--repeat" to repeat successful tasks.', | ||||
| $this->describeTask($task))); | $this->describeTask($task))); | ||||
| continue; | continue; | ||||
| } | } | ||||
| } | } | ||||
| echo tsprintf( | $this->logInfo( | ||||
| "**<bg:yellow> %s </bg>** %s\n", | pht('UNARCHIVING'), | ||||
| pht('ARCHIVED'), | |||||
| pht( | pht( | ||||
| 'Unarchiving %s.', | 'Unarchiving %s.', | ||||
| $this->describeTask($task))); | $this->describeTask($task))); | ||||
| $task = $task->unarchiveTask(); | $task = $task->unarchiveTask(); | ||||
| } | } | ||||
| // NOTE: This ignores leases, maybe it should respect them without | // NOTE: This ignores leases, maybe it should respect them without | ||||
| // a parameter like --force? | // a parameter like --force? | ||||
| $task->setLeaseOwner(null); | $task | ||||
| $task->setLeaseExpires(PhabricatorTime::getNow()); | ->setLeaseOwner(null) | ||||
| $task->save(); | ->setLeaseExpires(PhabricatorTime::getNow()) | ||||
| ->save(); | |||||
| $task_data = id(new PhabricatorWorkerTaskData())->loadOneWhere( | $task_data = id(new PhabricatorWorkerTaskData())->loadOneWhere( | ||||
| 'id = %d', | 'id = %d', | ||||
| $task->getDataID()); | $task->getDataID()); | ||||
| $task->setData($task_data->getData()); | $task->setData($task_data->getData()); | ||||
| echo tsprintf( | $this->logInfo( | ||||
| "%s\n", | pht('EXECUTE'), | ||||
| pht( | pht( | ||||
| 'Executing task %d (%s)...', | 'Executing %s...', | ||||
| $task->getID(), | $this->describeTask($task))); | ||||
| $task->getTaskClass())); | |||||
| $task = $task->executeTask(); | $task = $task->executeTask(); | ||||
| $ex = $task->getExecutionException(); | |||||
| $ex = $task->getExecutionException(); | |||||
| if ($ex) { | if ($ex) { | ||||
| throw $ex; | throw $ex; | ||||
| } | } | ||||
| $execute_count++; | |||||
| } | } | ||||
| $this->logOkay( | |||||
| pht('DONE'), | |||||
| pht('Executed %s task(s).', new PhutilNumber($execute_count))); | |||||
| return 0; | return 0; | ||||
| } | } | ||||
| } | } | ||||