Made the transcoder run as a periodic task, with the interval defined in the config TOML file
All checks were successful
ci/woodpecker/push/woodpecker Pipeline was successful
ci/woodpecker/tag/woodpecker Pipeline was successful

This commit is contained in:
Gregory Ballantine 2022-05-04 00:40:25 -04:00
parent 347211d566
commit 5bde5bfee0
4 changed files with 78 additions and 8 deletions

View File

@ -1,7 +1,10 @@
package tech.bitgoblin;
import tech.bitgoblin.config.Config;
import tech.bitgoblin.video.Transcoder;
import tech.bitgoblin.transcoder.RunTranscoderTask;
import tech.bitgoblin.transcoder.Transcoder;
import java.util.Timer;
/**
* The Bit Goblin video transcoder service.
@ -16,7 +19,8 @@ public class App {
Config c = new Config(configFile);
// create new Transcoder object and start the service
Transcoder t = new Transcoder(c);
t.transcode();
Timer timer = new Timer();
timer.scheduleAtFixedRate(new RunTranscoderTask(t), 2500, 120 * 1000);
}
}

View File

@ -4,6 +4,8 @@ import java.io.File;
import java.io.IOException;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.Objects;
import org.tomlj.Toml;
import org.tomlj.TomlParseResult;
import tech.bitgoblin.io.IOUtils;
@ -28,6 +30,10 @@ public class Config {
return this.result.getString(key);
}
public int getInt(String key) {
return Objects.requireNonNull(this.result.getDouble(key)).intValue();
}
public boolean contains(String key) {
return this.result.contains(key);
}

View File

@ -0,0 +1,23 @@
package tech.bitgoblin.transcoder;
import java.util.TimerTask;
public class RunTranscoderTask extends TimerTask {
private Transcoder transcoder;
public RunTranscoderTask(Transcoder t) {
this.transcoder = t;
}
@Override
public void run() {
// archive the files
transcoder.archive();
// run the transcoder
transcoder.transcode();
// clean up ingest
transcoder.cleanup();
}
}

View File

@ -1,16 +1,14 @@
package tech.bitgoblin.video;
package tech.bitgoblin.transcoder;
import java.io.BufferedReader;
import java.io.File;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.IOException;
import java.lang.InterruptedException;
import java.lang.Process;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.function.Consumer;
import java.util.concurrent.Executors;
import java.nio.file.StandardCopyOption;
import java.util.Timer;
import tech.bitgoblin.config.Config;
import tech.bitgoblin.io.IOUtils;
@ -31,6 +29,12 @@ public class Transcoder {
this.initDirectory();
}
// create a periodic timer task and start it
public void start() {
Timer timer = new Timer();
timer.scheduleAtFixedRate(new RunTranscoderTask(this), 10000, this.config.getInt("transcoder.interval") * 60 * 1000);
}
// transcode files in the working directory
public void transcode() {
// search for files
@ -38,6 +42,11 @@ public class Transcoder {
File repo = new File(Paths.get(this.repo_dir, "ingest").toString());
File[] sourceFiles = repo.listFiles();
if (sourceFiles.length == 0) {
System.out.println("There is nothing to transcode in " + this.repo_dir + "/ingest.");
return;
}
// transcode
System.out.println("Transcoding files in " + this.repo_dir + "/ingest...");
for (File f : sourceFiles) {
@ -74,6 +83,34 @@ public class Transcoder {
System.out.println();
}
// copies sources files to the archive directory
public void archive() {
File repo = new File(Paths.get(this.repo_dir, "ingest").toString());
File[] sourceFiles = repo.listFiles();
for (File f : sourceFiles) {
Path filePath = Path.of(f.toString());
String filename = filePath.getFileName().toString();
String archivePath = Paths.get(this.repo_dir, "archive", filename).toString();
try {
Files.copy(filePath, Paths.get(archivePath), StandardCopyOption.COPY_ATTRIBUTES, StandardCopyOption.REPLACE_EXISTING);
} catch (IOException e) {
throw new RuntimeException(e);
}
}
}
// clean up the ingest directory once we're done
public void cleanup() {
File repo = new File(Paths.get(this.repo_dir, "ingest").toString());
File[] sourceFiles = repo.listFiles();
for (File f : sourceFiles) {
f.delete();
}
}
// ensures the transcoder's working directory is available
private void initDirectory() {
// create transcoder directory