Differential D14219 Diff 34352 src/infrastructure/daemon/garbagecollector/PhabricatorGarbageCollector.php
Changeset View
Changeset View
Standalone View
Standalone View
src/infrastructure/daemon/garbagecollector/PhabricatorGarbageCollector.php
| Show First 20 Lines • Show All 41 Lines • ▼ Show 20 Lines | /* -( Getting Collector Information )-------------------------------------- */ | ||||
| * @task info | * @task info | ||||
| */ | */ | ||||
| public function getDefaultRetentionPolicy() { | public function getDefaultRetentionPolicy() { | ||||
| throw new PhutilMethodNotImplementedException(); | throw new PhutilMethodNotImplementedException(); | ||||
| } | } | ||||
| /** | /** | ||||
| * Get the effective retention policy. | |||||
| * | |||||
| * @return int|null Lifetime, or `null` for indefinite retention. | |||||
| * @task info | |||||
| */ | |||||
| public function getRetentionPolicy() { | |||||
| if ($this->hasAutomaticPolicy()) { | |||||
| throw new Exception( | |||||
| pht( | |||||
| 'Can not get retention policy of collector with automatic '. | |||||
| 'policy.')); | |||||
| } | |||||
| $config = PhabricatorEnv::getEnvConfig('phd.garbage-collection'); | |||||
| $const = $this->getCollectorConstant(); | |||||
| return idx($config, $const, $this->getDefaultRetentionPolicy()); | |||||
| } | |||||
| /** | |||||
| * Get a unique string constant identifying this collector. | * Get a unique string constant identifying this collector. | ||||
| * | * | ||||
| * @return string Collector constant. | * @return string Collector constant. | ||||
| * @task info | * @task info | ||||
| */ | */ | ||||
| final public function getCollectorConstant() { | final public function getCollectorConstant() { | ||||
| return $this->getPhobjectClassConstant('COLLECTORCONST', 64); | return $this->getPhobjectClassConstant('COLLECTORCONST', 64); | ||||
| } | } | ||||
| /* -( Collecting Garbage )------------------------------------------------- */ | /* -( Collecting Garbage )------------------------------------------------- */ | ||||
| /** | /** | ||||
| * Run the collector. | |||||
| * | |||||
| * @return bool True if there is more garbage to collect. | |||||
| * @task collect | |||||
| */ | |||||
| final public function runCollector() { | |||||
| // Don't do anything if this collector is configured with an indefinite | |||||
| // retention policy. | |||||
| if (!$this->hasAutomaticPolicy()) { | |||||
| $policy = $this->getRetentionPolicy(); | |||||
| if (!$policy) { | |||||
| return false; | |||||
| } | |||||
| } | |||||
| return $this->collectGarbage(); | |||||
| } | |||||
| /** | |||||
| * Collect garbage from whatever source this GC handles. | * Collect garbage from whatever source this GC handles. | ||||
| * | * | ||||
| * @return bool True if there is more garbage to collect. | * @return bool True if there is more garbage to collect. | ||||
| * @task collect | * @task collect | ||||
| */ | */ | ||||
| abstract public function collectGarbage(); | abstract protected function collectGarbage(); | ||||
| /** | |||||
| * Get the most recent epoch timestamp that is considered garbage. | |||||
| * | |||||
| * Records older than this should be collected. | |||||
| * | |||||
| * @return int Most recent garbage timestamp. | |||||
| * @task collect | |||||
| */ | |||||
| final protected function getGarbageEpoch() { | |||||
| if ($this->hasAutomaticPolicy()) { | |||||
| throw new Exception( | |||||
| pht( | |||||
| 'Can not get garbage epoch for a collector with an automatic '. | |||||
| 'collection policy.')); | |||||
| } | |||||
| $ttl = $this->getRetentionPolicy(); | |||||
| if (!$ttl) { | |||||
| throw new Exception( | |||||
| pht( | |||||
| 'Can not get garbage epoch for a collector with an indefinite '. | |||||
| 'retention policy.')); | |||||
| } | |||||
| return (PhabricatorTime::getNow() - $ttl); | |||||
| } | |||||
| /** | /** | ||||
| * Load all of the available garbage collectors. | * Load all of the available garbage collectors. | ||||
| * | * | ||||
| * @return list<PhabricatorGarbageCollector> Garbage collectors. | * @return list<PhabricatorGarbageCollector> Garbage collectors. | ||||
| * @task collect | * @task collect | ||||
| */ | */ | ||||
| final public static function getAllCollectors() { | final public static function getAllCollectors() { | ||||
| return id(new PhutilClassMapQuery()) | return id(new PhutilClassMapQuery()) | ||||
| ->setAncestorClass(__CLASS__) | ->setAncestorClass(__CLASS__) | ||||
| ->setUniqueMethod('getCollectorConstant') | ->setUniqueMethod('getCollectorConstant') | ||||
| ->execute(); | ->execute(); | ||||
| } | } | ||||
| } | } | ||||