mirror of
https://github.com/fnproject/fn.git
synced 2022-10-28 21:29:17 +03:00
First draft of modifying RunnerListener to CallListener to get it closer to the action (and named better).
This commit is contained in:
@@ -15,6 +15,7 @@ import (
|
||||
"github.com/fnproject/fn/api/agent/drivers/docker"
|
||||
"github.com/fnproject/fn/api/agent/protocol"
|
||||
"github.com/fnproject/fn/api/common"
|
||||
"github.com/fnproject/fn/api/extenders"
|
||||
"github.com/fnproject/fn/api/id"
|
||||
"github.com/fnproject/fn/api/models"
|
||||
"github.com/opentracing/opentracing-go"
|
||||
@@ -110,12 +111,14 @@ type Agent interface {
|
||||
|
||||
// Return the http.Handler used to handle Prometheus metric requests
|
||||
PromHandler() http.Handler
|
||||
AddCallListener(extenders.CallListener)
|
||||
}
|
||||
|
||||
type agent struct {
|
||||
// TODO maybe these should be on GetCall? idk. was getting bloated.
|
||||
mq models.MessageQueue
|
||||
ds models.Datastore
|
||||
mq models.MessageQueue
|
||||
ds models.Datastore
|
||||
callListeners []extenders.CallListener
|
||||
|
||||
driver drivers.Driver
|
||||
|
||||
@@ -204,6 +207,11 @@ func (a *agent) Submit(callI Call) error {
|
||||
// to make this remove the container asynchronously?
|
||||
defer slot.Close() // notify our slot is free once we're done
|
||||
|
||||
err = a.fireBeforeCall(ctx, call.Model())
|
||||
if err != nil {
|
||||
return fmt.Errorf("BeforeCall: %v", err)
|
||||
}
|
||||
|
||||
// TODO Start is checking the timer now, we could do it here, too.
|
||||
err = call.Start(ctx)
|
||||
if err != nil {
|
||||
@@ -231,6 +239,11 @@ func (a *agent) Submit(callI Call) error {
|
||||
ctx = opentracing.ContextWithSpan(context.Background(), span)
|
||||
call.End(ctx, err)
|
||||
|
||||
err = a.fireAfterCall(ctx, call.Model())
|
||||
if err != nil {
|
||||
return fmt.Errorf("AfterCall: %v", err)
|
||||
}
|
||||
|
||||
return err
|
||||
}
|
||||
|
||||
|
||||
@@ -331,7 +331,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?
|
||||
// XXX (reed): should we append the error to logs? Error field? (TR) think so, otherwise it's lost looks like?
|
||||
c.Status = "error"
|
||||
}
|
||||
|
||||
|
||||
32
api/agent/listeners.go
Normal file
32
api/agent/listeners.go
Normal file
@@ -0,0 +1,32 @@
|
||||
package agent
|
||||
|
||||
import (
|
||||
"context"
|
||||
|
||||
"github.com/fnproject/fn/api/extenders"
|
||||
"github.com/fnproject/fn/api/models"
|
||||
)
|
||||
|
||||
func (a *agent) AddCallListener(listener extenders.CallListener) {
|
||||
a.callListeners = append(a.callListeners, listener)
|
||||
}
|
||||
|
||||
func (a *agent) fireBeforeCall(ctx context.Context, call *models.Call) error {
|
||||
for _, l := range a.callListeners {
|
||||
err := l.BeforeCall(ctx, call)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func (a *agent) fireAfterCall(ctx context.Context, call *models.Call) error {
|
||||
for _, l := range a.callListeners {
|
||||
err := l.AfterCall(ctx, call)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
Reference in New Issue
Block a user