Skip to content

Take control over your live stream video by running it yourself. Streaming + chat out of the box.

fix(logging): remove stale rotation lock files at startup

Gabe Kangas9c77c5cd00e5
diff --git a/logging/logging.go b/logging/logging.go
index a07126f326..132efa4827 100644
--- a/logging/logging.go
+++ b/logging/logging.go
@@ -39,6 +39,9 @@ func Setup(logDirectory string, enableDebugOptions bool, enableVerboseLogging bo
}
}
+ removeStaleRotationFiles(getLogFilePath(logDirectory))
+ removeStaleRotationFiles(GetTranscoderLogFilePath(logDirectory))
+
// Write logs to a file
path := getLogFilePath(logDirectory)
writer, _ := rotatelogs.New(
@@ -82,6 +85,24 @@ func Setup(logDirectory string, enableDebugOptions bool, enableVerboseLogging bo
Logger = _logger
}
+// removeStaleRotationFiles deletes leftover rotatelogs lock and symlink
+// staging files for the given log path. They can survive a previous run
+// that was killed mid-rotation, and a stale lock file blocks every future
+// rotation of that file with a "failed to rotate" error.
+func removeStaleRotationFiles(logFilePath string) {
+ for _, suffix := range []string{"_lock", "_symlink"} {
+ matches, err := filepath.Glob(logFilePath + ".*" + suffix)
+ if err != nil {
+ continue
+ }
+ for _, staleFile := range matches {
+ if err := os.Remove(staleFile); err != nil {
+ log.Debugln("unable to remove stale log rotation file", staleFile, err)
+ }
+ }
+ }
+}
+
// Fire runs for every logging request.
func (l *OCLogger) Fire(e *log.Entry) error {
// Store all log messages to return back in the logging API
diff --git a/logging/logging_test.go b/logging/logging_test.go
new file mode 100644
index 0000000000..7d8c996a0d
--- /dev/null
+++ b/logging/logging_test.go
@@ -0,0 +1,46 @@
+package logging
+
+import (
+ "os"
+ "path/filepath"
+ "testing"
+)
+
+// A previous run that dies mid-rotation can leave rotatelogs _lock and
+// _symlink staging files behind, which block every future rotation of that
+// log. Verify startup cleanup removes them without touching real log files.
+func TestRemoveStaleRotationFiles(t *testing.T) {
+ dir := t.TempDir()
+ logPath := filepath.Join(dir, "transcoder.log")
+
+ staleFiles := []string{
+ "transcoder.log.202607020000_lock",
+ "transcoder.log.202607020000.1_lock",
+ "transcoder.log.202607020000_symlink",
+ }
+ keepFiles := []string{
+ "transcoder.log.202607020000",
+ "transcoder.log.202607020000.1",
+ "owncast.log.202607020000_lock",
+ }
+
+ for _, name := range append(append([]string{}, staleFiles...), keepFiles...) {
+ if err := os.WriteFile(filepath.Join(dir, name), nil, 0o600); err != nil {
+ t.Fatalf("unable to create test file %s: %v", name, err)
+ }
+ }
+
+ removeStaleRotationFiles(logPath)
+
+ for _, name := range staleFiles {
+ if _, err := os.Stat(filepath.Join(dir, name)); !os.IsNotExist(err) {
+ t.Errorf("stale rotation file %s should have been removed", name)
+ }
+ }
+
+ for _, name := range keepFiles {
+ if _, err := os.Stat(filepath.Join(dir, name)); err != nil {
+ t.Errorf("file %s should not have been removed: %v", name, err)
+ }
+ }
+}