|
| 1 | +/** |
| 2 | + * ContainerProxy |
| 3 | + * |
| 4 | + * Copyright (C) 2016-2021 Open Analytics |
| 5 | + * |
| 6 | + * =========================================================================== |
| 7 | + * |
| 8 | + * This program is free software: you can redistribute it and/or modify |
| 9 | + * it under the terms of the Apache License as published by |
| 10 | + * The Apache Software Foundation, either version 2 of the License, or |
| 11 | + * (at your option) any later version. |
| 12 | + * |
| 13 | + * This program is distributed in the hope that it will be useful, |
| 14 | + * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 15 | + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 16 | + * Apache License for more details. |
| 17 | + * |
| 18 | + * You should have received a copy of the Apache License |
| 19 | + * along with this program. If not, see <http://www.apache.org/licenses/> |
| 20 | + */ |
| 21 | +package eu.openanalytics.containerproxy.service; |
| 22 | + |
| 23 | +import eu.openanalytics.containerproxy.model.runtime.Proxy; |
| 24 | +import eu.openanalytics.containerproxy.model.runtime.ProxyStatus; |
| 25 | +import org.apache.commons.lang.time.DurationFormatUtils; |
| 26 | +import org.apache.logging.log4j.LogManager; |
| 27 | +import org.apache.logging.log4j.Logger; |
| 28 | +import org.springframework.core.env.Environment; |
| 29 | +import org.springframework.stereotype.Service; |
| 30 | + |
| 31 | +import javax.annotation.PostConstruct; |
| 32 | +import javax.inject.Inject; |
| 33 | +import java.time.Instant; |
| 34 | +import java.time.temporal.ChronoUnit; |
| 35 | +import java.util.Timer; |
| 36 | +import java.util.TimerTask; |
| 37 | + |
| 38 | +/** |
| 39 | + * This service releases proxies which reached their max-lifetime. |
| 40 | + */ |
| 41 | +@Service |
| 42 | +public class ProxyMaxLifetimeService { |
| 43 | + |
| 44 | + private static final Integer CLEANUP_INTERVAL = 5 * 60 * 1000; |
| 45 | + private static final String PROP_DEFAULT_PROXY_MAX_LIFETIME = "proxy.default-proxy-max-lifetime"; |
| 46 | + |
| 47 | + private final Logger log = LogManager.getLogger(ProxyMaxLifetimeService.class); |
| 48 | + |
| 49 | + @Inject |
| 50 | + private ProxyService proxyService; |
| 51 | + |
| 52 | + @Inject |
| 53 | + private Environment environment; |
| 54 | + |
| 55 | + private Long defaultMaxLifetime; |
| 56 | + |
| 57 | + @PostConstruct |
| 58 | + public void init() { |
| 59 | + defaultMaxLifetime = environment.getProperty(PROP_DEFAULT_PROXY_MAX_LIFETIME, Long.class, -1L); |
| 60 | + |
| 61 | + new Timer().schedule(new TimerTask() { |
| 62 | + @Override |
| 63 | + public void run() { |
| 64 | + performCleanup(); |
| 65 | + } |
| 66 | + }, CLEANUP_INTERVAL, CLEANUP_INTERVAL); |
| 67 | + } |
| 68 | + |
| 69 | + private void performCleanup() { |
| 70 | + for (Proxy proxy : proxyService.getProxies(null, true)) { |
| 71 | + if (mustBeReleased(proxy)) { |
| 72 | + String uptime = DurationFormatUtils.formatDurationWords( |
| 73 | + System.currentTimeMillis() - proxy.getCreatedTimestamp(), |
| 74 | + true, false); |
| 75 | + log.info(String.format("Forcefully releasing proxy because it reached the max lifetime [user: %s] [spec: %s] [id: %s] [uptime: %s]", proxy.getUserId(), proxy.getSpec().getId(), proxy.getId(), uptime)); |
| 76 | + proxyService.stopProxy(proxy, true, true); |
| 77 | + } |
| 78 | + } |
| 79 | + |
| 80 | + } |
| 81 | + |
| 82 | + private Boolean mustBeReleased(Proxy proxy) { |
| 83 | + if (proxy.getStatus() != ProxyStatus.Up) { |
| 84 | + return false; |
| 85 | + } |
| 86 | + |
| 87 | + Long maxLifeTime = proxy.getSpec().getMaxLifeTime(); |
| 88 | + if (maxLifeTime == null) { |
| 89 | + maxLifeTime = defaultMaxLifetime; |
| 90 | + } |
| 91 | + |
| 92 | + Instant notBeforeTime = Instant.now().minus(maxLifeTime, ChronoUnit.MINUTES); |
| 93 | + |
| 94 | + return Instant.ofEpochMilli(proxy.getCreatedTimestamp()).isBefore(notBeforeTime); |
| 95 | + } |
| 96 | + |
| 97 | +} |
0 commit comments