forked from j62/ctbrec
1
0
Fork 0

Reduce config save spam from recorder operations (400ms delay)

This commit is contained in:
reusedname 2025-02-27 21:23:50 +05:00
parent 994bb99b1f
commit 466d705131
1 changed files with 23 additions and 9 deletions

View File

@ -30,6 +30,7 @@ import java.util.*;
import java.util.concurrent.*;
import java.util.concurrent.locks.ReentrantLock;
import java.util.stream.Collectors;
import com.google.common.collect.Ordering;
import static ctbrec.Recording.State.WAITING;
@ -64,6 +65,9 @@ public class SimplifiedLocalRecorder implements Recorder {
private final Thread maintenanceThread;
private long lastSpaceCheck;
Timer saveConfigTimer = new Timer();
TimerTask saveConfigTask = null;
public SimplifiedLocalRecorder(Config config, List<Site> sites) throws IOException {
this.config = config;
@ -553,16 +557,30 @@ public class SimplifiedLocalRecorder implements Recorder {
}
getRecordingProcessForModel(model).ifPresent(this::stopRecordingProcess);
} catch (IOException e) {
errorSavingConfig(e);
} finally {
recorderLock.unlock();
}
}
private void saveConfig() throws IOException {
config.getSettings().models = models.stream().map(Mappers.getMapper(ModelMapper.class)::toDto).collect(Collectors.toList());
config.save();
private void saveConfig() {
if (saveConfigTask != null)
saveConfigTask.cancel();
saveConfigTask = new TimerTask() {
@Override
public void run() {
recorderLock.lock();
try {
config.getSettings().models = models.stream().map(Mappers.getMapper(ModelMapper.class)::toDto).collect(Collectors.toList());
config.save();
} catch (IOException e) {
errorSavingConfig(e);
} finally {
recorderLock.unlock();
}
}
};
saveConfigTimer.schedule(saveConfigTask, 400);
}
@Override
@ -622,8 +640,6 @@ public class SimplifiedLocalRecorder implements Recorder {
}
getRecordingProcessForModel(model).ifPresent(this::stopRecordingProcess);
} catch (IOException e) {
errorSavingConfig(e);
} finally {
recorderLock.unlock();
}
@ -901,8 +917,6 @@ public class SimplifiedLocalRecorder implements Recorder {
} else {
log.warn("Couldn't change priority for model {}. Not found in list", model.getName());
}
} catch (IOException e) {
errorSavingConfig(e);
} finally {
recorderLock.unlock();
}