com.instaclustr.operations.OperationsExpirationService Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of commons Show documentation
Show all versions of commons Show documentation
Common classes and utilities integrated with various projects
package com.instaclustr.operations;
import java.time.Instant;
import java.util.Map;
import java.util.UUID;
import java.util.concurrent.TimeUnit;
import com.google.common.util.concurrent.AbstractScheduledService;
import com.google.inject.Inject;
import com.google.inject.name.Named;
public class OperationsExpirationService extends AbstractScheduledService {
private final long expirationPeriodInSeconds;
private final Map operations;
@Inject
public OperationsExpirationService(final @OperationsMap Map operations,
final @Named("operationsExpirationPeriod") long expirationPeriodInSeconds) {
this.operations = operations;
this.expirationPeriodInSeconds = expirationPeriodInSeconds;
}
@Override
protected void runOneIteration() throws Exception {
final Instant expirationThreshold = Instant.now().minusSeconds(expirationPeriodInSeconds);
operations.values().removeIf(value -> value.state.isTerminalState() && value.completionTime != null && value.completionTime.isBefore(expirationThreshold));
}
@Override
protected Scheduler scheduler() {
return Scheduler.newFixedRateSchedule(expirationPeriodInSeconds, expirationPeriodInSeconds, TimeUnit.SECONDS);
}
}