2011-03-10 13:48:29 -08:00
|
|
|
<?php
|
|
|
|
|
|
|
|
/*
|
2012-03-13 16:21:04 -07:00
|
|
|
* Copyright 2012 Facebook, Inc.
|
2011-03-10 13:48:29 -08:00
|
|
|
*
|
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
* you may not use this file except in compliance with the License.
|
|
|
|
* You may obtain a copy of the License at
|
|
|
|
*
|
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
*
|
|
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
* See the License for the specific language governing permissions and
|
|
|
|
* limitations under the License.
|
|
|
|
*/
|
|
|
|
|
2012-03-13 16:21:04 -07:00
|
|
|
final class PhabricatorTaskmasterDaemon extends PhabricatorDaemon {
|
2011-03-10 13:48:29 -08:00
|
|
|
|
|
|
|
public function run() {
|
|
|
|
$sleep = 0;
|
|
|
|
do {
|
2012-11-01 11:30:16 -07:00
|
|
|
$tasks = id(new PhabricatorWorkerLeaseQuery())
|
|
|
|
->setLimit(1)
|
|
|
|
->execute();
|
2011-03-10 13:48:29 -08:00
|
|
|
|
2012-11-01 11:30:16 -07:00
|
|
|
if ($tasks) {
|
2011-03-10 13:48:29 -08:00
|
|
|
foreach ($tasks as $task) {
|
2012-10-31 15:22:16 -07:00
|
|
|
$id = $task->getID();
|
|
|
|
$class = $task->getTaskClass();
|
|
|
|
|
|
|
|
$this->log("Working on task {$id} ({$class})...");
|
|
|
|
|
2011-03-10 13:48:29 -08:00
|
|
|
// TODO: We should detect if we acquired a task with an expired lease
|
|
|
|
// and log about it / bump up failure count.
|
|
|
|
|
|
|
|
// TODO: We should detect if we acquired a task with an excessive
|
|
|
|
// failure count and fail it permanently.
|
|
|
|
|
2012-11-01 11:30:16 -07:00
|
|
|
$data = $task->getData();
|
2011-03-10 13:48:29 -08:00
|
|
|
try {
|
2012-05-30 16:38:53 -07:00
|
|
|
if (!class_exists($class) ||
|
|
|
|
!is_subclass_of($class, 'PhabricatorWorker')) {
|
2011-03-10 13:48:29 -08:00
|
|
|
throw new Exception(
|
|
|
|
"Task class '{$class}' does not extend PhabricatorWorker.");
|
|
|
|
}
|
|
|
|
$worker = newv($class, array($data));
|
|
|
|
|
|
|
|
$lease = $worker->getRequiredLeaseTime();
|
|
|
|
if ($lease !== null) {
|
|
|
|
$task->setLeaseDuration($lease);
|
|
|
|
}
|
|
|
|
|
2012-10-31 15:22:16 -07:00
|
|
|
$t_start = microtime(true);
|
2011-03-10 13:48:29 -08:00
|
|
|
$worker->executeTask();
|
2012-10-31 15:22:16 -07:00
|
|
|
$t_end = microtime(true);
|
2011-03-10 13:48:29 -08:00
|
|
|
|
2012-10-31 15:22:16 -07:00
|
|
|
$task->archiveTask(
|
|
|
|
PhabricatorWorkerArchiveTask::RESULT_SUCCESS,
|
|
|
|
(int)(1000000 * ($t_end - $t_start)));
|
|
|
|
$this->log("Task {$id} complete! Moved to archive.");
|
2011-03-10 13:48:29 -08:00
|
|
|
} catch (Exception $ex) {
|
|
|
|
$task->setFailureCount($task->getFailureCount() + 1);
|
|
|
|
$task->save();
|
2012-10-31 15:22:16 -07:00
|
|
|
|
|
|
|
$this->log("Task {$id} failed!");
|
2011-03-10 13:48:29 -08:00
|
|
|
throw $ex;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
$sleep = 0;
|
|
|
|
} else {
|
|
|
|
$sleep = min($sleep + 1, 30);
|
|
|
|
}
|
|
|
|
|
|
|
|
$this->sleep($sleep);
|
|
|
|
} while (true);
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|