Skip to content

Commit

Permalink
Tidy up logs a bit
Browse files Browse the repository at this point in the history
  • Loading branch information
Tristan971 committed Jan 13, 2025
1 parent 7f48aa2 commit 0e2b3d5
Show file tree
Hide file tree
Showing 18 changed files with 120 additions and 74 deletions.
4 changes: 3 additions & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,9 @@
<artifactId>maven-surefire-plugin</artifactId>
<version>${maven-surefire-plugin.version}</version>
<configuration>
<argLine>--enable-preview -Djava.io.tmpdir=${surefireTmpDir} @{jacoco.surefireArgLine} -javaagent:${org.mockito:mockito-core:jar} -Dspring.profiles.active=test</argLine>
<argLine>--enable-preview -Djava.io.tmpdir=${surefireTmpDir} @{jacoco.surefireArgLine} -javaagent:${org.mockito:mockito-core:jar}
-Dspring.profiles.active=test
</argLine>
<redirectTestOutputToFile>true</redirectTestOutputToFile>
</configuration>
</plugin>
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/org/mangadex/mcw/MCW.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.context.properties.ConfigurationPropertiesScan;

import org.mangadex.mcw.Bootstrap.BuildInfo;
import org.mangadex.mcw.lifecycle.MCRLifecycle.BuildInfo;

@SpringBootApplication
@ConfigurationPropertiesScan
Expand Down
4 changes: 2 additions & 2 deletions src/main/java/org/mangadex/mcw/dns/DnsResolver.java
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ private DnsResolution handleA(LookupResult result) {
if (r.getType() != Type.A) {
LOGGER.warn("Unexpected non-A record discarded: {}", r);
} else {
LOGGER.debug("+ A record {}", r);
LOGGER.trace("+ A record {}", r);
}
})
.filter(r -> r.getType() == Type.A)
Expand All @@ -88,7 +88,7 @@ private DnsResolution handleSRV(LookupResult result) {
if (r.getType() != Type.SRV) {
LOGGER.debug("Unexpected non-SRV response in SRV query: {}", r);
} else {
LOGGER.debug("+ SRV record {}", r);
LOGGER.trace("+ SRV record {}", r);
}
})
.filter(r -> r.getType() == Type.SRV)
Expand Down
12 changes: 0 additions & 12 deletions src/main/java/org/mangadex/mcw/lifecycle/LifecycleProperties.java

This file was deleted.

22 changes: 12 additions & 10 deletions src/main/java/org/mangadex/mcw/lifecycle/MCRConfigCollector.java
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
package org.mangadex.mcw.lifecycle;

import static java.util.Arrays.stream;

import java.util.ArrayList;
import java.util.List;

Expand Down Expand Up @@ -30,24 +28,25 @@ public List<MCRConfig> findAll(ApplicationArguments args) {

var argWatch = parseFromArguments(args);
if (argWatch != null) {
LOGGER.info("Added application arguments watch: {}", argWatch);
LOGGER.info("Found arguments-based watch: {}", argWatch);
watches.add(argWatch);
}

var configWatches = parseFromConfig();
LOGGER.debug("Added configuration watches: {}", watches);
watches.addAll(configWatches);
if (configWatches != null && !configWatches.isEmpty()) {
LOGGER.debug("Found configuration-based watches: {}", watches);
watches.addAll(configWatches);
}

return watches;
}

private MCRConfig parseFromArguments(ApplicationArguments args) {
LOGGER.debug("Parsing watch from arguments: {}", stream(args.getSourceArgs()).toList());

LOGGER.trace("Parsing watch from {} arguments", args.getOptionNames().size());
boolean hasSource = args.containsOption("source");
boolean hasOutput = args.containsOption("output");
if (!hasSource && !hasOutput) {
LOGGER.debug("No source or output specified in arguments");
LOGGER.trace("No source or output specified in arguments");
return null;
}

Expand All @@ -65,16 +64,19 @@ private MCRConfig parseFromArguments(ApplicationArguments args) {
throw new IllegalArgumentException("Expected exactly one 'output' option");
}

return MCRConfigParser.parseWatch(sourceOpt.getFirst(), outputOpt.getFirst());
var watch = MCRConfigParser.parseWatch(sourceOpt.getFirst(), outputOpt.getFirst());
LOGGER.trace("+ watch {}", watch);
return watch;
}

private List<MCRConfig> parseFromConfig() {
LOGGER.debug("Parsing configs from configuration: {}", MCRConfigProperties.configs());
LOGGER.trace("Parsing configs from configuration: {}", MCRConfigProperties.configs());

return MCRConfigProperties
.configs()
.stream()
.map(wce -> MCRConfigParser.parseWatch(wce.source(), wce.output()))
.peek(watch -> LOGGER.trace("+ watch {}", watch))
.toList();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import jakarta.validation.Valid;
import jakarta.validation.constraints.NotNull;
import jakarta.validation.constraints.Pattern;
import jakarta.validation.constraints.Positive;

import org.hibernate.validator.constraints.UniqueElements;
import org.springframework.boot.context.properties.ConfigurationProperties;
Expand All @@ -16,7 +17,11 @@ public record MCRConfigProperties(
@Validated
@NotNull
@UniqueElements
List<@Valid @NotNull MCRConfigProperty> configs
List<@Valid @NotNull MCRConfigProperty> configs,

@Validated
@NotNull
LifecycleProperties lifecycle

) {

Expand All @@ -26,4 +31,9 @@ public record MCRConfigProperty(
@NotNull @Pattern(regexp = "^[a-z]+://.+") String output
) { }

@Validated
public record LifecycleProperties(
@Validated @Positive int retryDelaySeconds
) { }

}
Original file line number Diff line number Diff line change
@@ -1,32 +1,51 @@
package org.mangadex.mcw;
package org.mangadex.mcw.lifecycle;

import static java.util.Objects.requireNonNull;

import java.io.IOException;
import java.util.concurrent.atomic.AtomicBoolean;

import org.springframework.boot.ApplicationArguments;
import org.springframework.boot.ApplicationRunner;
import org.springframework.boot.env.YamlPropertySourceLoader;
import org.springframework.context.SmartLifecycle;
import org.springframework.core.io.ClassPathResource;
import org.springframework.stereotype.Component;

import org.mangadex.mcw.lifecycle.MCRConfigCollector;
import org.mangadex.mcw.lifecycle.MCRWatchLifecycler;

@Component
public class Bootstrap implements ApplicationRunner {
public class MCRLifecycle implements SmartLifecycle {

private final ApplicationArguments applicationArguments;
private final MCRConfigCollector configCollector;
private final MCRWatchLifecycler watchLifecycler;
private final MCRWatchRegistry watchLifecycler;

private final AtomicBoolean running = new AtomicBoolean(false);

public Bootstrap(MCRConfigCollector configCollector, MCRWatchLifecycler watchLifecycler) {
public MCRLifecycle(
ApplicationArguments applicationArguments,
MCRConfigCollector configCollector,
MCRWatchRegistry watchLifecycler
) {
this.applicationArguments = applicationArguments;
this.configCollector = configCollector;
this.watchLifecycler = watchLifecycler;
}

@Override
public void run(ApplicationArguments args) throws Exception {
configCollector.findAll(args).forEach(watchLifecycler::register);
public void start() {
watchLifecycler.start();
configCollector.findAll(applicationArguments).forEach(watchLifecycler::register);
running.set(true);
}

@Override
public void stop() {
running.set(false);
watchLifecycler.stop();
}

@Override
public boolean isRunning() {
return running.get();
}

public record BuildInfo(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@
import org.springframework.boot.actuate.info.Info.Builder;
import org.springframework.boot.actuate.info.InfoContributor;
import org.springframework.context.ApplicationContext;
import org.springframework.context.SmartLifecycle;
import org.springframework.stereotype.Component;
import org.springframework.util.function.ThrowingConsumer;

Expand All @@ -24,30 +23,30 @@
import org.mangadex.mcw.source.file.FSWatcher;

@Component
public class MCRWatchLifecycler implements SmartLifecycle, InfoContributor {
public class MCRWatchRegistry implements InfoContributor {

private static final Logger LOGGER = LoggerFactory.getLogger(MCRWatchLifecycler.class);
private static final Logger LOGGER = LoggerFactory.getLogger(MCRWatchRegistry.class);

private final ApplicationContext context;
private final RenderService renderService;
private final LifecycleProperties lifecycleProperties;
private final MCRConfigProperties mcrConfigProperties;

private final AtomicBoolean running = new AtomicBoolean(false);
private final Map<MCRConfig, Registration> registrations = new ConcurrentHashMap<>();
private final Set<MCRConfig> registeredConfigs = unmodifiableSet(registrations.keySet());

public MCRWatchLifecycler(ApplicationContext context, RenderService renderService, LifecycleProperties lifecycleProperties) {
public MCRWatchRegistry(ApplicationContext context, RenderService renderService, MCRConfigProperties mcrConfigProperties) {
this.context = context;
this.renderService = renderService;
this.lifecycleProperties = lifecycleProperties;
this.mcrConfigProperties = mcrConfigProperties;
}

public void register(MCRConfig config) {
if (!running.get()) {
throw new IllegalStateException("Application is not running yet. Ignoring registration of " + config);
}

LOGGER.info("Registering config watch for {}", config);
LOGGER.debug("Registering config watch for {}", config);

var source = config.source();
var output = config.output();
Expand All @@ -64,7 +63,7 @@ public void register(MCRConfig config) {
ScheduledRenderTask renderTask = new ScheduledRenderTask(
renderService::render,
writeToOutput,
lifecycleProperties.retryDelaySeconds()
mcrConfigProperties.lifecycle().retryDelaySeconds()
);

switch (source) {
Expand All @@ -82,13 +81,11 @@ public void register(MCRConfig config) {
LOGGER.info("Started config watch for {}", config);
}

@Override
public void start() {
LOGGER.info("Starting config watches");
LOGGER.debug("Starting config watches");
running.set(true);
}

@Override
public void stop() {
LOGGER.info("Shutting down all config watches");
running.set(false);
Expand All @@ -103,11 +100,6 @@ public void stop() {
});
}

@Override
public boolean isRunning() {
return running.get();
}

@Override
public void contribute(Builder builder) {
builder.withDetail("registrations", registeredConfigs);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
package org.mangadex.mcw.lifecycle.scheduling;

import java.util.Objects;
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.ScheduledFuture;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.concurrent.atomic.AtomicReference;

import org.slf4j.Logger;
Expand All @@ -16,14 +18,17 @@
public final class ScheduledRenderTask {

private static final Logger LOGGER = LoggerFactory.getLogger(ScheduledRenderTask.class);
private static final AtomicInteger SRTi = new AtomicInteger();

private final ScheduledExecutorService executor = Executors.newSingleThreadScheduledExecutor();
private final ScheduledExecutorService executor = Executors.newSingleThreadScheduledExecutor(t -> new Thread(t, "srt-" + SRTi.getAndIncrement()));
private final AtomicReference<ScheduledFuture<?>> nextTick = new AtomicReference<>();

private final ThrowingFunction<String, Render> render;
private final ThrowingConsumer<String> write;
private final long retryDelaySeconds;

private Render lastRender;

public ScheduledRenderTask(
ThrowingFunction<String, Render> render,
ThrowingConsumer<String> write,
Expand All @@ -43,19 +48,28 @@ public void templateChanged(String template) {
nextTick.get().cancel(true);
}

renderWriteAndSchedule(template);
if (executor.isShutdown()) {
LOGGER.info("Render task is shutting down, ignoring template change...");
return;
}
executor.submit(() -> renderWriteAndSchedule(template));
}

private void renderWriteAndSchedule(String template) {
if (executor.isShutdown()) {
LOGGER.warn("Render task is shutting down, ignoring template change...");
return;
}

long nextScheduleSeconds;
try {
Render render = this.render.applyWithException(template);
write.accept(render.rendered());
if (lastRender == null || !Objects.equals(lastRender.rendered(), render.rendered())) {
LOGGER.info("Configuration changed: {} -> {}", lastRender == null ? "null" : lastRender.md5sum(), render.md5sum());
write.accept(render.rendered());
lastRender = render;
} else {
LOGGER.debug("Configuration left unchanged after rendering");
}
nextScheduleSeconds = render.ttl();
} catch (Exception e) {
LOGGER.error("Failed rendering template", e);
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/org/mangadex/mcw/output/file/FSWriter.java
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ public void flush(FSOutput target, String rendered) throws IOException {
LOGGER.debug("Using temporary file {}", tmp.toAbsolutePath());

Files.writeString(tmp, rendered, StandardOpenOption.CREATE, StandardOpenOption.TRUNCATE_EXISTING);
LOGGER.trace("Write rendered configuration to temporary file {}", target);
LOGGER.debug("Wrote rendered configuration to temporary file {}", target);

moveTmpToTarget(tmp, finalPath, writeMethod);
if (target.attributes().uid() != null) {
Expand Down
25 changes: 22 additions & 3 deletions src/main/java/org/mangadex/mcw/render/Render.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,25 @@
package org.mangadex.mcw.render;

import static java.nio.charset.StandardCharsets.UTF_8;
import static org.springframework.util.DigestUtils.md5DigestAsHex;

import jakarta.validation.constraints.NotNull;
import jakarta.validation.constraints.PositiveOrZero;

import org.springframework.validation.annotation.Validated;

@Validated
public record Render(
String rendered,
long ttl
) { }
@NotNull String rendered,
@PositiveOrZero long ttl,
@NotNull String md5sum
) {

public Render(
@NotNull String rendered,
@PositiveOrZero long ttl
) {
this(rendered, ttl, md5DigestAsHex(rendered.getBytes(UTF_8)));
}

}
Loading

0 comments on commit 0e2b3d5

Please sign in to comment.