Skip to content

Commit

Permalink
Revert "fix(front50): Update Front50 cache periodically and serve liv…
Browse files Browse the repository at this point in the history
…e calls from cache. (spinnaker#351)" (spinnaker#378)

* Revert "fix(front50): Update Front50 cache periodically and serve live calls from cache. (spinnaker#351)"

This reverts commit 4ea2d71.

* fix(front50): Refresh Front50 cache on schedule.
  • Loading branch information
dibyom committed Apr 26, 2019
1 parent 5b2f5a5 commit 16d8b4c
Showing 1 changed file with 43 additions and 23 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,8 @@
@Slf4j
public class Front50Service implements HealthTrackable, InitializingBean {

private static final String GROUP_KEY = "front50Service";

private final Front50Api front50Api;

@Autowired
Expand All @@ -48,42 +50,60 @@ public Front50Service(Front50Api front50Api) {

@Override
public void afterPropertiesSet() throws Exception {
try {
// Initialize caches (also indicates service is healthy)
refreshApplications();
refreshServiceAccounts();
} catch (Exception e) {
log.warn("Cache prime failed: ", e);
}
refreshCache();
}

public List<Application> getAllApplicationPermissions() {
return applicationCache.get();
return new SimpleJava8HystrixCommand<>(
GROUP_KEY,
"getAllApplicationPermissions",
() -> {
applicationCache.set(front50Api.getAllApplicationPermissions());
healthTracker.success();
return applicationCache.get();
},
(Throwable cause) -> {
logFallback("application", cause);
List<Application> applications = applicationCache.get();
if (applications == null) {
throw new HystrixBadRequestException("Front50 is unavailable", cause);
}
return applications;
}).execute();
}

public List<ServiceAccount> getAllServiceAccounts() {
return serviceAccountCache.get();
return new SimpleJava8HystrixCommand<>(
GROUP_KEY,
"getAccounts",
() -> {
serviceAccountCache.set(front50Api.getAllServiceAccounts());
healthTracker.success();
return serviceAccountCache.get();
},
(Throwable cause) -> {
logFallback("service account", cause);
List<ServiceAccount> serviceAccounts = serviceAccountCache.get();
if (serviceAccounts == null) {
throw new HystrixBadRequestException("Front50 is unavailable", cause);
}
return serviceAccounts;
}).execute();
}

private static void logFallback(String resource, Throwable cause) {
String message = cause != null ? "Cause: " + cause.getMessage() : "";
log.info("Falling back to {} cache. {}", resource, message);
}


@Scheduled(fixedDelayString = "${fiat.front50RefreshMs:30000}")
public void refreshApplications() {
applicationCache.set(
front50Api.getAllApplicationPermissions()
);
healthTracker.success();
}

@Scheduled(fixedDelayString = "${fiat.front50RefreshMs:30000}")
public void refreshServiceAccounts() {
serviceAccountCache.set(
front50Api.getAllServiceAccounts()
);
healthTracker.success();
private void refreshCache() {
try {
// Initialize caches (also indicates service is healthy)
getAllApplicationPermissions();
getAllServiceAccounts();
} catch (Exception e) {
log.warn("Cache prime failed: ", e);
}
}
}

0 comments on commit 16d8b4c

Please sign in to comment.