Skip to content

Commit

Permalink
Add sleep-before and quiet options
Browse files Browse the repository at this point in the history
  • Loading branch information
lomik committed Nov 30, 2016
1 parent fd77267 commit 423c128
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 2 deletions.
1 change: 0 additions & 1 deletion elock.go
Original file line number Diff line number Diff line change
Expand Up @@ -144,7 +144,6 @@ func New(options Options) (*XLock, error) {
etcdClient: etcdClient,
}

x.Debug("value: %s", x.lockValue)
return x, nil
}

Expand Down
18 changes: 17 additions & 1 deletion main/elock.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"fmt"
"io/ioutil"
"log"
"math/rand"
"os"
"os/exec"
"os/signal"
Expand All @@ -17,7 +18,7 @@ import (
)

const APP = "elock"
const VERSION = "0.3.2"
const VERSION = "0.4.0"

type Config struct {
EtcdEndpoints []string `json:"etcd-endpoints"`
Expand All @@ -41,6 +42,8 @@ func main() {
remove := flag.Bool("rm", false, "Remove lock by path (from list)")
zeroExit := flag.Bool("0", false, "Exit code 0 on etcd errors or lock timeout")
runAnyway := flag.Bool("run-anyway", false, "Run the command even if the lock could not take")
sleepBefore := flag.Duration("sleep-before", 0, "Sleep random time (from zero to selected) before lock attempt")
quiet := flag.Bool("quiet", false, "Don't print anything")

flag.Usage = func() {
fmt.Fprintf(os.Stderr, `%s %s
Expand Down Expand Up @@ -70,6 +73,19 @@ Usage: %s [options] etcd_key command
return
}

if *quiet {
log.SetOutput(ioutil.Discard)
}

if *sleepBefore != 0 {
rand.Seed(time.Now().UnixNano())
sleepTime := time.Duration(rand.Int63n((*sleepBefore).Nanoseconds()))
if *debug {
log.Printf("sleep-before: %s", sleepTime.String())
}
time.Sleep(sleepTime)
}

exit := func(code int) {
if *zeroExit {
os.Exit(0)
Expand Down

0 comments on commit 423c128

Please sign in to comment.