Skip to content

Commit

Permalink
Update executor support error log instead of fail only
Browse files Browse the repository at this point in the history
  • Loading branch information
tengattack committed Mar 7, 2019
1 parent 399ab1c commit 896118c
Show file tree
Hide file tree
Showing 9 changed files with 134 additions and 70 deletions.
12 changes: 11 additions & 1 deletion builtin/bins/dkron-executor-http/http.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,17 @@ type HTTP struct {
// "expectBody": "", // Expect response body, support regexp, such as /success/
// "debug": "true" // Debug option, will log everything when this option is not empty
// }
func (s *HTTP) Execute(args *dkron.ExecuteRequest) ([]byte, error) {
func (s *HTTP) Execute(args *dkron.ExecuteRequest) (*dkron.ExecuteResponse, error) {
out, err := s.ExecuteImpl(args)
resp := &dkron.ExecuteResponse{Output: out}
if err != nil {
resp.Error = err.Error()
}
return resp, nil
}

// ExecuteImpl do http request
func (s *HTTP) ExecuteImpl(args *dkron.ExecuteRequest) ([]byte, error) {
output, _ := circbuf.NewBuffer(maxBufSize)
var debug bool
if args.Config["debug"] != "" {
Expand Down
4 changes: 2 additions & 2 deletions builtin/bins/dkron-executor-http/http_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ func TestExecute(t *testing.T) {
}
http := &HTTP{}
output, err := http.Execute(pa)
fmt.Println(string(output))
fmt.Println(string(output.Output))
fmt.Println(err)
if err != nil {
t.Fatal(err)
Expand All @@ -41,7 +41,7 @@ func TestExecutePost(t *testing.T) {
}
http := &HTTP{}
output, err := http.Execute(pa)
fmt.Println(string(output))
fmt.Println(string(output.Output))
fmt.Println(err)
if err != nil {
t.Fatal(err)
Expand Down
18 changes: 12 additions & 6 deletions builtin/bins/dkron-executor-shell/shell.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,17 @@ const (
type Shell struct{}

// Execute method of the plugin
func (s *Shell) Execute(args *dkron.ExecuteRequest) ([]byte, error) {
func (s *Shell) Execute(args *dkron.ExecuteRequest) (*dkron.ExecuteResponse, error) {
out, err := s.ExecuteImpl(args)
resp := &dkron.ExecuteResponse{Output: out}
if err != nil {
resp.Error = err.Error()
}
return resp, nil
}

// ExecuteImpl do execute command
func (s *Shell) ExecuteImpl(args *dkron.ExecuteRequest) ([]byte, error) {
output, _ := circbuf.NewBuffer(maxBufSize)

shell, err := strconv.ParseBool(args.Config["shell"])
Expand Down Expand Up @@ -87,11 +97,7 @@ func (s *Shell) Execute(args *dkron.ExecuteRequest) ([]byte, error) {
// Always log output
log.Printf("shell: Command output %s", output)

if err != nil {
return nil, err
}

return output.Bytes(), nil
return output.Bytes(), err
}

// Determine the shell invocation based on OS
Expand Down
2 changes: 1 addition & 1 deletion dkron/executor.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ package dkron

// Executor is the interface that we're exposing as a plugin.
type Executor interface {
Execute(args *ExecuteRequest) ([]byte, error)
Execute(args *ExecuteRequest) (*ExecuteResponse, error)
}

// ExecutorPluginConfig is the plugin config
Expand Down
145 changes: 95 additions & 50 deletions dkron/executor.pb.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions dkron/grpc.go
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,7 @@ func (grpcs *GRPCServer) ExecutionDone(ctx context.Context, execDoneReq *proto.E
log.WithFields(logrus.Fields{
"group": execDoneReq.Group,
"job": execDoneReq.JobName,
"from": execDoneReq.NodeName,
}).Debug("grpc: Received execution done")

var execution Execution
Expand Down
10 changes: 8 additions & 2 deletions dkron/invoke.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,15 +38,21 @@ func (a *Agent) invokeJob(job *Job, execution *Execution) error {
JobName: job.Name,
Config: exc,
})

if err == nil && out.Error != "" {
err = errors.New(out.Error)
}
if err != nil {
log.WithError(err).WithField("job", job.Name).WithField("plugin", executor).Error("invoke: command error output")
success = false
output.Write([]byte(err.Error()))
output.Write([]byte(err.Error() + "\n"))
} else {
success = true
}

output.Write(out)
if out != nil {
output.Write(out.Output)
}
} else {
log.WithField("executor", jex).Error("invoke: Specified executor is not present")
}
Expand Down
11 changes: 3 additions & 8 deletions plugin/executor.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,13 +32,9 @@ type ExecutorClient struct {
client dkron.ExecutorClient
}

func (m *ExecutorClient) Execute(args *dkron.ExecuteRequest) ([]byte, error) {
func (m *ExecutorClient) Execute(args *dkron.ExecuteRequest) (*dkron.ExecuteResponse, error) {
// This is where the magic conversion to Proto happens
out, err := m.client.Execute(context.Background(), args)
if err != nil {
return nil, err
}
return out.Output, nil
return m.client.Execute(context.Background(), args)
}

// Here is the gRPC server that GRPCClient talks to.
Expand All @@ -49,6 +45,5 @@ type ExecutorServer struct {

// Execute is where the magic happens
func (m ExecutorServer) Execute(ctx context.Context, req *dkron.ExecuteRequest) (*dkron.ExecuteResponse, error) {
out, err := m.Impl.Execute(req)
return &dkron.ExecuteResponse{Output: out}, err
return m.Impl.Execute(req)
}
1 change: 1 addition & 0 deletions proto/executor.proto
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ message ExecuteRequest {

message ExecuteResponse {
bytes output = 1;
string error = 2;
}

service Executor {
Expand Down

0 comments on commit 896118c

Please sign in to comment.