All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.instaclustr.operations.OperationsExpirationService Maven / Gradle / Ivy

There is a newer version: 2.0.0
Show newest version
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);
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy