// Copyright 2018 Yahoo Holdings. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
package com.yahoo.vespa.config.server.maintenance;

import com.yahoo.vespa.config.server.ApplicationRepository;
import com.yahoo.vespa.curator.Curator;

import java.time.Duration;

/**
 * Removes inactive sessions
 * <p>
 * Note: Unit test is in ApplicationRepositoryTest
 *
 * @author hmusum
 */
public class SessionsMaintainer extends ConfigServerMaintainer {
    private final boolean hostedVespa;

    SessionsMaintainer(ApplicationRepository applicationRepository, Curator curator, Duration interval) {
        // Start this maintainer immediately. It frees disk space, so if disk goes full and config server
        // restarts this makes sure that cleanup will happen as early as possible
        super(applicationRepository, curator, Duration.ZERO, interval);
        this.hostedVespa = applicationRepository.configserverConfig().hostedVespa();
    }

    @Override
    protected void maintain() {
        applicationRepository.deleteExpiredLocalSessions();

        // Expired remote sessions are not expected to exist, they should have been deleted when
        // a deployment happened or when the application was deleted. We still see them from time to time,
        // probably due to some race or another bug
        if (hostedVespa) {
            Duration expiryTime = Duration.ofDays(30);
            applicationRepository.deleteExpiredRemoteSessions(expiryTime);
        }
    }
}

@@ -1122,172 +1122,82 @@
 are 
-not expected to exist, they should have been deleted when%0A        // a deployment happened or when the application was deleted. We still see them from time to time,
+sessions that belong to an application that have external deployments that
 %0A   
@@ -1208,48 +1208,28 @@
  // 
-probably due to some race or another bug
+are no longer active
 %0A   
@@ -1306,10 +1306,9 @@
 ays(
-30
+7
 );%0A 

Revert "Revert "Reduce the time we keep expired remotes sessions and fix comment""