Pushed triggers into start() and end()

This commit is contained in:
Travis Reeder
2017-10-25 14:11:27 +02:00
parent fb386b2864
commit de04562b8e
7 changed files with 39 additions and 30 deletions

View File

@@ -29,13 +29,13 @@ type Call interface {
// etc.
// TODO Start and End can likely be unexported as they are only used in the agent,
// and on a type which is constructed in a specific agent. meh.
Start(ctx context.Context) error
Start(ctx context.Context, t callTrigger) error
// End will be called immediately after attempting a call execution,
// regardless of whether the execution failed or not. An error will be passed
// to End, which if nil indicates a successful execution. Any error returned
// from End will be returned as the error from Submit.
End(ctx context.Context, err error)
End(ctx context.Context, err error, t callTrigger) error
}
// TODO build w/o closures... lazy
@@ -278,7 +278,7 @@ type call struct {
func (c *call) Model() *models.Call { return c.Call }
func (c *call) Start(ctx context.Context) error {
func (c *call) Start(ctx context.Context, t callTrigger) error {
span, ctx := opentracing.StartSpanFromContext(ctx, "agent_call_start")
defer span.Finish()
@@ -316,10 +316,16 @@ func (c *call) Start(ctx context.Context) error {
return err // let another thread try this
}
}
err := t.fireBeforeCall(ctx, c.Model())
if err != nil {
return fmt.Errorf("BeforeCall: %v", err)
}
return nil
}
func (c *call) End(ctx context.Context, err error) {
func (c *call) End(ctx context.Context, err error, t callTrigger) error {
span, ctx := opentracing.StartSpanFromContext(ctx, "agent_call_end")
defer span.Finish()
@@ -331,7 +337,7 @@ func (c *call) End(ctx context.Context, err error) {
case context.DeadlineExceeded:
c.Status = "timeout"
default:
// XXX (reed): should we append the error to logs? Error field? (TR) think so, otherwise it's lost looks like?
// XXX (reed): should we append the error to logs? Error field? (TR) yes, think so, otherwise it's lost looks like?
c.Status = "error"
}
@@ -344,14 +350,23 @@ func (c *call) End(ctx context.Context, err error) {
// TODO: this should be update, really
if err := c.ds.InsertCall(ctx, c.Call); err != nil {
common.Logger(ctx).WithError(err).Error("error inserting call into datastore")
return err
}
if err := c.ds.InsertLog(ctx, c.AppName, c.ID, c.stderr); err != nil {
common.Logger(ctx).WithError(err).Error("error uploading log")
return err
}
// NOTE call this after InsertLog or the buffer will get reset
c.stderr.Close()
err = t.fireAfterCall(ctx, c.Model())
if err != nil {
return fmt.Errorf("AfterCall: %v", err)
}
return err
}
func fakeHandler(http.ResponseWriter, *http.Request, Params) {}