Skip to content

Commit e9af4f8

Browse files
author
epriestley
committedDec 18, 2015
Fix an issue where Drydock followup tasks would not queue if the main task failed
Summary: Ref T9994. This fixes the first issue discussed on that task, which is that when a merge fails after "arc land", we would not clean up all the leases properly. Specifically, when a merge fails, we use `queueTask()` to schedule a followup task. This followup destroys the lease and frees the underlying resource. However, the default behavior of `queueTask()` is to //not queue tasks// if the parent task fails. This is a reasonable, safe behavior that was originally introduced in D8774, where it kept us from sending too much mail if a task did "send some mail" and then failed a little later on and got retried. Since I think the default behavior is correct, I just special cased the behavior for Drydock to make it queue even on failure. These are the only types of followup tasks we currently want to queue on main task failure. (It's possible that future Blueprints might want some kind of more specialized behavior, where some tasks queue only on success, but we can cross that bridge when we come to it.) Test Plan: - See T9994#149878 for test case setup. - I ran that test case again with this patch, and saw the followup task queue properly in the `--trace` log, a correspoinding update task show up in `/daemon/`, and the lease get destroyed when I ran it a moment later. {F1029915} Reviewers: chad Reviewed By: chad Maniphest Tasks: T9994 Differential Revision: https://secure.phabricator.com/D14818
1 parent 5bbf0ba commit e9af4f8

File tree

5 files changed

+59
-16
lines changed

5 files changed

+59
-16
lines changed
 

‎src/applications/drydock/worker/DrydockLeaseUpdateWorker.php

+1
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ protected function doWork() {
2626
$this->handleUpdate($lease);
2727
} catch (Exception $ex) {
2828
$lock->unlock();
29+
$this->flushDrydockTaskQueue();
2930
throw $ex;
3031
}
3132

‎src/applications/drydock/worker/DrydockResourceUpdateWorker.php

+1
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ protected function doWork() {
2424
$this->handleUpdate($resource);
2525
} catch (Exception $ex) {
2626
$lock->unlock();
27+
$this->flushDrydockTaskQueue();
2728
throw $ex;
2829
}
2930

‎src/applications/drydock/worker/DrydockWorker.php

+26
Original file line numberDiff line numberDiff line change
@@ -170,4 +170,30 @@ protected function getYieldDurationFromException(Exception $ex) {
170170
return 15;
171171
}
172172

173+
protected function flushDrydockTaskQueue() {
174+
// NOTE: By default, queued tasks are not scheduled if the current task
175+
// fails. This is a good, safe default behavior. For example, it can
176+
// protect us from executing side effect tasks too many times, like
177+
// sending extra email.
178+
179+
// However, it is not the behavior we want in Drydock, because we queue
180+
// followup tasks after lease and resource failures and want them to
181+
// execute in order to clean things up.
182+
183+
// At least for now, we just explicitly flush the queue before exiting
184+
// with a failure to make sure tasks get queued up properly.
185+
try {
186+
$this->flushTaskQueue();
187+
} catch (Exception $ex) {
188+
// If this fails, we want to swallow the exception so the caller throws
189+
// the original error, since we're more likely to be able to understand
190+
// and fix the problem if we have the original error than if we replace
191+
// it with this one.
192+
phlog($ex);
193+
}
194+
195+
return $this;
196+
}
197+
198+
173199
}

‎src/infrastructure/daemon/workers/PhabricatorWorker.php

+26-6
Original file line numberDiff line numberDiff line change
@@ -158,11 +158,8 @@ final public static function scheduleTask(
158158

159159
while (true) {
160160
try {
161-
$worker->doWork();
162-
foreach ($worker->getQueuedTasks() as $queued_task) {
163-
list($queued_class, $queued_data, $queued_options) = $queued_task;
164-
self::scheduleTask($queued_class, $queued_data, $queued_options);
165-
}
161+
$worker->executeTask();
162+
$worker->flushTaskQueue();
166163
break;
167164
} catch (PhabricatorWorkerYieldException $ex) {
168165
phlog(
@@ -236,11 +233,34 @@ final protected function queueTask(
236233
*
237234
* @return list<tuple<string, wild, int|null>> Queued task specifications.
238235
*/
239-
final public function getQueuedTasks() {
236+
final protected function getQueuedTasks() {
240237
return $this->queuedTasks;
241238
}
242239

243240

241+
/**
242+
* Schedule any queued tasks, then empty the task queue.
243+
*
244+
* By default, the queue is flushed only if a task succeeds. You can call
245+
* this method to force the queue to flush before failing (for example, if
246+
* you are using queues to improve locking behavior).
247+
*
248+
* @param map<string, wild> Optional default options.
249+
* @return this
250+
*/
251+
final public function flushTaskQueue($defaults = array()) {
252+
foreach ($this->getQueuedTasks() as $task) {
253+
list($class, $data, $options) = $task;
254+
255+
$options = $options + $defaults;
256+
257+
self::scheduleTask($class, $data, $options);
258+
}
259+
260+
$this->queuedTasks = array();
261+
}
262+
263+
244264
/**
245265
* Awaken tasks that have yielded.
246266
*

‎src/infrastructure/daemon/workers/storage/PhabricatorWorkerActiveTask.php

+5-10
Original file line numberDiff line numberDiff line change
@@ -216,16 +216,11 @@ public function executeTask() {
216216
// NOTE: If this throws, we don't want it to cause the task to fail again,
217217
// so execute it out here and just let the exception escape.
218218
if ($did_succeed) {
219-
foreach ($worker->getQueuedTasks() as $task) {
220-
list($class, $data, $options) = $task;
221-
222-
// Default the new task priority to our own priority.
223-
$options = $options + array(
224-
'priority' => (int)$this->getPriority(),
225-
);
226-
227-
PhabricatorWorker::scheduleTask($class, $data, $options);
228-
}
219+
// Default the new task priority to our own priority.
220+
$defaults = array(
221+
'priority' => (int)$this->getPriority(),
222+
);
223+
$worker->flushTaskQueue($defaults);
229224
}
230225

231226
return $result;

0 commit comments

Comments
 (0)
Failed to load comments.