2021-04-08 23:55:10 +00:00
|
|
|
package lock
|
|
|
|
|
|
|
|
import (
|
|
|
|
"errors"
|
|
|
|
"path"
|
|
|
|
"sync"
|
|
|
|
|
|
|
|
"github.com/spf13/viper"
|
|
|
|
)
|
|
|
|
|
|
|
|
var lock *viper.Viper
|
|
|
|
var file string
|
|
|
|
var once sync.Once
|
|
|
|
|
|
|
|
func getLock() *viper.Viper {
|
|
|
|
if lock == nil {
|
|
|
|
|
|
|
|
once.Do(func() {
|
|
|
|
lock = viper.New()
|
|
|
|
lock.SetDefault("running", false)
|
|
|
|
p := path.Dir(viper.ConfigFileUsed())
|
|
|
|
file = path.Join(p, ".autorestic.lock.yml")
|
|
|
|
lock.SetConfigFile(file)
|
|
|
|
lock.SetConfigType("yml")
|
|
|
|
lock.ReadInConfig()
|
|
|
|
})
|
|
|
|
}
|
|
|
|
return lock
|
|
|
|
}
|
|
|
|
|
2021-04-11 13:02:27 +00:00
|
|
|
func setLock(locked bool) error {
|
2021-04-08 23:55:10 +00:00
|
|
|
lock := getLock()
|
|
|
|
if locked {
|
|
|
|
running := lock.GetBool("running")
|
|
|
|
if running {
|
|
|
|
return errors.New("an instance is already running")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
lock.Set("running", locked)
|
|
|
|
if err := lock.WriteConfigAs(file); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2021-04-11 13:02:27 +00:00
|
|
|
func GetCron(location string) int64 {
|
|
|
|
lock := getLock()
|
|
|
|
return lock.GetInt64("cron." + location)
|
|
|
|
}
|
|
|
|
|
|
|
|
func SetCron(location string, value int64) {
|
|
|
|
lock.Set("cron."+location, value)
|
|
|
|
lock.WriteConfigAs(file)
|
|
|
|
}
|
|
|
|
|
2021-04-08 23:55:10 +00:00
|
|
|
func Lock() error {
|
2021-04-11 13:02:27 +00:00
|
|
|
return setLock(true)
|
2021-04-08 23:55:10 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func Unlock() error {
|
2021-04-11 13:02:27 +00:00
|
|
|
return setLock(false)
|
2021-04-08 23:55:10 +00:00
|
|
|
}
|