From 8b553d2f183b83483deb453c3fa6dd7575a7eeef Mon Sep 17 00:00:00 2001 From: epriestley Date: Fri, 24 Mar 2017 13:25:22 -0700 Subject: [PATCH] Allow taskmaster daemons to hibernate Summary: Ref T12298. Like PullLocal daemons, this allows the last daemon in the pool to hibernate if there's no work to be done, and awakens the pool when work arrives. Test Plan: - Ran `bin/phd debug task --trace`. - Saw the pool hibernate and look for tasks. - Commented on an object. - Saw the pool wake up and process the queue. Reviewers: chad Reviewed By: chad Maniphest Tasks: T12298 Differential Revision: https://secure.phabricator.com/D17559 --- src/__phutil_library_map__.php | 2 ++ .../workers/PhabricatorTaskmasterDaemon.php | 5 +++ .../PhabricatorTaskmasterDaemonModule.php | 33 +++++++++++++++++++ 3 files changed, 40 insertions(+) create mode 100644 src/infrastructure/daemon/workers/PhabricatorTaskmasterDaemonModule.php diff --git a/src/__phutil_library_map__.php b/src/__phutil_library_map__.php index f137d1b221..30f6020dd2 100644 --- a/src/__phutil_library_map__.php +++ b/src/__phutil_library_map__.php @@ -3989,6 +3989,7 @@ phutil_register_library_map(array( 'PhabricatorTOTPAuthFactor' => 'applications/auth/factor/PhabricatorTOTPAuthFactor.php', 'PhabricatorTOTPAuthFactorTestCase' => 'applications/auth/factor/__tests__/PhabricatorTOTPAuthFactorTestCase.php', 'PhabricatorTaskmasterDaemon' => 'infrastructure/daemon/workers/PhabricatorTaskmasterDaemon.php', + 'PhabricatorTaskmasterDaemonModule' => 'infrastructure/daemon/workers/PhabricatorTaskmasterDaemonModule.php', 'PhabricatorTestApplication' => 'applications/base/controller/__tests__/PhabricatorTestApplication.php', 'PhabricatorTestCase' => 'infrastructure/testing/PhabricatorTestCase.php', 'PhabricatorTestController' => 'applications/base/controller/__tests__/PhabricatorTestController.php', @@ -9318,6 +9319,7 @@ phutil_register_library_map(array( 'PhabricatorTOTPAuthFactor' => 'PhabricatorAuthFactor', 'PhabricatorTOTPAuthFactorTestCase' => 'PhabricatorTestCase', 'PhabricatorTaskmasterDaemon' => 'PhabricatorDaemon', + 'PhabricatorTaskmasterDaemonModule' => 'PhutilDaemonOverseerModule', 'PhabricatorTestApplication' => 'PhabricatorApplication', 'PhabricatorTestCase' => 'PhutilTestCase', 'PhabricatorTestController' => 'PhabricatorController', diff --git a/src/infrastructure/daemon/workers/PhabricatorTaskmasterDaemon.php b/src/infrastructure/daemon/workers/PhabricatorTaskmasterDaemon.php index 49e283c946..6cbbd8698e 100644 --- a/src/infrastructure/daemon/workers/PhabricatorTaskmasterDaemon.php +++ b/src/infrastructure/daemon/workers/PhabricatorTaskmasterDaemon.php @@ -43,6 +43,11 @@ final class PhabricatorTaskmasterDaemon extends PhabricatorDaemon { $sleep = 0; } else { + + if ($this->shouldHibernate(60)) { + break; + } + // When there's no work, sleep for one second. The pool will // autoscale down if we're continuously idle for an extended period // of time. diff --git a/src/infrastructure/daemon/workers/PhabricatorTaskmasterDaemonModule.php b/src/infrastructure/daemon/workers/PhabricatorTaskmasterDaemonModule.php new file mode 100644 index 0000000000..ddd0e082bb --- /dev/null +++ b/src/infrastructure/daemon/workers/PhabricatorTaskmasterDaemonModule.php @@ -0,0 +1,33 @@ +getPoolDaemonClass(); + + if ($class != 'PhabricatorTaskmasterDaemon') { + return false; + } + + if ($this->shouldThrottle($class, 1)) { + return false; + } + + $table = new PhabricatorWorkerActiveTask(); + $conn = $table->establishConnection('r'); + + $row = queryfx_one( + $conn, + 'SELECT id FROM %T WHERE leaseOwner IS NULL + OR leaseExpires <= %d LIMIT 1', + $table->getTableName(), + PhabricatorTime::getNow()); + if (!$row) { + return false; + } + + return true; + } + +}