Add flag to disable post-processors

This commit is contained in:
0xb00bface 2023-02-19 18:51:30 +01:00
parent 9e82b61df7
commit 519e6a49f3
4 changed files with 35 additions and 25 deletions

View File

@ -1,16 +1,15 @@
package ctbrec.io; package ctbrec.io;
import java.io.IOException;
import java.lang.reflect.InvocationTargetException;
import java.util.Map.Entry;
import com.squareup.moshi.JsonAdapter; import com.squareup.moshi.JsonAdapter;
import com.squareup.moshi.JsonReader; import com.squareup.moshi.JsonReader;
import com.squareup.moshi.JsonReader.Token; import com.squareup.moshi.JsonReader.Token;
import com.squareup.moshi.JsonWriter; import com.squareup.moshi.JsonWriter;
import ctbrec.recorder.postprocessing.PostProcessor; import ctbrec.recorder.postprocessing.PostProcessor;
import java.io.IOException;
import java.lang.reflect.InvocationTargetException;
import java.util.Map.Entry;
public class PostProcessorJsonAdapter extends JsonAdapter<PostProcessor> { public class PostProcessorJsonAdapter extends JsonAdapter<PostProcessor> {
@Override @Override
@ -18,16 +17,18 @@ public class PostProcessorJsonAdapter extends JsonAdapter<PostProcessor> {
reader.beginObject(); reader.beginObject();
Object type = null; Object type = null;
PostProcessor postProcessor = null; PostProcessor postProcessor = null;
while(reader.hasNext()) { while (reader.hasNext()) {
try { try {
Token token = reader.peek(); Token token = reader.peek();
if(token == Token.NAME) { if (token == Token.NAME) {
String key = reader.nextName(); String key = reader.nextName();
if(key.equals("type")) { if (key.equals("type")) {
type = reader.readJsonValue(); type = reader.readJsonValue();
Class<?> modelClass = Class.forName(type.toString()); Class<?> modelClass = Class.forName(type.toString());
postProcessor = (PostProcessor) modelClass.getDeclaredConstructor().newInstance(); postProcessor = (PostProcessor) modelClass.getDeclaredConstructor().newInstance();
} else if(key.equals("config")) { } else if (key.equals("enabled")) {
postProcessor.setEnabled(reader.nextBoolean());
} else if (key.equals("config")) {
reader.beginObject(); reader.beginObject();
} else { } else {
String value = reader.nextString(); String value = reader.nextString();
@ -36,7 +37,8 @@ public class PostProcessorJsonAdapter extends JsonAdapter<PostProcessor> {
} else { } else {
reader.skipValue(); reader.skipValue();
} }
} catch (ClassNotFoundException | InstantiationException | IllegalAccessException | IllegalArgumentException | InvocationTargetException | NoSuchMethodException | SecurityException e) { } catch (ClassNotFoundException | InstantiationException | IllegalAccessException | IllegalArgumentException | InvocationTargetException |
NoSuchMethodException | SecurityException e) {
throw new IOException("Couldn't instantiate post-processor class [" + type + "]", e); throw new IOException("Couldn't instantiate post-processor class [" + type + "]", e);
} }
} }
@ -50,6 +52,7 @@ public class PostProcessorJsonAdapter extends JsonAdapter<PostProcessor> {
public void toJson(JsonWriter writer, PostProcessor pp) throws IOException { public void toJson(JsonWriter writer, PostProcessor pp) throws IOException {
writer.beginObject(); writer.beginObject();
writer.name("type").value(pp.getClass().getName()); writer.name("type").value(pp.getClass().getName());
writer.name("enabled").value(pp.isEnabled());
writer.name("config"); writer.name("config");
writer.beginObject(); writer.beginObject();
for (Entry<String, String> entry : pp.getConfig().entrySet()) { for (Entry<String, String> entry : pp.getConfig().entrySet()) {

View File

@ -38,7 +38,7 @@ public class NextGenLocalRecorder implements Recorder {
public static final boolean IGNORE_CACHE = true; public static final boolean IGNORE_CACHE = true;
private final List<Model> models = Collections.synchronizedList(new ArrayList<>()); private final List<Model> models = Collections.synchronizedList(new ArrayList<>());
private final Config config; private final Config config;
private volatile boolean recording = true; private volatile boolean recording;
private final ReentrantLock recorderLock = new ReentrantLock(); private final ReentrantLock recorderLock = new ReentrantLock();
private final ReentrantLock modelGroupLock = new ReentrantLock(); private final ReentrantLock modelGroupLock = new ReentrantLock();
private final RecorderHttpClient client; private final RecorderHttpClient client;
@ -190,10 +190,14 @@ public class NextGenLocalRecorder implements Recorder {
List<PostProcessor> postProcessors = config.getSettings().postProcessors; List<PostProcessor> postProcessors = config.getSettings().postProcessors;
PostProcessingContext ctx = createPostProcessingContext(recording); PostProcessingContext ctx = createPostProcessingContext(recording);
for (PostProcessor postProcessor : postProcessors) { for (PostProcessor postProcessor : postProcessors) {
LOG.debug("Running post-processor: {}", postProcessor.getName()); if (postProcessor.isEnabled()) {
boolean continuePP = postProcessor.postprocess(ctx); LOG.debug("Running post-processor: {}", postProcessor.getName());
if (!continuePP) { boolean continuePP = postProcessor.postprocess(ctx);
break; if (!continuePP) {
break;
}
} else {
LOG.debug("Skipping post-processor {} because it is disabled", postProcessor.getName());
} }
} }
recording.refresh(); recording.refresh();
@ -445,7 +449,7 @@ public class NextGenLocalRecorder implements Recorder {
} }
@Override @Override
public List<Recording> getRecordings() throws IOException { public List<Recording> getRecordings() {
return recordingManager.getAll(); return recordingManager.getAll();
} }

View File

@ -1,21 +1,18 @@
package ctbrec.recorder.postprocessing; package ctbrec.recorder.postprocessing;
import lombok.Getter;
import lombok.Setter;
import java.util.HashMap; import java.util.HashMap;
import java.util.Map; import java.util.Map;
@Getter
@Setter
public abstract class AbstractPostProcessor implements PostProcessor { public abstract class AbstractPostProcessor implements PostProcessor {
private Map<String, String> config = new HashMap<>(); private Map<String, String> config = new HashMap<>();
@Override private boolean enabled = true;
public Map<String, String> getConfig() {
return config;
}
@Override
public void setConfig(Map<String, String> conf) {
this.config = conf;
}
@Override @Override
public String toString() { public String toString() {

View File

@ -9,6 +9,7 @@ public interface PostProcessor extends Serializable {
/** /**
* Runs the post-processing code on the given recording * Runs the post-processing code on the given recording
*
* @param ctx {@link PostProcessingContext}, which allows access to post-processing related objects * @param ctx {@link PostProcessingContext}, which allows access to post-processing related objects
* @return false to stop futher post-processing, true to continue * @return false to stop futher post-processing, true to continue
* @throws IOException * @throws IOException
@ -16,6 +17,11 @@ public interface PostProcessor extends Serializable {
*/ */
boolean postprocess(PostProcessingContext ctx) throws IOException, InterruptedException; boolean postprocess(PostProcessingContext ctx) throws IOException, InterruptedException;
boolean isEnabled();
void setEnabled(boolean enabled);
Map<String, String> getConfig(); Map<String, String> getConfig();
void setConfig(Map<String, String> conf); void setConfig(Map<String, String> conf);
} }