@recaptime-dev's working patches + fork for Phorge, a community fork of Phabricator. (Upstream dev and stable branches are at upstream/main and upstream/stable respectively.)
hq.recaptime.dev/wiki/Phorge
phorge
phabricator
1<?php
2
3/**
4 * Overseer modules allow daemons to be externally influenced.
5 *
6 * See @{class:PhabricatorDaemonOverseerModule} for a concrete example.
7 */
8abstract class PhutilDaemonOverseerModule extends Phobject {
9
10 private $throttles = array();
11
12
13 /**
14 * This method is used to indicate to the overseer that daemons should reload.
15 *
16 * @return bool True if the daemons should reload, otherwise false.
17 */
18 public function shouldReloadDaemons() {
19 return false;
20 }
21
22
23 /**
24 * Should a hibernating daemon pool be awoken immediately?
25 *
26 * @return bool True to awaken the pool immediately.
27 */
28 public function shouldWakePool(PhutilDaemonPool $pool) {
29 return false;
30 }
31
32
33 public static function getAllModules() {
34 return id(new PhutilClassMapQuery())
35 ->setAncestorClass(self::class)
36 ->execute();
37 }
38
39
40 /**
41 * Throttle checks from executing too often.
42 *
43 * If you throttle a check like this, it will only execute once every 2.5
44 * seconds:
45 *
46 * if ($this->shouldThrottle('some.check', 2.5)) {
47 * return;
48 * }
49 *
50 * @param string $name Throttle key.
51 * @param float $duration Duration in seconds.
52 * @return bool True to throttle the check.
53 */
54 protected function shouldThrottle($name, $duration) {
55 $throttle = idx($this->throttles, $name, 0);
56 $now = microtime(true);
57
58 // If not enough time has elapsed, throttle the check.
59 $elapsed = ($now - $throttle);
60 if ($elapsed < $duration) {
61 return true;
62 }
63
64 // Otherwise, mark the current time as the last time we ran the check,
65 // then let it continue.
66 $this->throttles[$name] = $now;
67
68 return false;
69 }
70
71}