lock_command.go 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. // Copyright 2016 The etcd Authors
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. package command
  15. import (
  16. "errors"
  17. "os"
  18. "os/signal"
  19. "github.com/coreos/etcd/clientv3"
  20. "github.com/coreos/etcd/clientv3/concurrency"
  21. "github.com/spf13/cobra"
  22. "golang.org/x/net/context"
  23. )
  24. // NewLockCommand returns the cobra command for "lock".
  25. func NewLockCommand() *cobra.Command {
  26. c := &cobra.Command{
  27. Use: "lock <lockname>",
  28. Short: "lock acquires a named lock",
  29. Run: lockCommandFunc,
  30. }
  31. return c
  32. }
  33. func lockCommandFunc(cmd *cobra.Command, args []string) {
  34. if len(args) != 1 {
  35. ExitWithError(ExitBadArgs, errors.New("lock takes one lock name arguement."))
  36. }
  37. c := mustClientFromCmd(cmd)
  38. if err := lockUntilSignal(c, args[0]); err != nil {
  39. ExitWithError(ExitError, err)
  40. }
  41. }
  42. func lockUntilSignal(c *clientv3.Client, lockname string) error {
  43. m := concurrency.NewMutex(c, lockname)
  44. ctx, cancel := context.WithCancel(context.TODO())
  45. // unlock in case of ordinary shutdown
  46. donec := make(chan struct{})
  47. sigc := make(chan os.Signal, 1)
  48. signal.Notify(sigc, os.Interrupt, os.Kill)
  49. go func() {
  50. <-sigc
  51. cancel()
  52. close(donec)
  53. }()
  54. s, serr := concurrency.NewSession(c)
  55. if serr != nil {
  56. return serr
  57. }
  58. if err := m.Lock(ctx); err != nil {
  59. return err
  60. }
  61. k, kerr := c.Get(ctx, m.Key())
  62. if kerr != nil {
  63. return kerr
  64. }
  65. if len(k.Kvs) == 0 {
  66. return errors.New("lock lost on init")
  67. }
  68. display.Get(*k)
  69. select {
  70. case <-donec:
  71. return m.Unlock()
  72. case <-s.Done():
  73. }
  74. return errors.New("session expired")
  75. }