Added a quick and dirty file open check
All checks were successful
ci/woodpecker/push/woodpecker Pipeline was successful

This commit is contained in:
Gregory Ballantine 2022-09-01 15:16:56 -04:00
parent 6d1a24cefb
commit 24f9b2b779
2 changed files with 32 additions and 7 deletions

View File

@ -1,12 +1,15 @@
package main package main
import ( import (
"log"
"path/filepath"
"time" "time"
"github.com/spf13/viper" "github.com/spf13/viper"
"git.metaunix.net/BitGoblin/adept/config" "git.metaunix.net/BitGoblin/adept/config"
"git.metaunix.net/BitGoblin/adept/transcoder" "git.metaunix.net/BitGoblin/adept/transcoder"
"git.metaunix.net/BitGoblin/adept/util"
) )
func main() { func main() {
@ -18,7 +21,16 @@ func main() {
for { for {
ingestFiles := r.SearchIngest() ingestFiles := r.SearchIngest()
if len(ingestFiles) < 1 {
log.Println("There were no files found in ingest to transcode; skipping run.")
} else {
for _, i := range ingestFiles { for _, i := range ingestFiles {
// check if the file is open in another program (e.g. still being written to)
if util.IsFileLocked(filepath.Join(viper.GetString("transcoder.repository"), "ingest", i.Name())) {
log.Printf("%s appears to be open in another program; skipping it for this run.", i.Name())
continue
}
// archive file // archive file
r.ArchiveFile(i.Name()) r.ArchiveFile(i.Name())
// transcode file // transcode file
@ -26,6 +38,7 @@ func main() {
// clean up source file // clean up source file
r.CleanupFile(i.Name()) r.CleanupFile(i.Name())
} }
}
// sleep for X minutes - specified in the adept.toml config file // sleep for X minutes - specified in the adept.toml config file
interval := viper.GetInt("transcoder.interval") interval := viper.GetInt("transcoder.interval")

View File

@ -1,6 +1,7 @@
package util package util
import ( import (
"os/exec"
"path" "path"
"strings" "strings"
) )
@ -8,3 +9,14 @@ import (
func FilenameWithoutExtension(filename string) string { func FilenameWithoutExtension(filename string) string {
return strings.TrimSuffix(filename, path.Ext(filename)) return strings.TrimSuffix(filename, path.Ext(filename))
} }
func IsFileLocked(filepath string) bool {
cmd := exec.Command("/usr/bin/lsof", filepath)
stdout, _ := cmd.Output()
if strings.Contains(string(stdout), filepath) {
return true
}
return false
}