Add cache maintenance locking
This commit is contained in:
@ -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)
|
||||
|
Reference in New Issue
Block a user