Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
freebsd
GitHub Repository: freebsd/phabricator
Path: blob/master/src/infrastructure/daemon/PhutilDaemonOverseerModule.php
12241 views
1
<?php
2
3
/**
4
* Overseer modules allow daemons to be externally influenced.
5
*
6
* See @{class:PhabricatorDaemonOverseerModule} for a concrete example.
7
*/
8
abstract 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(__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 Throttle key.
51
* @param float 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
}
72
73