From 24f9b2b779cd60d951eb8af577927ec023119d49 Mon Sep 17 00:00:00 2001 From: Gregory Ballantine Date: Thu, 1 Sep 2022 15:16:56 -0400 Subject: [PATCH] Added a quick and dirty file open check --- adept.go | 27 ++++++++++++++++++++------- util/file.go | 12 ++++++++++++ 2 files changed, 32 insertions(+), 7 deletions(-) diff --git a/adept.go b/adept.go index fa44153..d7aa932 100644 --- a/adept.go +++ b/adept.go @@ -1,12 +1,15 @@ package main import ( + "log" + "path/filepath" "time" "github.com/spf13/viper" "git.metaunix.net/BitGoblin/adept/config" "git.metaunix.net/BitGoblin/adept/transcoder" + "git.metaunix.net/BitGoblin/adept/util" ) func main() { @@ -18,13 +21,23 @@ func main() { for { ingestFiles := r.SearchIngest() - for _, i := range ingestFiles { - // archive file - r.ArchiveFile(i.Name()) - // transcode file - transcoder.Transcode(i.Name()) - // clean up source file - r.CleanupFile(i.Name()) + if len(ingestFiles) < 1 { + log.Println("There were no files found in ingest to transcode; skipping run.") + } else { + 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 + r.ArchiveFile(i.Name()) + // transcode file + transcoder.Transcode(i.Name()) + // clean up source file + r.CleanupFile(i.Name()) + } } // sleep for X minutes - specified in the adept.toml config file diff --git a/util/file.go b/util/file.go index e2ca568..0807cf4 100644 --- a/util/file.go +++ b/util/file.go @@ -1,6 +1,7 @@ package util import ( + "os/exec" "path" "strings" ) @@ -8,3 +9,14 @@ import ( func FilenameWithoutExtension(filename string) string { 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 +}