...

Source file src/cloud.google.com/go/longrunning/example_test.go

Documentation: cloud.google.com/go/longrunning

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

View as plain text