go.uber.org/cadence@v1.2.9/test/replaytests/workflows.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  	"context"
    25  	"errors"
    26  	"time"
    27  
    28  	"go.uber.org/zap"
    29  
    30  	"go.uber.org/cadence/activity"
    31  	"go.uber.org/cadence/client"
    32  	"go.uber.org/cadence/workflow"
    33  )
    34  
    35  // Workflow workflow decider
    36  func Workflow(ctx workflow.Context, name string) error {
    37  	ao := workflow.ActivityOptions{
    38  		ScheduleToStartTimeout: time.Minute,
    39  		StartToCloseTimeout:    time.Minute,
    40  		HeartbeatTimeout:       time.Second * 20,
    41  	}
    42  	ctx = workflow.WithActivityOptions(ctx, ao)
    43  
    44  	logger := workflow.GetLogger(ctx)
    45  	logger.Info("helloworld workflow started")
    46  	var helloworldResult string
    47  	v := workflow.GetVersion(ctx, "test-change", workflow.DefaultVersion, 1)
    48  	if v == workflow.DefaultVersion {
    49  		return errors.New("no default-version history")
    50  	} else {
    51  		err := workflow.ExecuteActivity(ctx, helloworldActivity, name).Get(ctx, &helloworldResult)
    52  		if err != nil {
    53  			logger.Error("First activity failed.", zap.Error(err))
    54  			return err
    55  		}
    56  		err = workflow.ExecuteActivity(ctx, helloworldActivity, name).Get(ctx, &helloworldResult)
    57  		if err != nil {
    58  			logger.Error("Second activity failed.", zap.Error(err))
    59  			return err
    60  		}
    61  	}
    62  
    63  	err := workflow.ExecuteActivity(ctx, helloworldActivity, name).Get(ctx, &helloworldResult)
    64  	if err != nil {
    65  		logger.Error("Third activity failed.", zap.Error(err))
    66  		return err
    67  	}
    68  
    69  	logger.Info("Workflow completed.", zap.String("Result", helloworldResult))
    70  
    71  	return nil
    72  }
    73  
    74  // Workflow2 workflow decider
    75  func Workflow2(ctx workflow.Context, name string) error {
    76  	ao := workflow.ActivityOptions{
    77  		ScheduleToStartTimeout: time.Minute,
    78  		StartToCloseTimeout:    time.Minute,
    79  		HeartbeatTimeout:       time.Second * 20,
    80  	}
    81  	ctx = workflow.WithActivityOptions(ctx, ao)
    82  
    83  	logger := workflow.GetLogger(ctx)
    84  	logger.Info("helloworld workflow started")
    85  	var helloworldResult string
    86  
    87  	workflow.GetVersion(ctx, "test-change", workflow.DefaultVersion, 1)
    88  
    89  	err := workflow.UpsertSearchAttributes(ctx, map[string]interface{}{"CustomKeywordField": "testkey"})
    90  	if err != nil {
    91  		logger.Error("upsert failed", zap.Error(err))
    92  		return err
    93  	}
    94  
    95  	err = workflow.ExecuteActivity(ctx, helloworldActivity, name).Get(ctx, &helloworldResult)
    96  	if err != nil {
    97  		logger.Error("Activity failed.", zap.Error(err))
    98  		return err
    99  	}
   100  
   101  	logger.Info("Workflow completed.", zap.String("Result", helloworldResult))
   102  
   103  	return nil
   104  }
   105  
   106  func helloworldActivity(ctx context.Context, name string) (string, error) {
   107  	logger := activity.GetLogger(ctx)
   108  	logger.Info("helloworld activity started")
   109  	return "Hello " + name + "!", nil
   110  }
   111  
   112  func childWorkflowBug(ctx workflow.Context) error {
   113  	ctx = workflow.WithChildOptions(ctx, workflow.ChildWorkflowOptions{
   114  		ExecutionStartToCloseTimeout: 30 * time.Second,
   115  		TaskStartToCloseTimeout:      30 * time.Second,
   116  		WorkflowIDReusePolicy:        client.WorkflowIDReusePolicyTerminateIfRunning, // not relevant, just convenient
   117  		Bugports: workflow.Bugports{
   118  			// child_bug.json records a history against this workflow where the child workflow IS executed,
   119  			// despite the canceled context.
   120  			//
   121  			// this bug has been fixed, and this flag un-does the fix.
   122  			// if the flag is removed, just delete this test + that history.
   123  			StartChildWorkflowsOnCanceledContext: true,
   124  		},
   125  	})
   126  	ctx, cancel := workflow.WithCancel(ctx)
   127  	cancel()
   128  	return workflow.ExecuteChildWorkflow(ctx, "child").Get(ctx, nil)
   129  }
   130  
   131  func childWorkflow(ctx workflow.Context) error {
   132  	_ = workflow.Sleep(ctx, 10*time.Second)
   133  	return nil
   134  }