Like the setlock command using Consul session/kv.
This is a conceptual code, not for a production use.
Now, Consul 0.5 provides lock
command. I recommend use it.
github.com/fujiwara/consul-lock
github.com/fujiwara/consul-lock/releases
Install to $GOPATH.
$ go get github.com/fujiwara/consul-lock
$ consul-lock [-nNxX] KEY program [ arg ... ]
-n: No delay. If KEY is locked by another process, consul-lock gives up.
-N: (Default.) Delay. If KEY is locked by another process, consul-lock waits until it can obtain a new lock.
-x: If KEY is locked, consul-lock exits zero.
-X: (Default.) If KEY is locked, consul-lock prints an error message and exits nonzero.
-lock-delay=15: Consul session LockDelay seconds
MIT