lock_command.go 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  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. "syscall"
  20. "github.com/coreos/etcd/clientv3"
  21. "github.com/coreos/etcd/clientv3/concurrency"
  22. "github.com/spf13/cobra"
  23. "golang.org/x/net/context"
  24. )
  25. // NewLockCommand returns the cobra command for "lock".
  26. func NewLockCommand() *cobra.Command {
  27. c := &cobra.Command{
  28. Use: "lock <lockname>",
  29. Short: "Acquires a named lock",
  30. Run: lockCommandFunc,
  31. }
  32. return c
  33. }
  34. func lockCommandFunc(cmd *cobra.Command, args []string) {
  35. if len(args) != 1 {
  36. ExitWithError(ExitBadArgs, errors.New("lock takes one lock name argument."))
  37. }
  38. c := mustClientFromCmd(cmd)
  39. if err := lockUntilSignal(c, args[0]); err != nil {
  40. ExitWithError(ExitError, err)
  41. }
  42. }
  43. func lockUntilSignal(c *clientv3.Client, lockname string) error {
  44. s, err := concurrency.NewSession(c)
  45. if err != nil {
  46. return err
  47. }
  48. m := concurrency.NewMutex(s, lockname)
  49. ctx, cancel := context.WithCancel(context.TODO())
  50. // unlock in case of ordinary shutdown
  51. donec := make(chan struct{})
  52. sigc := make(chan os.Signal, 1)
  53. signal.Notify(sigc, syscall.SIGINT, syscall.SIGTERM)
  54. go func() {
  55. <-sigc
  56. cancel()
  57. close(donec)
  58. }()
  59. if err := m.Lock(ctx); err != nil {
  60. return err
  61. }
  62. k, kerr := c.Get(ctx, m.Key())
  63. if kerr != nil {
  64. return kerr
  65. }
  66. if len(k.Kvs) == 0 {
  67. return errors.New("lock lost on init")
  68. }
  69. display.Get(*k)
  70. select {
  71. case <-donec:
  72. return m.Unlock(context.TODO())
  73. case <-s.Done():
  74. }
  75. return errors.New("session expired")
  76. }