From cc2aa3c8d5f4ec6a65f1a1c85de0cecd352f79e4 Mon Sep 17 00:00:00 2001 From: 0xboobface <0xboobface@gmail.com> Date: Mon, 26 Nov 2018 15:28:44 +0100 Subject: [PATCH] Add threshold setting for minimum space on disk If there is less space left on the device than specified by the setting, the recorder will stop all recordings and don't start new ones until the free space rises above this threshold again. --- common/src/main/java/ctbrec/Settings.java | 1 + .../java/ctbrec/recorder/LocalRecorder.java | 30 +++++++++++++++++-- 2 files changed, 28 insertions(+), 3 deletions(-) diff --git a/common/src/main/java/ctbrec/Settings.java b/common/src/main/java/ctbrec/Settings.java index a34e24e9..3b613845 100644 --- a/common/src/main/java/ctbrec/Settings.java +++ b/common/src/main/java/ctbrec/Settings.java @@ -37,6 +37,7 @@ public class Settings { public String httpServer = "localhost"; public String recordingsDir = System.getProperty("user.home") + File.separator + "ctbrec"; public DirectoryStructure recordingsDirStructure = DirectoryStructure.FLAT; + public long minimumSpaceLeftInBytes = 0; public String mediaPlayer = "/usr/bin/mpv"; public String postProcessing = ""; public String username = ""; // chaturbate username TODO maybe rename this onetime diff --git a/common/src/main/java/ctbrec/recorder/LocalRecorder.java b/common/src/main/java/ctbrec/recorder/LocalRecorder.java index f4fb1f1f..14aca3a7 100644 --- a/common/src/main/java/ctbrec/recorder/LocalRecorder.java +++ b/common/src/main/java/ctbrec/recorder/LocalRecorder.java @@ -64,6 +64,7 @@ public class LocalRecorder implements Recorder { private List deleteInProgress = Collections.synchronizedList(new ArrayList<>()); private RecorderHttpClient client = new RecorderHttpClient(); private ReentrantLock lock = new ReentrantLock(); + private long lastSpaceMessage = 0; public LocalRecorder(Config config) { this.config = config; @@ -134,7 +135,6 @@ public class LocalRecorder implements Recorder { return; } - LOG.debug("Starting recording for model {}", model.getName()); if (recordingProcesses.containsKey(model)) { LOG.error("A recording for model {} is already running", model); return; @@ -150,6 +150,16 @@ public class LocalRecorder implements Recorder { lock.unlock(); } + if(!enoughSpaceForRecording()) { + long now = System.currentTimeMillis(); + if( (now - lastSpaceMessage) > TimeUnit.MINUTES.toMillis(1)) { + LOG.info("Not enough space for recording, not starting recording for {}", model); + lastSpaceMessage = now; + } + return; + } + + LOG.debug("Starting recording for model {}", model.getName()); Download download; if (Config.getInstance().isServerMode()) { download = new HlsDownload(client); @@ -330,6 +340,15 @@ public class LocalRecorder implements Recorder { public void run() { running = true; while (running) { + try { + if(!enoughSpaceForRecording() && !recordingProcesses.isEmpty()) { + LOG.info("No space left -> Stopping all recordings"); + stopRecordingProcesses(); + } + } catch (IOException e1) { + LOG.warn("Couldn't check free space left", e1); + } + List restart = new ArrayList<>(); for (Iterator> iterator = recordingProcesses.entrySet().iterator(); iterator.hasNext();) { Entry entry = iterator.next(); @@ -416,7 +435,7 @@ public class LocalRecorder implements Recorder { boolean isOnline = model.isOnline(IGNORE_CACHE); LOG.trace("Checking online state for {}: {}", model, (isOnline ? "online" : "offline")); if (isOnline && !isSuspended(model) && !recordingProcesses.containsKey(model)) { - LOG.info("Model {}'s room back to public. Starting recording", model); + LOG.info("Model {}'s room back to public", model); startRecordingProcess(model); } } catch (HttpException e) { @@ -758,8 +777,13 @@ public class LocalRecorder implements Recorder { } private FileStore getRecordingsFileStore() throws IOException { - File recordingsDir = new File(Config.getInstance().getSettings().recordingsDir); + File recordingsDir = new File(config.getSettings().recordingsDir); FileStore store = Files.getFileStore(recordingsDir.toPath()); return store; } + + private boolean enoughSpaceForRecording() throws IOException { + long minimum = config.getSettings().minimumSpaceLeftInBytes; + return getFreeSpaceBytes() > minimum; + } }