github.com/TIBCOSoftware/flogo-lib@v0.5.9/engine/runner/direct.go (about)

     1  package runner
     2  
     3  import (
     4  	"context"
     5  	"errors"
     6  
     7  	"github.com/TIBCOSoftware/flogo-lib/core/action"
     8  	"github.com/TIBCOSoftware/flogo-lib/core/data"
     9  )
    10  
    11  // DirectRunner runs an action synchronously
    12  type DirectRunner struct {
    13  }
    14  
    15  // NewDirectRunner create a new DirectRunner
    16  func NewDirect() *DirectRunner {
    17  	return &DirectRunner{}
    18  }
    19  
    20  // Start will start the engine, by starting all of its workers
    21  func (runner *DirectRunner) Start() error {
    22  	//op-op
    23  	return nil
    24  }
    25  
    26  // Stop will stop the engine, by stopping all of its workers
    27  func (runner *DirectRunner) Stop() error {
    28  	//no-op
    29  	return nil
    30  }
    31  
    32  // Deprecated: Use Execute() instead
    33  func (runner *DirectRunner) Run(ctx context.Context, act action.Action, uri string, options interface{}) (code int, data interface{}, err error) {
    34  
    35  	return 0, nil, errors.New("unsupported")
    36  }
    37  
    38  // Deprecated: Use Execute() instead
    39  func (runner *DirectRunner) RunAction(ctx context.Context, act action.Action, options map[string]interface{}) (results map[string]*data.Attribute, err error) {
    40  
    41  	return nil, errors.New("unsupported")
    42  }
    43  
    44  // Execute implements action.Runner.Execute
    45  func (runner *DirectRunner) Execute(ctx context.Context, act action.Action, inputs map[string]*data.Attribute) (results map[string]*data.Attribute, err error) {
    46  
    47  	if act == nil {
    48  		return nil, errors.New("Action not specified")
    49  	}
    50  
    51  	md := action.GetMetadata(act)
    52  
    53  	if !md.Async {
    54  		syncAct := act.(action.SyncAction)
    55  		return syncAct.Run(ctx, inputs)
    56  	} else {
    57  		asyncAct := act.(action.AsyncAction)
    58  
    59  		handler := &SyncResultHandler{done: make(chan bool, 1)}
    60  
    61  		err = asyncAct.Run(ctx, inputs, handler)
    62  
    63  		if err != nil {
    64  			return nil, err
    65  		}
    66  
    67  		<-handler.done
    68  
    69  		return handler.Result()
    70  	}
    71  
    72  }
    73  
    74  // SyncResultHandler simple result handler to use in synchronous case
    75  type SyncResultHandler struct {
    76  	done       chan bool
    77  	resultData map[string]*data.Attribute
    78  	err        error
    79  	set        bool
    80  }
    81  
    82  // HandleResult implements action.ResultHandler.HandleResult
    83  func (rh *SyncResultHandler) HandleResult(resultData map[string]*data.Attribute, err error) {
    84  
    85  	if !rh.set {
    86  		rh.set = true
    87  		rh.resultData = resultData
    88  		rh.err = err
    89  	}
    90  }
    91  
    92  // Done implements action.ResultHandler.Done
    93  func (rh *SyncResultHandler) Done() {
    94  	rh.done <- true
    95  }
    96  
    97  // Result returns the latest Result set on the handler
    98  func (rh *SyncResultHandler) Result() (resultData map[string]*data.Attribute, err error) {
    99  	return rh.resultData, rh.err
   100  }