Add REST endpoint to flush a cache

Change-Id: Ic5d1ab37f5808d760e0d86b5956cce9557d0292b
Signed-off-by: Edwin Kempin <edwin.kempin@sap.com>
This commit is contained in:
Edwin Kempin
2014-05-23 10:52:20 +02:00
parent 4ad8307a10
commit 37cc41a930
6 changed files with 229 additions and 14 deletions

View File

@@ -20,8 +20,9 @@ import com.google.common.cache.Cache;
import com.google.gerrit.common.data.GlobalCapability;
import com.google.gerrit.extensions.annotations.RequiresCapability;
import com.google.gerrit.extensions.registration.DynamicMap;
import com.google.gerrit.server.IdentifiedUser;
import com.google.gerrit.sshd.BaseCommand;
import com.google.gerrit.extensions.restapi.RestApiException;
import com.google.gerrit.server.config.CacheResource;
import com.google.gerrit.server.config.FlushCache;
import com.google.gerrit.sshd.CommandMetaData;
import com.google.inject.Inject;
@@ -48,18 +49,10 @@ final class FlushCaches extends CacheCommand {
private boolean list;
@Inject
IdentifiedUser currentUser;
private FlushCache flushCache;
@Override
protected void run() throws Failure {
if (caches.contains(WEB_SESSIONS)
&& !currentUser.getCapabilities().canAdministrateServer()) {
String msg = String.format(
"fatal: only site administrators can flush %s",
WEB_SESSIONS);
throw new UnloggedFailure(BaseCommand.STATUS_NOT_ADMIN, msg);
}
if (list) {
if (all || caches.size() > 0) {
throw error("error: cannot use --list with --all or --cache");
@@ -104,9 +97,12 @@ final class FlushCaches extends CacheCommand {
String n = cacheNameOf(e.getPluginName(), e.getExportName());
if (flush(n)) {
try {
e.getProvider().get().invalidateAll();
} catch (Throwable err) {
stderr.println("error: cannot flush cache \"" + n + "\": " + err);
flushCache.apply(
new CacheResource(e.getPluginName(), e.getExportName(),
e.getProvider()), null);
} catch (RestApiException err) {
stderr.println("error: cannot flush cache \"" + n + "\": "
+ err.getMessage());
}
}
}