From e4470c0db1ccc5110e35d589b713efe372720f3d Mon Sep 17 00:00:00 2001 From: "Lincoln Baxter, III" Date: Mon, 23 Jun 2014 12:47:22 -0400 Subject: [PATCH] Fix cache invalidation --- .../website/rest/HooksIntegrationService.java | 41 ++++++++++++------- .../forge/website/service/CacheEntry.java | 8 ++++ .../forge/website/service/Downloader.java | 7 +++- 3 files changed, 40 insertions(+), 16 deletions(-) diff --git a/src/main/java/org/jboss/forge/website/rest/HooksIntegrationService.java b/src/main/java/org/jboss/forge/website/rest/HooksIntegrationService.java index 85cd98c..4a6de88 100644 --- a/src/main/java/org/jboss/forge/website/rest/HooksIntegrationService.java +++ b/src/main/java/org/jboss/forge/website/rest/HooksIntegrationService.java @@ -2,6 +2,8 @@ import java.util.Map; +import javax.ejb.Asynchronous; +import javax.ejb.Stateless; import javax.inject.Inject; import javax.ws.rs.POST; import javax.ws.rs.Path; @@ -23,6 +25,7 @@ /** * @author Lincoln Baxter, III */ +@Stateless @Path("/v1/webhooks") @Produces({ "application/xml", "application/json" }) public class HooksIntegrationService @@ -51,27 +54,35 @@ public Response githubUpdateRepository(String payload) throws Exception } else { - try (CloseableHttpClient client = HttpClientBuilder.create().build()) + try { - Address address = AddressBuilder.begin().scheme("http").domain(SiteConstants.REDOCULOUS_DOMAIN) - .path("/api/v1/manage").query("repo", repo).build(); - - HttpResponse response = client.execute(new HttpPut(address.toString())); - - if (response.getStatusLine().getStatusCode() != 200) - throw new IllegalStateException("failed! (server returned status code: " - + response.getStatusLine().getStatusCode() + ")"); - } - catch (Exception e) - { - return Response.status(Status.INTERNAL_SERVER_ERROR) - .entity("Failed to purge Redoculous cache for repo: " + repo).build(); + invalidateRedoculous(repo); } finally { - downloader.invalidateCachesByAddress(".*repo=" + repo + ".*"); + downloader.invalidateCachesByAddress(repo); } } return Response.status(Status.OK).build(); } + + @Asynchronous + private void invalidateRedoculous(String repo) + { + try (CloseableHttpClient client = HttpClientBuilder.create().build()) + { + Address address = AddressBuilder.begin().scheme("http").domain(SiteConstants.REDOCULOUS_DOMAIN) + .path("/api/v1/manage").query("repo", repo).build(); + + HttpResponse response = client.execute(new HttpPut(address.toString())); + + if (response.getStatusLine().getStatusCode() != 200) + throw new IllegalStateException("failed! (server returned status code: " + + response.getStatusLine().getStatusCode() + ")"); + } + catch (Exception e) + { + throw new IllegalStateException("Failed to purge Redoculous cache for repo: " + repo, e); + } + } } diff --git a/src/main/java/org/jboss/forge/website/service/CacheEntry.java b/src/main/java/org/jboss/forge/website/service/CacheEntry.java index 577f6df..928a9ac 100644 --- a/src/main/java/org/jboss/forge/website/service/CacheEntry.java +++ b/src/main/java/org/jboss/forge/website/service/CacheEntry.java @@ -44,4 +44,12 @@ public String getAddress() { return address; } + + @Override + public String toString() + { + return "CacheEntry [address=" + address + ", time=" + time + "]"; + } + + } diff --git a/src/main/java/org/jboss/forge/website/service/Downloader.java b/src/main/java/org/jboss/forge/website/service/Downloader.java index 984bf33..c6bd06a 100644 --- a/src/main/java/org/jboss/forge/website/service/Downloader.java +++ b/src/main/java/org/jboss/forge/website/service/Downloader.java @@ -10,6 +10,7 @@ import java.util.Map; import java.util.concurrent.ConcurrentHashMap; import java.util.logging.Logger; +import java.util.regex.Pattern; import javax.inject.Singleton; @@ -18,6 +19,7 @@ import org.apache.http.impl.client.CloseableHttpClient; import org.apache.http.impl.client.HttpClientBuilder; import org.ocpsoft.common.util.Streams; +import org.ocpsoft.urlbuilder.util.Decoder; /** * @author Lincoln Baxter, III @@ -83,10 +85,13 @@ public void invalidateCaches() public void invalidateCachesByAddress(String pattern) { + pattern = ".*" + Pattern.quote(pattern) + ".*"; log.info("Invalidating caches for pattern [" + pattern + "]"); for (CacheEntry entry : cache.values()) { - if (entry.getAddress().matches(pattern)) + if (entry.getAddress().matches(pattern) + || Decoder.query(entry.getAddress()).matches(pattern) + || Decoder.path(entry.getAddress()).matches(pattern)) { log.info("Invalidating cache entry [" + entry.getAddress() + "] for pattern [" + pattern + "]"); entry.invalidate();