From bd3f441098a02f4e96947574af6c458d45b0d96d Mon Sep 17 00:00:00 2001 From: epriestley Date: Thu, 22 Jun 2017 10:41:10 -0700 Subject: [PATCH] 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 --- src/__phutil_library_map__.php | 10 ++ ...habricatorCacheManagementPurgeWorkflow.php | 142 ++++++------------ .../cache/purger/PhabricatorCachePurger.php | 30 ++++ .../PhabricatorChangesetCachePurger.php | 18 +++ .../purger/PhabricatorGeneralCachePurger.php | 18 +++ .../purger/PhabricatorRemarkupCachePurger.php | 18 +++ .../purger/PhabricatorUserCachePurger.php | 18 +++ 7 files changed, 161 insertions(+), 93 deletions(-) create mode 100644 src/applications/cache/purger/PhabricatorCachePurger.php create mode 100644 src/applications/cache/purger/PhabricatorChangesetCachePurger.php create mode 100644 src/applications/cache/purger/PhabricatorGeneralCachePurger.php create mode 100644 src/applications/cache/purger/PhabricatorRemarkupCachePurger.php create mode 100644 src/applications/cache/purger/PhabricatorUserCachePurger.php diff --git a/src/__phutil_library_map__.php b/src/__phutil_library_map__.php index f1fd9648cd..d7b197028f 100644 --- a/src/__phutil_library_map__.php +++ b/src/__phutil_library_map__.php @@ -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', diff --git a/src/applications/cache/management/PhabricatorCacheManagementPurgeWorkflow.php b/src/applications/cache/management/PhabricatorCacheManagementPurgeWorkflow.php index 91ab0cf83d..d40c0f35e6 100644 --- a/src/applications/cache/management/PhabricatorCacheManagementPurgeWorkflow.php +++ b/src/applications/cache/management/PhabricatorCacheManagementPurgeWorkflow.php @@ -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; } } diff --git a/src/applications/cache/purger/PhabricatorCachePurger.php b/src/applications/cache/purger/PhabricatorCachePurger.php new file mode 100644 index 0000000000..0115b45d7e --- /dev/null +++ b/src/applications/cache/purger/PhabricatorCachePurger.php @@ -0,0 +1,30 @@ +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(); + } + +} diff --git a/src/applications/cache/purger/PhabricatorChangesetCachePurger.php b/src/applications/cache/purger/PhabricatorChangesetCachePurger.php new file mode 100644 index 0000000000..fd6ff9940a --- /dev/null +++ b/src/applications/cache/purger/PhabricatorChangesetCachePurger.php @@ -0,0 +1,18 @@ +establishConnection('w'); + + queryfx( + $conn, + 'TRUNCATE TABLE %T', + DifferentialChangeset::TABLE_CACHE); + } + +} diff --git a/src/applications/cache/purger/PhabricatorGeneralCachePurger.php b/src/applications/cache/purger/PhabricatorGeneralCachePurger.php new file mode 100644 index 0000000000..f2dea1e1e5 --- /dev/null +++ b/src/applications/cache/purger/PhabricatorGeneralCachePurger.php @@ -0,0 +1,18 @@ +establishConnection('w'); + + queryfx( + $conn, + 'TRUNCATE TABLE %T', + 'cache_general'); + } + +} diff --git a/src/applications/cache/purger/PhabricatorRemarkupCachePurger.php b/src/applications/cache/purger/PhabricatorRemarkupCachePurger.php new file mode 100644 index 0000000000..2c416fff36 --- /dev/null +++ b/src/applications/cache/purger/PhabricatorRemarkupCachePurger.php @@ -0,0 +1,18 @@ +establishConnection('w'); + + queryfx( + $conn, + 'TRUNCATE TABLE %T', + $table->getTableName()); + } + +} diff --git a/src/applications/cache/purger/PhabricatorUserCachePurger.php b/src/applications/cache/purger/PhabricatorUserCachePurger.php new file mode 100644 index 0000000000..64f631c96b --- /dev/null +++ b/src/applications/cache/purger/PhabricatorUserCachePurger.php @@ -0,0 +1,18 @@ +establishConnection('w'); + + queryfx( + $conn, + 'TRUNCATE TABLE %T', + $table->getTableName()); + } + +}