2017-12-05 03:46:52 +00:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
2019-02-20 14:24:25 +00:00
|
|
|
"errors"
|
2017-12-05 03:46:52 +00:00
|
|
|
"fmt"
|
|
|
|
"io"
|
2018-04-03 05:21:22 +00:00
|
|
|
"os"
|
2017-12-05 03:46:52 +00:00
|
|
|
"path"
|
|
|
|
|
2018-03-26 09:28:47 +00:00
|
|
|
fslock "github.com/ipfs/go-fs-lock"
|
2017-12-05 03:46:52 +00:00
|
|
|
)
|
|
|
|
|
2018-08-23 12:12:55 +00:00
|
|
|
// lock logic heavily inspired by go-ipfs/repo/fsrepo/lock/lock.go
|
|
|
|
|
2017-12-05 03:46:52 +00:00
|
|
|
// The name of the file used for locking
|
|
|
|
const lockFileName = "cluster.lock"
|
|
|
|
|
2017-12-06 19:10:15 +00:00
|
|
|
var locker *lock
|
2017-12-05 03:46:52 +00:00
|
|
|
|
2017-12-06 19:10:15 +00:00
|
|
|
// lock helps to coordinate procees via a lock file
|
|
|
|
type lock struct {
|
2017-12-05 03:46:52 +00:00
|
|
|
lockCloser io.Closer
|
|
|
|
path string
|
|
|
|
}
|
|
|
|
|
2019-02-20 14:24:25 +00:00
|
|
|
func (l *lock) lock() {
|
2017-12-06 19:10:15 +00:00
|
|
|
if l.lockCloser != nil {
|
2019-02-20 14:24:25 +00:00
|
|
|
checkErr("", errors.New("cannot acquire lock twice"))
|
2017-12-05 03:46:52 +00:00
|
|
|
}
|
2018-04-03 05:21:22 +00:00
|
|
|
|
2019-02-20 14:24:25 +00:00
|
|
|
if _, err := os.Stat(configPath); os.IsNotExist(err) {
|
|
|
|
errMsg := "%s config hasn't been initialized. Please run '%s init'"
|
|
|
|
errMsg = fmt.Sprintf(errMsg, programName, programName)
|
|
|
|
checkErr("", errors.New(errMsg))
|
2018-04-03 05:21:22 +00:00
|
|
|
}
|
|
|
|
|
2017-12-05 03:46:52 +00:00
|
|
|
// set the lock file within this function
|
|
|
|
logger.Debug("checking lock")
|
2018-03-26 09:28:47 +00:00
|
|
|
lk, err := fslock.Lock(l.path, lockFileName)
|
2017-12-05 03:46:52 +00:00
|
|
|
if err != nil {
|
|
|
|
logger.Debug(err)
|
2017-12-06 19:10:15 +00:00
|
|
|
l.lockCloser = nil
|
2019-02-20 14:24:25 +00:00
|
|
|
errStr := "%s. If no other "
|
|
|
|
errStr += "%s process is running, remove %s, or make sure "
|
|
|
|
errStr += "that the config folder is writable for the user "
|
|
|
|
errStr += "running %s."
|
|
|
|
errStr = fmt.Sprintf(
|
|
|
|
errStr,
|
|
|
|
err,
|
|
|
|
programName,
|
|
|
|
path.Join(l.path, lockFileName),
|
|
|
|
programName,
|
|
|
|
)
|
|
|
|
checkErr("obtaining execution lock", errors.New(errStr))
|
2017-12-05 03:46:52 +00:00
|
|
|
}
|
2019-02-20 14:24:25 +00:00
|
|
|
logger.Debugf("%s execution lock acquired", programName)
|
2017-12-06 19:10:15 +00:00
|
|
|
l.lockCloser = lk
|
2017-12-05 03:46:52 +00:00
|
|
|
}
|
|
|
|
|
2017-12-06 19:10:15 +00:00
|
|
|
func (l *lock) tryUnlock() error {
|
2017-12-05 03:46:52 +00:00
|
|
|
// Noop in the uninitialized case
|
2017-12-06 19:10:15 +00:00
|
|
|
if l.lockCloser == nil {
|
2017-12-05 03:46:52 +00:00
|
|
|
logger.Debug("locking not initialized, unlock is noop")
|
|
|
|
return nil
|
|
|
|
}
|
2017-12-06 19:10:15 +00:00
|
|
|
err := l.lockCloser.Close()
|
2017-12-05 03:46:52 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2019-02-20 14:24:25 +00:00
|
|
|
logger.Debug("successfully released execution lock")
|
2017-12-06 19:10:15 +00:00
|
|
|
l.lockCloser = nil
|
2017-12-05 03:46:52 +00:00
|
|
|
return nil
|
|
|
|
}
|