Skip to content

Commit

Permalink
check for initialized when run and redo default interval value
Browse files Browse the repository at this point in the history
  • Loading branch information
CarstenHollmann committed Sep 6, 2022
1 parent e9b9f59 commit 9d7d351
Showing 1 changed file with 30 additions and 29 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -47,33 +47,12 @@ public abstract class AbstractSchedulingContentCacheController implements Conten
private static final Logger LOGGER = LoggerFactory.getLogger(AbstractSchedulingContentCacheController.class);

private boolean initialized;
private long updateInterval = 120;
private long updateInterval;
private final Timer timer = new Timer("52n-iceland-capabilities-cache-controller", true);
private TimerTask current;
private Optional<StaticCapabilitiesProvider> staticCapabilitiesProvider;
private Optional<StaticCapabilitiesProvider> staticCapabilitiesProvider = Optional.empty();


/**
* Starts a new timer task
*/
private void schedule() {
/*
* Timers can not be rescheduled. To make the interval changeable
* reschedule a new timer.
*/
current = new UpdateTimerTask();
long delay = getUpdateInterval();
if (!isInitialized()) {
delay = 1;
setInitialized(true);
}
if (delay > 0) {
LOGGER.info("Next CapabilitiesCacheUpdate in {}m: {}", delay / 60000,
new DateTime(System.currentTimeMillis() + delay));
timer.schedule(current, delay);
}
}

@Setting(ScheduledContentCacheControllerSettings.CAPABILITIES_CACHE_UPDATE)
public void setCronExpression(String cronExpression) {
Validation.notNullOrEmpty("Cron expression for cache update", cronExpression);
Expand All @@ -84,7 +63,7 @@ public void setCronExpression(String cronExpression) {
Date next = cronExp.getNextValidTimeAfter(first);
setUpdateInterval(DateTimeHelper.getMinutesSince(new DateTime(first), new DateTime(next)));
} catch (ParseException e) {

throw new ConfigurationError(String.format("The defined cron expression '%s' is invalid!", cronExpression),
e);
}
Expand All @@ -107,6 +86,26 @@ private long getUpdateInterval() {
return this.updateInterval * 60000;
}

/**
* Starts a new timer task
*/
private void schedule() {
/*
* Timers can not be rescheduled. To make the interval changeable
* reschedule a new timer.
*/
current = new UpdateTimerTask();
long delay = getUpdateInterval();
if (!isInitialized()) {
delay = 1;
}
if (delay > 0) {
LOGGER.info("Next CapabilitiesCacheUpdate in {}m: {}", delay / 60000,
new DateTime(System.currentTimeMillis() + delay));
timer.schedule(current, delay);
}
}

/**
* Stops the current task, if available and starts a new {@link TimerTask}.
*
Expand Down Expand Up @@ -153,11 +152,13 @@ private class UpdateTimerTask extends TimerTask {
@Override
public void run() {
try {
update();
LOGGER.info("Timertask: capabilities cache update successful!");
schedule();
if (staticCapabilitiesProvider.isPresent()) {
staticCapabilitiesProvider.get().create();
if (isInitialized()) {
update();
LOGGER.info("Timertask: capabilities cache update successful!");
schedule();
if (staticCapabilitiesProvider != null && staticCapabilitiesProvider.isPresent()) {
staticCapabilitiesProvider.get().create();
}
}
} catch (OwsExceptionReport e) {
LOGGER.error("Fatal error: Timertask couldn't update capabilities cache! " +
Expand Down

0 comments on commit 9d7d351

Please sign in to comment.