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.
This commit is contained in:
0xboobface 2018-11-26 15:28:44 +01:00
parent 5708d7f259
commit cc2aa3c8d5
2 changed files with 28 additions and 3 deletions

View File

@ -37,6 +37,7 @@ public class Settings {
public String httpServer = "localhost"; public String httpServer = "localhost";
public String recordingsDir = System.getProperty("user.home") + File.separator + "ctbrec"; public String recordingsDir = System.getProperty("user.home") + File.separator + "ctbrec";
public DirectoryStructure recordingsDirStructure = DirectoryStructure.FLAT; public DirectoryStructure recordingsDirStructure = DirectoryStructure.FLAT;
public long minimumSpaceLeftInBytes = 0;
public String mediaPlayer = "/usr/bin/mpv"; public String mediaPlayer = "/usr/bin/mpv";
public String postProcessing = ""; public String postProcessing = "";
public String username = ""; // chaturbate username TODO maybe rename this onetime public String username = ""; // chaturbate username TODO maybe rename this onetime

View File

@ -64,6 +64,7 @@ public class LocalRecorder implements Recorder {
private List<File> deleteInProgress = Collections.synchronizedList(new ArrayList<>()); private List<File> deleteInProgress = Collections.synchronizedList(new ArrayList<>());
private RecorderHttpClient client = new RecorderHttpClient(); private RecorderHttpClient client = new RecorderHttpClient();
private ReentrantLock lock = new ReentrantLock(); private ReentrantLock lock = new ReentrantLock();
private long lastSpaceMessage = 0;
public LocalRecorder(Config config) { public LocalRecorder(Config config) {
this.config = config; this.config = config;
@ -134,7 +135,6 @@ public class LocalRecorder implements Recorder {
return; return;
} }
LOG.debug("Starting recording for model {}", model.getName());
if (recordingProcesses.containsKey(model)) { if (recordingProcesses.containsKey(model)) {
LOG.error("A recording for model {} is already running", model); LOG.error("A recording for model {} is already running", model);
return; return;
@ -150,6 +150,16 @@ public class LocalRecorder implements Recorder {
lock.unlock(); 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; Download download;
if (Config.getInstance().isServerMode()) { if (Config.getInstance().isServerMode()) {
download = new HlsDownload(client); download = new HlsDownload(client);
@ -330,6 +340,15 @@ public class LocalRecorder implements Recorder {
public void run() { public void run() {
running = true; running = true;
while (running) { 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<Model> restart = new ArrayList<>(); List<Model> restart = new ArrayList<>();
for (Iterator<Entry<Model, Download>> iterator = recordingProcesses.entrySet().iterator(); iterator.hasNext();) { for (Iterator<Entry<Model, Download>> iterator = recordingProcesses.entrySet().iterator(); iterator.hasNext();) {
Entry<Model, Download> entry = iterator.next(); Entry<Model, Download> entry = iterator.next();
@ -416,7 +435,7 @@ public class LocalRecorder implements Recorder {
boolean isOnline = model.isOnline(IGNORE_CACHE); boolean isOnline = model.isOnline(IGNORE_CACHE);
LOG.trace("Checking online state for {}: {}", model, (isOnline ? "online" : "offline")); LOG.trace("Checking online state for {}: {}", model, (isOnline ? "online" : "offline"));
if (isOnline && !isSuspended(model) && !recordingProcesses.containsKey(model)) { 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); startRecordingProcess(model);
} }
} catch (HttpException e) { } catch (HttpException e) {
@ -758,8 +777,13 @@ public class LocalRecorder implements Recorder {
} }
private FileStore getRecordingsFileStore() throws IOException { 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()); FileStore store = Files.getFileStore(recordingsDir.toPath());
return store; return store;
} }
private boolean enoughSpaceForRecording() throws IOException {
long minimum = config.getSettings().minimumSpaceLeftInBytes;
return getFreeSpaceBytes() > minimum;
}
} }