Add cache maintenance locking

This commit is contained in:
2023-12-10 00:05:39 -05:00
parent 78662b3e09
commit 5d2ca40d04
4 changed files with 36 additions and 4 deletions

View File

@ -51,6 +51,33 @@ func (c *Cache) Load() error {
return err
}
func (c *Cache) UnlockCache() {
c.log.Info("Unlocking cache")
if err := os.Remove(c.file + ".lock"); err != nil {
c.log.Fatal("Failed unlocking cache, manual rm may be necessary",
c.log.Args("error", err, "lockFile", c.file+".lock"),
)
}
}
func (c *Cache) LockCache() {
c.log.Info("Attempting to lock cache")
c.checkLock()
file, err := os.OpenFile(c.file+".lock", os.O_RDWR|os.O_CREATE|os.O_TRUNC, 0640)
defer file.Close()
if err != nil {
c.log.Fatal("Failed to lock cache", c.log.Args("error", err))
}
}
func (c *Cache) checkLock() {
if _, err := os.Stat(c.file + ".lock"); err == nil {
c.log.Fatal("Can't manage cache, already locked")
}
}
// Saves the current state of the cache to disk
func (c *Cache) write() {
file, err := os.OpenFile(c.file, os.O_RDWR|os.O_CREATE|os.O_TRUNC, 0640)