2024-11-10 18:37:59 +00:00
|
|
|
package internal
|
2021-04-08 23:55:10 +00:00
|
|
|
|
|
|
|
import (
|
2021-04-17 18:51:40 +00:00
|
|
|
"os"
|
2021-04-08 23:55:10 +00:00
|
|
|
"path"
|
|
|
|
"sync"
|
|
|
|
|
2021-04-17 18:51:40 +00:00
|
|
|
"github.com/cupcakearmy/autorestic/internal/colors"
|
2022-02-16 20:42:54 +00:00
|
|
|
"github.com/cupcakearmy/autorestic/internal/flags"
|
2021-04-08 23:55:10 +00:00
|
|
|
"github.com/spf13/viper"
|
|
|
|
)
|
|
|
|
|
|
|
|
var lock *viper.Viper
|
|
|
|
var file string
|
2024-11-10 18:37:59 +00:00
|
|
|
var lockOnce sync.Once
|
2021-04-08 23:55:10 +00:00
|
|
|
|
2022-06-06 10:59:47 +00:00
|
|
|
const (
|
|
|
|
RUNNING = "running"
|
|
|
|
)
|
|
|
|
|
2024-07-03 01:12:26 +00:00
|
|
|
// getLockfilePath returns the path to the lockfile. If flags.LOCKFILE_PATH is
|
|
|
|
// set, its value is used, otherwise the path is generated relative to the
|
|
|
|
// config file.
|
|
|
|
func getLockfilePath() string {
|
|
|
|
if flags.LOCKFILE_PATH != "" {
|
|
|
|
return flags.LOCKFILE_PATH
|
|
|
|
} else {
|
|
|
|
p := viper.ConfigFileUsed()
|
|
|
|
if p == "" {
|
|
|
|
colors.Error.Println("cannot lock before reading config location")
|
|
|
|
os.Exit(1)
|
|
|
|
}
|
|
|
|
return path.Join(path.Dir(p), ".autorestic.lock.yml")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-04-08 23:55:10 +00:00
|
|
|
func getLock() *viper.Viper {
|
|
|
|
if lock == nil {
|
2024-11-10 18:37:59 +00:00
|
|
|
lockOnce.Do(func() {
|
2021-04-08 23:55:10 +00:00
|
|
|
lock = viper.New()
|
|
|
|
lock.SetDefault("running", false)
|
2024-07-03 01:12:26 +00:00
|
|
|
file = getLockfilePath()
|
2022-02-16 20:42:54 +00:00
|
|
|
if !flags.CRON_LEAN {
|
|
|
|
colors.Faint.Println("Using lock:\t", file)
|
|
|
|
}
|
2021-04-08 23:55:10 +00:00
|
|
|
lock.SetConfigFile(file)
|
|
|
|
lock.SetConfigType("yml")
|
|
|
|
lock.ReadInConfig()
|
|
|
|
})
|
|
|
|
}
|
|
|
|
return lock
|
|
|
|
}
|
|
|
|
|
2022-06-06 10:59:47 +00:00
|
|
|
func setLockValue(key string, value interface{}) (*viper.Viper, error) {
|
2021-04-08 23:55:10 +00:00
|
|
|
lock := getLock()
|
2022-06-06 10:59:47 +00:00
|
|
|
|
|
|
|
if key == RUNNING {
|
|
|
|
value := value.(bool)
|
|
|
|
if value && lock.GetBool(key) {
|
2021-04-17 18:51:40 +00:00
|
|
|
colors.Error.Println("an instance is already running. exiting")
|
|
|
|
os.Exit(1)
|
2021-04-08 23:55:10 +00:00
|
|
|
}
|
|
|
|
}
|
2022-06-06 10:59:47 +00:00
|
|
|
|
|
|
|
lock.Set(key, value)
|
2021-04-08 23:55:10 +00:00
|
|
|
if err := lock.WriteConfigAs(file); err != nil {
|
2022-06-06 10:59:47 +00:00
|
|
|
return nil, err
|
2021-04-08 23:55:10 +00:00
|
|
|
}
|
2022-06-06 10:59:47 +00:00
|
|
|
return lock, nil
|
2021-04-08 23:55:10 +00:00
|
|
|
}
|
|
|
|
|
2021-04-11 13:02:27 +00:00
|
|
|
func GetCron(location string) int64 {
|
2022-06-06 10:59:47 +00:00
|
|
|
return getLock().GetInt64("cron." + location)
|
2021-04-11 13:02:27 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func SetCron(location string, value int64) {
|
2022-06-06 10:59:47 +00:00
|
|
|
setLockValue("cron."+location, value)
|
2021-04-11 13:02:27 +00:00
|
|
|
}
|
|
|
|
|
2021-04-08 23:55:10 +00:00
|
|
|
func Lock() error {
|
2022-06-06 10:59:47 +00:00
|
|
|
_, err := setLockValue(RUNNING, true)
|
|
|
|
return err
|
2021-04-08 23:55:10 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func Unlock() error {
|
2022-06-06 10:59:47 +00:00
|
|
|
_, err := setLockValue(RUNNING, false)
|
|
|
|
return err
|
2021-04-08 23:55:10 +00:00
|
|
|
}
|