From c681c76cc85348438a03f3e09661ac6aa59d7c7c Mon Sep 17 00:00:00 2001
From: Edward Welch <edward.welch@grafana.com>
Date: Mon, 15 Apr 2019 20:03:19 -0400
Subject: [PATCH] periodically clean up positions file, removing any files
 which no longer exist

---
 pkg/promtail/positions/positions.go | 16 ++++++++++++++++
 1 file changed, 16 insertions(+)

diff --git a/pkg/promtail/positions/positions.go b/pkg/promtail/positions/positions.go
index c2dedf13..cddd031c 100644
--- a/pkg/promtail/positions/positions.go
+++ b/pkg/promtail/positions/positions.go
@@ -107,6 +107,7 @@ func (p *Positions) run() {
 			return
 		case <-ticker.C:
 			p.save()
+			p.cleanup()
 		}
 	}
 }
@@ -124,6 +125,21 @@ func (p *Positions) save() {
 	}
 }
 
+func (p *Positions) cleanup() {
+	for k := range p.positions {
+		if _, err := os.Stat(k); err == nil {
+			// File still exists.
+		} else if os.IsNotExist(err) {
+			// File no longer exists.
+			p.Remove(k)
+		} else {
+			// Can't determine if file exists or not, some other error.
+			level.Warn(p.logger).Log("msg", "could not determine if log file "+
+				"still exists while cleaning positions file", "error", err)
+		}
+	}
+}
+
 func readPositionsFile(filename string) (map[string]int64, error) {
 	buf, err := ioutil.ReadFile(filepath.Clean(filename))
 	if err != nil {
-- 
GitLab