1
0
Fork 0
mirror of https://we.phorge.it/source/phorge.git synced 2025-01-10 23:01:04 +01:00
phorge-phorge/src/infrastructure/daemon/workers/PhabricatorTaskmasterDaemon.php
epriestley 9309723ac4 Send graceful shutdown signals to daemons in Phabricator
Summary:
Fixes T5855. Adds a `--graceful N` flag to `phd stop` and `phd restart`.

`phd` will send SIGINT, wait `N` seconds, SIGTERM, wait 15 seconds, and SIGKILL. By default, `N` is 15.

Test Plan:
  - Ran `bin/phd debug ...` and used `^C` to interrupt daemons. Saw graceful shutdown behavior, and abrupt termination on multiple `^C`.
  - Ran `bin/phd start`, `bin/phd stop` and `bin/phd restart` with `--graceful` set to various things, notably `0`. Saw graceful shutdowns on the CLI and in the web UI. With `0`, abrupt shutdowns.

Reviewers: btrahan, hach-que

Reviewed By: hach-que

Subscribers: epriestley

Maniphest Tasks: T5855

Differential Revision: https://secure.phabricator.com/D10228
2014-08-11 20:18:31 -07:00

46 lines
1.2 KiB
PHP

<?php
final class PhabricatorTaskmasterDaemon extends PhabricatorDaemon {
public function run() {
$sleep = 0;
do {
$tasks = id(new PhabricatorWorkerLeaseQuery())
->setLimit(1)
->execute();
if ($tasks) {
foreach ($tasks as $task) {
$id = $task->getID();
$class = $task->getTaskClass();
$this->log("Working on task {$id} ({$class})...");
$task = $task->executeTask();
$ex = $task->getExecutionException();
if ($ex) {
if ($ex instanceof PhabricatorWorkerPermanentFailureException) {
$this->log("Task {$id} failed permanently.");
} else if ($ex instanceof PhabricatorWorkerYieldException) {
$this->log(pht('Task %s yielded.', $id));
} else {
$this->log("Task {$id} failed!");
throw new PhutilProxyException(
"Error while executing task ID {$id} from queue.",
$ex);
}
} else {
$this->log("Task {$id} complete! Moved to archive.");
}
}
$sleep = 0;
} else {
$sleep = min($sleep + 1, 30);
}
$this->sleep($sleep);
} while (!$this->shouldExit());
}
}