go.uber.org/cadence@v1.2.9/workflow/session.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 workflow
    22  
    23  import (
    24  	"go.uber.org/cadence/internal"
    25  )
    26  
    27  type (
    28  	// SessionInfo contains information of a created session. For now, exported
    29  	// fields are SessionID and HostName.
    30  	// SessionID is a uuid generated when CreateSession() or RecreateSession()
    31  	// is called and can be used to uniquely identify a session.
    32  	// HostName specifies which host is executing the session
    33  	SessionInfo = internal.SessionInfo
    34  
    35  	// SessionOptions specifies metadata for a session.
    36  	// ExecutionTimeout: required, no default
    37  	//     Specifies the maximum amount of time the session can run
    38  	// CreationTimeout: required, no default
    39  	//     Specifies how long session creation can take before returning an error
    40  	// HeartbeatTimeout: optional, default 20s
    41  	//     Specifies the heartbeat timeout. If heartbeat is not received by server
    42  	//     within the timeout, the session will be declared as failed
    43  	SessionOptions = internal.SessionOptions
    44  )
    45  
    46  // ErrSessionFailed is the error returned when user tries to execute an activity but the
    47  // session it belongs to has already failed
    48  var ErrSessionFailed = internal.ErrSessionFailed
    49  
    50  // Note: Worker should be configured to process session. To do this, set the following
    51  // fields in WorkerOptions:
    52  //     EnableSessionWorker: true
    53  //     MaxConcurrentSessionExecutionSize: the maximum number of concurrently sessions the resource
    54  //         support. By default, 1000 is used.
    55  
    56  // CreateSession creates a session and returns a new context which contains information
    57  // of the created session. The session will be created on the tasklist user specified in
    58  // ActivityOptions. If none is specified, the default one will be used.
    59  //
    60  // CreationSession will fail in the following situations:
    61  //  1. The context passed in already contains a session which is still open
    62  //     (not closed and failed).
    63  //  2. All the workers are busy (number of sessions currently running on all the workers have reached
    64  //     MaxConcurrentSessionExecutionSize, which is specified when starting the workers) and session
    65  //     cannot be created within a specified timeout.
    66  //
    67  // If an activity is executed using the returned context, it's regarded as part of the
    68  // session. All activities within the same session will be executed by the same worker.
    69  // User still needs to handle the error returned when executing an activity. Session will
    70  // not be marked as failed if an activity within it returns an error. Only when the worker
    71  // executing the session is down, that session will be marked as failed. Executing an activity
    72  // within a failed session will return ErrSessionFailed immediately without scheduling that activity.
    73  //
    74  // The returned session Context will be cancelled if the session fails (worker died) or CompleteSession()
    75  // is called. This means that in these two cases, all user activities scheduled using the returned session
    76  // Context will also be cancelled.
    77  //
    78  // If user wants to end a session since activity returns some error, use CompleteSession API below.
    79  // New session can be created if necessary to retry the whole session.
    80  //
    81  // Example:
    82  //
    83  //	   so := &SessionOptions{
    84  //		      ExecutionTimeout: time.Minute,
    85  //		      CreationTimeout:  time.Minute,
    86  //	   }
    87  //	   sessionCtx, err := CreateSession(ctx, so)
    88  //	   if err != nil {
    89  //			    // Creation failed. Wrong ctx or too many outstanding sessions.
    90  //	   }
    91  //	   defer CompleteSession(sessionCtx)
    92  //	   err = ExecuteActivity(sessionCtx, someActivityFunc, activityInput).Get(sessionCtx, nil)
    93  //	   if err == ErrSessionFailed {
    94  //	       // Session has failed
    95  //	   } else {
    96  //	       // Handle activity error
    97  //	   }
    98  //	   ... // execute more activities using sessionCtx
    99  func CreateSession(ctx Context, sessionOptions *SessionOptions) (Context, error) {
   100  	return internal.CreateSession(ctx, sessionOptions)
   101  }
   102  
   103  // RecreateSession recreate a session based on the sessionInfo passed in. Activities executed within
   104  // the recreated session will be executed by the same worker as the previous session. RecreateSession()
   105  // returns an error under the same situation as CreateSession() or the token passed in is invalid.
   106  // It also has the same usage as CreateSession().
   107  //
   108  // The main usage of RecreateSession is for long sessions that are splited into multiple runs. At the end of
   109  // one run, complete the current session, get recreateToken from sessionInfo by calling SessionInfo.GetRecreateToken()
   110  // and pass the token to the next run. In the new run, session can be recreated using that token.
   111  func RecreateSession(ctx Context, recreateToken []byte, sessionOptions *SessionOptions) (Context, error) {
   112  	return internal.RecreateSession(ctx, recreateToken, sessionOptions)
   113  }
   114  
   115  // CompleteSession completes a session. It releases worker resources, so other sessions can be created.
   116  // CompleteSession won't do anything if the context passed in doesn't contain any session information or the
   117  // session has already completed or failed.
   118  //
   119  // After a session has completed, user can continue to use the context, but the activities will be scheduled
   120  // on the normal taskList (as user specified in ActivityOptions) and may be picked up by another worker since
   121  // it's not in a session.
   122  func CompleteSession(ctx Context) {
   123  	internal.CompleteSession(ctx)
   124  }
   125  
   126  // GetSessionInfo returns the sessionInfo stored in the context. If there are multiple sessions in the context,
   127  // (for example, the same context is used to create, complete, create another session. Then user found that the
   128  // session has failed, and created a new one on it), the most recent sessionInfo will be returned.
   129  //
   130  // This API will return nil if there's no sessionInfo in the context.
   131  func GetSessionInfo(ctx Context) *SessionInfo {
   132  	return internal.GetSessionInfo(ctx)
   133  }