go.uber.org/cadence@v1.2.9/test/replaytests/exclusive_choice_workflow.go (about)

     1  // Copyright (c) 2017 Uber Technologies, Inc.
     2  //
     3  // Permission is hereby granted, free of charge, to any person obtaining a copy
     4  // of this software and associated documentation files (the "Software"), to deal
     5  // in the Software without restriction, including without limitation the rights
     6  // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
     7  // copies of the Software, and to permit persons to whom the Software is
     8  // furnished to do so, subject to the following conditions:
     9  //
    10  // The above copyright notice and this permission notice shall be included in
    11  // all copies or substantial portions of the Software.
    12  //
    13  // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
    14  // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
    15  // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
    16  // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
    17  // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
    18  // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
    19  // THE SOFTWARE.
    20  
    21  package replaytests
    22  
    23  import (
    24  	"fmt"
    25  	"time"
    26  
    27  	"go.uber.org/zap"
    28  
    29  	"go.uber.org/cadence/workflow"
    30  )
    31  
    32  /**
    33   * This sample workflow Execute one of many code paths based on the result of an activity.
    34   */
    35  
    36  const (
    37  	orderChoiceApple  = "apple"
    38  	orderChoiceBanana = "banana"
    39  	orderChoiceCherry = "cherry"
    40  )
    41  
    42  // exclusiveChoiceWorkflow Workflow Decider. This workflow executes Cherry order.
    43  func exclusiveChoiceWorkflow(ctx workflow.Context) error {
    44  	// Get order.
    45  	ao := workflow.ActivityOptions{
    46  		ScheduleToStartTimeout: time.Minute,
    47  		StartToCloseTimeout:    time.Minute,
    48  		HeartbeatTimeout:       time.Second * 20,
    49  	}
    50  	ctx = workflow.WithActivityOptions(ctx, ao)
    51  
    52  	var orderChoice string
    53  	err := workflow.ExecuteActivity(ctx, getOrderActivity).Get(ctx, &orderChoice)
    54  	if err != nil {
    55  		return err
    56  	}
    57  
    58  	logger := workflow.GetLogger(ctx)
    59  
    60  	// choose next activity based on order result
    61  	switch orderChoice {
    62  	case orderChoiceBanana:
    63  		workflow.ExecuteActivity(ctx, orderBananaActivity, orderChoice)
    64  	case orderChoiceCherry:
    65  		workflow.ExecuteActivity(ctx, orderCherryActivity, orderChoice)
    66  	default:
    67  		logger.Error("Unexpected order", zap.String("Choice", orderChoice))
    68  	}
    69  
    70  	logger.Info("Workflow completed.")
    71  	return nil
    72  }
    73  
    74  // This workflow explicitly executes Apple Activity received from the getorderActivity.
    75  func exclusiveChoiceWorkflow2(ctx workflow.Context) error {
    76  	// Get order.
    77  	ao := workflow.ActivityOptions{
    78  		ScheduleToStartTimeout: time.Minute,
    79  		StartToCloseTimeout:    time.Minute,
    80  		HeartbeatTimeout:       time.Second * 20,
    81  	}
    82  	ctx = workflow.WithActivityOptions(ctx, ao)
    83  
    84  	var orderChoice string
    85  	err := workflow.ExecuteActivity(ctx, getAppleOrderActivity).Get(ctx, &orderChoice)
    86  	if err != nil {
    87  		return err
    88  	}
    89  
    90  	logger := workflow.GetLogger(ctx)
    91  
    92  	// choose next activity based on order result. It's apple in this case.
    93  	switch orderChoice {
    94  	case orderChoiceApple:
    95  		workflow.ExecuteActivity(ctx, orderAppleActivity, orderChoice)
    96  	default:
    97  		logger.Error("Unexpected order", zap.String("Choice", orderChoice))
    98  	}
    99  
   100  	logger.Info("Workflow completed.")
   101  	return nil
   102  }
   103  
   104  func getOrderActivity() (string, error) {
   105  	fmt.Printf("Order is for Cherry")
   106  	return "cherry", nil
   107  }
   108  
   109  func getAppleOrderActivity() (string, error) {
   110  	fmt.Printf("Order is for Apple")
   111  	return "apple", nil
   112  }
   113  
   114  func orderAppleActivity(choice string) error {
   115  	fmt.Printf("Order choice: %v\n", choice)
   116  	return nil
   117  }
   118  
   119  func orderBananaActivity(choice string) error {
   120  	fmt.Printf("Order choice: %v\n", choice)
   121  	return nil
   122  }
   123  
   124  func orderCherryActivity(choice string) error {
   125  	fmt.Printf("Order choice: %v\n", choice)
   126  	return nil
   127  }