You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 

117 lines
2.8 KiB

  1. // Copyright 2016 Google LLC
  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 longrunning
  15. import (
  16. "context"
  17. "fmt"
  18. "time"
  19. "github.com/golang/protobuf/ptypes"
  20. "github.com/golang/protobuf/ptypes/duration"
  21. "github.com/golang/protobuf/ptypes/timestamp"
  22. pb "google.golang.org/genproto/googleapis/longrunning"
  23. )
  24. func bestMomentInHistory() (*Operation, error) {
  25. t, err := time.Parse("2006-01-02 15:04:05.999999999 -0700 MST", "2009-11-10 23:00:00 +0000 UTC")
  26. if err != nil {
  27. return nil, err
  28. }
  29. resp, err := ptypes.TimestampProto(t)
  30. if err != nil {
  31. return nil, err
  32. }
  33. respAny, err := ptypes.MarshalAny(resp)
  34. if err != nil {
  35. return nil, err
  36. }
  37. metaAny, err := ptypes.MarshalAny(ptypes.DurationProto(1 * time.Hour))
  38. return &Operation{
  39. proto: &pb.Operation{
  40. Name: "best-moment",
  41. Done: true,
  42. Metadata: metaAny,
  43. Result: &pb.Operation_Response{
  44. Response: respAny,
  45. },
  46. },
  47. }, err
  48. }
  49. func ExampleOperation_Wait() {
  50. // Complex computation, might take a long time.
  51. op, err := bestMomentInHistory()
  52. if err != nil {
  53. // TODO: Handle err.
  54. }
  55. var ts timestamp.Timestamp
  56. err = op.Wait(context.TODO(), &ts)
  57. if err != nil && !op.Done() {
  58. fmt.Println("failed to fetch operation status", err)
  59. } else if err != nil && op.Done() {
  60. fmt.Println("operation completed with error", err)
  61. } else {
  62. fmt.Println(ptypes.TimestampString(&ts))
  63. }
  64. // Output:
  65. // 2009-11-10T23:00:00Z
  66. }
  67. func ExampleOperation_Metadata() {
  68. op, err := bestMomentInHistory()
  69. if err != nil {
  70. // TODO: Handle err.
  71. }
  72. // The operation might contain metadata.
  73. // In this example, the metadata contains the estimated length of time
  74. // the operation might take to complete.
  75. var meta duration.Duration
  76. if err := op.Metadata(&meta); err != nil {
  77. // TODO: Handle err.
  78. }
  79. d, err := ptypes.Duration(&meta)
  80. if err == ErrNoMetadata {
  81. fmt.Println("no metadata")
  82. } else if err != nil {
  83. // TODO: Handle err.
  84. } else {
  85. fmt.Println(d)
  86. }
  87. // Output:
  88. // 1h0m0s
  89. }
  90. func ExampleOperation_Cancel() {
  91. op, err := bestMomentInHistory()
  92. if err != nil {
  93. // TODO: Handle err.
  94. }
  95. if err := op.Cancel(context.Background()); err != nil {
  96. // TODO: Handle err.
  97. }
  98. }
  99. func ExampleOperation_Delete() {
  100. op, err := bestMomentInHistory()
  101. if err != nil {
  102. // TODO: Handle err.
  103. }
  104. if err := op.Delete(context.Background()); err != nil {
  105. // TODO: Handle err.
  106. }
  107. }