github.com/iron-io/functions@v0.0.0-20180820112432-d59d7d1c40b2/api/server/runner_listeners.go (about)

     1  package server
     2  
     3  import (
     4  	"context"
     5  	"github.com/iron-io/functions/api/models"
     6  )
     7  
     8  type RunnerListener interface {
     9  	// BeforeDispatch called before a function run
    10  	BeforeDispatch(ctx context.Context, route *models.Route) error
    11  	// AfterDispatch called after a function run
    12  	AfterDispatch(ctx context.Context, route *models.Route) error
    13  }
    14  
    15  // AddRunListeners adds a listener that will be fired before and after a function run.
    16  func (s *Server) AddRunnerListener(listener RunnerListener) {
    17  	s.runnerListeners = append(s.runnerListeners, listener)
    18  }
    19  
    20  func (s *Server) FireBeforeDispatch(ctx context.Context, route *models.Route) error {
    21  	for _, l := range s.runnerListeners {
    22  		err := l.BeforeDispatch(ctx, route)
    23  		if err != nil {
    24  			return err
    25  		}
    26  	}
    27  	return nil
    28  }
    29  
    30  func (s *Server) FireAfterDispatch(ctx context.Context, route *models.Route) error {
    31  	for _, l := range s.runnerListeners {
    32  		err := l.AfterDispatch(ctx, route)
    33  		if err != nil {
    34  			return err
    35  		}
    36  	}
    37  	return nil
    38  }