1
0
Fork 0
mirror of https://we.phorge.it/source/phorge.git synced 2024-12-23 05:50:55 +01:00

Modularize "bin/cache" purgers

Summary: Ref T12859. This is an older command with a lot of hard-coded flags. Modularize cache purging in a modern way so it can be extended.

Test Plan: Ran `bin/cache purge --trace` with various valid and invalid flags.

Reviewers: chad

Reviewed By: chad

Maniphest Tasks: T12859

Differential Revision: https://secure.phabricator.com/D18146
This commit is contained in:
epriestley 2017-06-22 10:41:10 -07:00
parent 519bec3e6f
commit bd3f441098
7 changed files with 161 additions and 93 deletions

View file

@ -2171,6 +2171,7 @@ phutil_register_library_map(array(
'PhabricatorCacheManagementPurgeWorkflow' => 'applications/cache/management/PhabricatorCacheManagementPurgeWorkflow.php',
'PhabricatorCacheManagementWorkflow' => 'applications/cache/management/PhabricatorCacheManagementWorkflow.php',
'PhabricatorCacheMarkupGarbageCollector' => 'applications/cache/garbagecollector/PhabricatorCacheMarkupGarbageCollector.php',
'PhabricatorCachePurger' => 'applications/cache/purger/PhabricatorCachePurger.php',
'PhabricatorCacheSchemaSpec' => 'applications/cache/storage/PhabricatorCacheSchemaSpec.php',
'PhabricatorCacheSetupCheck' => 'applications/config/check/PhabricatorCacheSetupCheck.php',
'PhabricatorCacheSpec' => 'applications/cache/spec/PhabricatorCacheSpec.php',
@ -2320,6 +2321,7 @@ phutil_register_library_map(array(
'PhabricatorCelerityApplication' => 'applications/celerity/application/PhabricatorCelerityApplication.php',
'PhabricatorCelerityTestCase' => '__tests__/PhabricatorCelerityTestCase.php',
'PhabricatorChangeParserTestCase' => 'applications/repository/worker/__tests__/PhabricatorChangeParserTestCase.php',
'PhabricatorChangesetCachePurger' => 'applications/cache/purger/PhabricatorChangesetCachePurger.php',
'PhabricatorChangesetResponse' => 'infrastructure/diff/PhabricatorChangesetResponse.php',
'PhabricatorChatLogApplication' => 'applications/chatlog/application/PhabricatorChatLogApplication.php',
'PhabricatorChatLogChannel' => 'applications/chatlog/storage/PhabricatorChatLogChannel.php',
@ -2930,6 +2932,7 @@ phutil_register_library_map(array(
'PhabricatorGarbageCollectorManagementCollectWorkflow' => 'infrastructure/daemon/garbagecollector/management/PhabricatorGarbageCollectorManagementCollectWorkflow.php',
'PhabricatorGarbageCollectorManagementSetPolicyWorkflow' => 'infrastructure/daemon/garbagecollector/management/PhabricatorGarbageCollectorManagementSetPolicyWorkflow.php',
'PhabricatorGarbageCollectorManagementWorkflow' => 'infrastructure/daemon/garbagecollector/management/PhabricatorGarbageCollectorManagementWorkflow.php',
'PhabricatorGeneralCachePurger' => 'applications/cache/purger/PhabricatorGeneralCachePurger.php',
'PhabricatorGestureUIExample' => 'applications/uiexample/examples/PhabricatorGestureUIExample.php',
'PhabricatorGitGraphStream' => 'applications/repository/daemon/PhabricatorGitGraphStream.php',
'PhabricatorGitHubAuthProvider' => 'applications/auth/provider/PhabricatorGitHubAuthProvider.php',
@ -3748,6 +3751,7 @@ phutil_register_library_map(array(
'PhabricatorRegistrationProfile' => 'applications/people/storage/PhabricatorRegistrationProfile.php',
'PhabricatorReleephApplication' => 'applications/releeph/application/PhabricatorReleephApplication.php',
'PhabricatorReleephApplicationConfigOptions' => 'applications/releeph/config/PhabricatorReleephApplicationConfigOptions.php',
'PhabricatorRemarkupCachePurger' => 'applications/cache/purger/PhabricatorRemarkupCachePurger.php',
'PhabricatorRemarkupControl' => 'view/form/control/PhabricatorRemarkupControl.php',
'PhabricatorRemarkupCowsayBlockInterpreter' => 'infrastructure/markup/interpreter/PhabricatorRemarkupCowsayBlockInterpreter.php',
'PhabricatorRemarkupCustomBlockRule' => 'infrastructure/markup/rule/PhabricatorRemarkupCustomBlockRule.php',
@ -4202,6 +4206,7 @@ phutil_register_library_map(array(
'PhabricatorUserBadgesCacheType' => 'applications/people/cache/PhabricatorUserBadgesCacheType.php',
'PhabricatorUserBlurbField' => 'applications/people/customfield/PhabricatorUserBlurbField.php',
'PhabricatorUserCache' => 'applications/people/storage/PhabricatorUserCache.php',
'PhabricatorUserCachePurger' => 'applications/cache/purger/PhabricatorUserCachePurger.php',
'PhabricatorUserCacheType' => 'applications/people/cache/PhabricatorUserCacheType.php',
'PhabricatorUserCardView' => 'applications/people/view/PhabricatorUserCardView.php',
'PhabricatorUserConfigOptions' => 'applications/people/config/PhabricatorUserConfigOptions.php',
@ -7366,6 +7371,7 @@ phutil_register_library_map(array(
'PhabricatorCacheManagementPurgeWorkflow' => 'PhabricatorCacheManagementWorkflow',
'PhabricatorCacheManagementWorkflow' => 'PhabricatorManagementWorkflow',
'PhabricatorCacheMarkupGarbageCollector' => 'PhabricatorGarbageCollector',
'PhabricatorCachePurger' => 'Phobject',
'PhabricatorCacheSchemaSpec' => 'PhabricatorConfigSchemaSpec',
'PhabricatorCacheSetupCheck' => 'PhabricatorSetupCheck',
'PhabricatorCacheSpec' => 'Phobject',
@ -7551,6 +7557,7 @@ phutil_register_library_map(array(
'PhabricatorCelerityApplication' => 'PhabricatorApplication',
'PhabricatorCelerityTestCase' => 'PhabricatorTestCase',
'PhabricatorChangeParserTestCase' => 'PhabricatorWorkingCopyTestCase',
'PhabricatorChangesetCachePurger' => 'PhabricatorCachePurger',
'PhabricatorChangesetResponse' => 'AphrontProxyResponse',
'PhabricatorChatLogApplication' => 'PhabricatorApplication',
'PhabricatorChatLogChannel' => array(
@ -8253,6 +8260,7 @@ phutil_register_library_map(array(
'PhabricatorGarbageCollectorManagementCollectWorkflow' => 'PhabricatorGarbageCollectorManagementWorkflow',
'PhabricatorGarbageCollectorManagementSetPolicyWorkflow' => 'PhabricatorGarbageCollectorManagementWorkflow',
'PhabricatorGarbageCollectorManagementWorkflow' => 'PhabricatorManagementWorkflow',
'PhabricatorGeneralCachePurger' => 'PhabricatorCachePurger',
'PhabricatorGestureUIExample' => 'PhabricatorUIExample',
'PhabricatorGitGraphStream' => 'PhabricatorRepositoryGraphStream',
'PhabricatorGitHubAuthProvider' => 'PhabricatorOAuth2AuthProvider',
@ -9204,6 +9212,7 @@ phutil_register_library_map(array(
'PhabricatorRegistrationProfile' => 'Phobject',
'PhabricatorReleephApplication' => 'PhabricatorApplication',
'PhabricatorReleephApplicationConfigOptions' => 'PhabricatorApplicationConfigOptions',
'PhabricatorRemarkupCachePurger' => 'PhabricatorCachePurger',
'PhabricatorRemarkupControl' => 'AphrontFormTextAreaControl',
'PhabricatorRemarkupCowsayBlockInterpreter' => 'PhutilRemarkupBlockInterpreter',
'PhabricatorRemarkupCustomBlockRule' => 'PhutilRemarkupBlockRule',
@ -9756,6 +9765,7 @@ phutil_register_library_map(array(
'PhabricatorUserBadgesCacheType' => 'PhabricatorUserCacheType',
'PhabricatorUserBlurbField' => 'PhabricatorUserCustomField',
'PhabricatorUserCache' => 'PhabricatorUserDAO',
'PhabricatorUserCachePurger' => 'PhabricatorCachePurger',
'PhabricatorUserCacheType' => 'Phobject',
'PhabricatorUserCardView' => 'AphrontTagView',
'PhabricatorUserConfigOptions' => 'PhabricatorApplicationConfigOptions',

View file

@ -6,119 +6,75 @@ final class PhabricatorCacheManagementPurgeWorkflow
protected function didConstruct() {
$this
->setName('purge')
->setSynopsis(pht('Drop data from caches. APC-based caches can be '.
'purged from the web interface.'))
->setSynopsis(pht('Drop data from readthrough caches.'))
->setArguments(
array(
array(
'name' => 'purge-all',
'help' => pht('Purge all caches.'),
'name' => 'all',
'help' => pht('Purge all caches.'),
),
array(
'name' => 'purge-remarkup',
'help' => pht('Purge the remarkup cache.'),
),
array(
'name' => 'purge-changeset',
'help' => pht('Purge the Differential changeset cache.'),
),
array(
'name' => 'purge-general',
'help' => pht('Purge the general cache.'),
),
array(
'name' => 'purge-user',
'help' => pht('Purge the user cache.'),
'name' => 'caches',
'param' => 'keys',
'help' => pht('Purge a specific set of caches.'),
),
));
}
public function execute(PhutilArgumentParser $args) {
$console = PhutilConsole::getConsole();
$all_purgers = PhabricatorCachePurger::getAllPurgers();
$purge_all = $args->getArg('purge-all');
$purge = array(
'remarkup' => $purge_all || $args->getArg('purge-remarkup'),
'changeset' => $purge_all || $args->getArg('purge-changeset'),
'general' => $purge_all || $args->getArg('purge-general'),
'user' => $purge_all || $args->getArg('purge-user'),
);
if (!array_filter($purge)) {
$list = array();
foreach ($purge as $key => $ignored) {
$list[] = "'--purge-".$key."'";
}
$is_all = $args->getArg('all');
$key_list = $args->getArg('caches');
if ($is_all && strlen($key_list)) {
throw new PhutilArgumentUsageException(
pht(
"Specify which cache or caches to purge, or use '%s'. Available ".
"caches are: %s. Use '%s' for more information.",
'--purge-all',
implode(', ', $list),
'--help'));
'Specify either "--all" or "--caches", not both.'));
} else if (!$is_all && !strlen($key_list)) {
throw new PhutilArgumentUsageException(
pht(
'Select caches to purge with "--all" or "--caches". Available '.
'caches are: %s.',
implode(', ', array_keys($all_purgers))));
}
if ($purge['remarkup']) {
$console->writeOut(pht('Purging remarkup cache...'));
$this->purgeRemarkupCache();
$console->writeOut("%s\n", pht('Done.'));
if ($is_all) {
$purgers = $all_purgers;
} else {
$key_list = preg_split('/[\s,]+/', $key_list);
$purgers = array();
foreach ($key_list as $key) {
if (isset($all_purgers[$key])) {
$purgers[$key] = $all_purgers[$key];
} else {
throw new PhutilArgumentUsageException(
pht(
'Cache purger "%s" is not recognized. Available caches '.
'are: %s.',
$key,
implode(', ', array_keys($all_purgers))));
}
}
if (!$purgers) {
throw new PhutilArgumentUsageException(
pht(
'When using "--caches", you must select at least one valid '.
'cache to purge.'));
}
}
if ($purge['changeset']) {
$console->writeOut(pht('Purging changeset cache...'));
$this->purgeChangesetCache();
$console->writeOut("%s\n", pht('Done.'));
foreach ($purgers as $key => $purger) {
echo tsprintf(
"%s\n",
pht(
'Purging "%s" cache...',
$key));
$purger->purgeCache();
}
if ($purge['general']) {
$console->writeOut(pht('Purging general cache...'));
$this->purgeGeneralCache();
$console->writeOut("%s\n", pht('Done.'));
}
if ($purge['user']) {
$console->writeOut(pht('Purging user cache...'));
$this->purgeUserCache();
$console->writeOut("%s\n", pht('Done.'));
}
}
private function purgeRemarkupCache() {
$conn_w = id(new PhabricatorMarkupCache())->establishConnection('w');
queryfx(
$conn_w,
'TRUNCATE TABLE %T',
id(new PhabricatorMarkupCache())->getTableName());
}
private function purgeChangesetCache() {
$conn_w = id(new DifferentialChangeset())->establishConnection('w');
queryfx(
$conn_w,
'TRUNCATE TABLE %T',
DifferentialChangeset::TABLE_CACHE);
}
private function purgeGeneralCache() {
$conn_w = id(new PhabricatorMarkupCache())->establishConnection('w');
queryfx(
$conn_w,
'TRUNCATE TABLE %T',
'cache_general');
}
private function purgeUserCache() {
$table = new PhabricatorUserCache();
$conn_w = $table->establishConnection('w');
queryfx(
$conn_w,
'TRUNCATE TABLE %T',
$table->getTableName());
return 0;
}
}

View file

@ -0,0 +1,30 @@
<?php
abstract class PhabricatorCachePurger
extends Phobject {
private $viewer;
abstract public function purgeCache();
final public function setViewer(PhabricatorUser $viewer) {
$this->viewer = $viewer;
return $this;
}
final public function getViewer() {
return $this->viewer;
}
final public function getPurgerKey() {
return $this->getPhobjectClassConstant('PURGERKEY');
}
final public static function getAllPurgers() {
return id(new PhutilClassMapQuery())
->setAncestorClass(__CLASS__)
->setUniqueMethod('getPurgerKey')
->execute();
}
}

View file

@ -0,0 +1,18 @@
<?php
final class PhabricatorChangesetCachePurger
extends PhabricatorCachePurger {
const PURGERKEY = 'changeset';
public function purgeCache() {
$table = new DifferentialChangeset();
$conn = $table->establishConnection('w');
queryfx(
$conn,
'TRUNCATE TABLE %T',
DifferentialChangeset::TABLE_CACHE);
}
}

View file

@ -0,0 +1,18 @@
<?php
final class PhabricatorGeneralCachePurger
extends PhabricatorCachePurger {
const PURGERKEY = 'general';
public function purgeCache() {
$table = new PhabricatorMarkupCache();
$conn = $table->establishConnection('w');
queryfx(
$conn,
'TRUNCATE TABLE %T',
'cache_general');
}
}

View file

@ -0,0 +1,18 @@
<?php
final class PhabricatorRemarkupCachePurger
extends PhabricatorCachePurger {
const PURGERKEY = 'remarkup';
public function purgeCache() {
$table = new PhabricatorMarkupCache();
$conn = $table->establishConnection('w');
queryfx(
$conn,
'TRUNCATE TABLE %T',
$table->getTableName());
}
}

View file

@ -0,0 +1,18 @@
<?php
final class PhabricatorUserCachePurger
extends PhabricatorCachePurger {
const PURGERKEY = 'user';
public function purgeCache() {
$table = new PhabricatorUserCache();
$conn = $table->establishConnection('w');
queryfx(
$conn,
'TRUNCATE TABLE %T',
$table->getTableName());
}
}