diff --git a/bubble-server/src/main/java/bubble/resources/cloud/LogsResource.java b/bubble-server/src/main/java/bubble/resources/cloud/LogsResource.java index 2ea1623f..f4c3500c 100644 --- a/bubble-server/src/main/java/bubble/resources/cloud/LogsResource.java +++ b/bubble-server/src/main/java/bubble/resources/cloud/LogsResource.java @@ -10,9 +10,12 @@ import lombok.NonNull; import org.glassfish.jersey.server.ContainerRequest; import org.springframework.beans.factory.annotation.Autowired; +import javax.annotation.Nullable; import javax.ws.rs.*; import javax.ws.rs.core.Context; import javax.ws.rs.core.Response; +import java.util.Optional; +import java.util.concurrent.TimeUnit; import static bubble.ApiConstants.*; import static org.cobbzilla.util.http.HttpContentTypes.APPLICATION_JSON; @@ -37,13 +40,19 @@ public class LogsResource { } @POST @Path(EP_START) - @NonNull public Response startLogging(@NonNull @Context final ContainerRequest ctx) { return setLogFlag(true); } + @NonNull public Response startLogging(@NonNull @Context final ContainerRequest ctx, + @Nullable @QueryParam("ttlDays") final Byte ttlDays) { + return setLogFlag(true, Optional.ofNullable(ttlDays)); + } + @POST @Path(EP_STOP) - @NonNull public Response stopLogging(@NonNull @Context final ContainerRequest ctx) { return setLogFlag(false); } + @NonNull public Response stopLogging(@NonNull @Context final ContainerRequest ctx) { + return setLogFlag(false, Optional.empty()); + } - @NonNull private Response setLogFlag(final boolean b) { + @NonNull private Response setLogFlag(final boolean b, @NonNull final Optional ttlInDays) { if (!account.admin()) throw forbiddenEx(); // caller must be admin - selfNodeService.setLogFlag(b); + selfNodeService.setLogFlag(b, ttlInDays.map(days -> (int) TimeUnit.DAYS.toSeconds(days))); return ok(); } } diff --git a/bubble-server/src/main/java/bubble/service/boot/SelfNodeService.java b/bubble-server/src/main/java/bubble/service/boot/SelfNodeService.java index afa89650..1ec7adcb 100644 --- a/bubble-server/src/main/java/bubble/service/boot/SelfNodeService.java +++ b/bubble-server/src/main/java/bubble/service/boot/SelfNodeService.java @@ -7,6 +7,9 @@ package bubble.service.boot; import bubble.model.bill.BubblePlan; import bubble.model.cloud.BubbleNetwork; import bubble.model.cloud.BubbleNode; +import lombok.NonNull; + +import java.util.Optional; public interface SelfNodeService { @@ -25,5 +28,5 @@ public interface SelfNodeService { BubblePlan getThisPlan(); Boolean getLogFlag(); - void setLogFlag(final boolean logFlag); + void setLogFlag(final boolean logFlag, @NonNull final Optional ttlInSeconds); } diff --git a/bubble-server/src/main/java/bubble/service/boot/StandardSelfNodeService.java b/bubble-server/src/main/java/bubble/service/boot/StandardSelfNodeService.java index 68c1a692..5389f04b 100644 --- a/bubble-server/src/main/java/bubble/service/boot/StandardSelfNodeService.java +++ b/bubble-server/src/main/java/bubble/service/boot/StandardSelfNodeService.java @@ -41,6 +41,7 @@ import org.springframework.stereotype.Service; import java.io.File; import java.util.ArrayList; import java.util.List; +import java.util.Optional; import java.util.concurrent.atomic.AtomicBoolean; import java.util.concurrent.atomic.AtomicReference; @@ -72,8 +73,8 @@ public class StandardSelfNodeService implements SelfNodeService { public static final long MIN_SAGE_KEY_TTL = MINUTES.toMillis(5); private static final String REDIS_LOG_FLAG_KEY = "bubble_server_logs_enabled"; - private static final long TTL_LOG_FLAG_NODE = DAYS.toSeconds(7); - private static final long TTL_LOG_FLAG_SAGE = DAYS.toSeconds(30); + private static final int TTL_LOG_FLAG_NODE = (int) DAYS.toSeconds(7); + private static final int TTL_LOG_FLAG_SAGE = (int) DAYS.toSeconds(30); @Autowired private BubbleNodeDAO nodeDAO; @Autowired private BubbleNodeKeyDAO nodeKeyDAO; @@ -448,10 +449,10 @@ public class StandardSelfNodeService implements SelfNodeService { } @Override - public void setLogFlag(final boolean logFlag) { + public void setLogFlag(final boolean logFlag, @NonNull final Optional ttlInSeconds) { if (logFlag) { getNodeConfig().set_plaintext(REDIS_LOG_FLAG_KEY, "true", EX, - isSelfSage() ? TTL_LOG_FLAG_SAGE : TTL_LOG_FLAG_NODE); + ttlInSeconds.orElse(isSelfSage() ? TTL_LOG_FLAG_SAGE : TTL_LOG_FLAG_NODE)); } else { // just (try to) remove the flag getNodeConfig().del(REDIS_LOG_FLAG_KEY); diff --git a/bubble-server/src/main/java/bubble/service_dbfilter/DbFilterSelfNodeService.java b/bubble-server/src/main/java/bubble/service_dbfilter/DbFilterSelfNodeService.java index 22b5b25f..8fe6de10 100644 --- a/bubble-server/src/main/java/bubble/service_dbfilter/DbFilterSelfNodeService.java +++ b/bubble-server/src/main/java/bubble/service_dbfilter/DbFilterSelfNodeService.java @@ -8,8 +8,11 @@ import bubble.model.bill.BubblePlan; import bubble.model.cloud.BubbleNetwork; import bubble.model.cloud.BubbleNode; import bubble.service.boot.SelfNodeService; +import lombok.NonNull; import org.springframework.stereotype.Service; +import java.util.Optional; + import static org.cobbzilla.util.daemon.ZillaRuntime.notSupported; @Service @@ -30,6 +33,8 @@ public class DbFilterSelfNodeService implements SelfNodeService { @Override public BubblePlan getThisPlan() { return notSupported("getThisPlan"); } @Override public Boolean getLogFlag() { return notSupported("getLogFlag"); } - @Override public void setLogFlag(boolean logFlag) { notSupported("setLogFlag"); } + @Override public void setLogFlag(boolean logFlag, @NonNull final Optional ttlInSeconds) { + notSupported("setLogFlag"); + } }