Skip to content

Commit

Permalink
Add action timeout. Use previously defined default of 1 minute if the…
Browse files Browse the repository at this point in the history
… action timeout is not specified. (elastic#26984)

* Add action timeout. Use previously defined default of 1 minutes if the action timeout is not specified.
  • Loading branch information
aleksmaus authored Jul 21, 2021
1 parent 4da1594 commit 99bc09f
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,19 @@ import (
"fmt"
"time"

"github.com/elastic/beats/v7/x-pack/elastic-agent/pkg/agent/errors"
"github.com/elastic/beats/v7/x-pack/elastic-agent/pkg/agent/storage/store"
"github.com/elastic/beats/v7/x-pack/elastic-agent/pkg/core/logger"
"github.com/elastic/beats/v7/x-pack/elastic-agent/pkg/core/server"
"github.com/elastic/beats/v7/x-pack/elastic-agent/pkg/fleetapi"
)

const defaultActionTimeout = time.Minute
const (
defaultActionTimeout = time.Minute
maxActionTimeout = time.Hour
)

var errActionTimeoutInvalid = errors.New("action timeout is invalid")

// AppAction is a handler for application actions.
type AppAction struct {
Expand Down Expand Up @@ -54,11 +60,25 @@ func (h *AppAction) Handle(ctx context.Context, a fleetapi.Action, acker store.F
}

start := time.Now().UTC()
res, err := appState.PerformAction(action.InputType, params, defaultActionTimeout)
timeout := defaultActionTimeout
if action.Timeout > 0 {
timeout = time.Duration(action.Timeout) * time.Second
if timeout > maxActionTimeout {
h.log.Debugf("handlerAppAction: action '%v' timeout exceeds maximum allowed %v", action.InputType, maxActionTimeout)
err = errActionTimeoutInvalid
}
}

var res map[string]interface{}
if err == nil {
h.log.Debugf("handlerAppAction: action '%v' started with timeout: %v", action.InputType, timeout)
res, err = appState.PerformAction(action.InputType, params, timeout)
}
end := time.Now().UTC()

startFormatted := start.Format(time.RFC3339Nano)
endFormatted := end.Format(time.RFC3339Nano)
h.log.Debugf("handlerAppAction: action '%v' finished, startFormatted: %v, endFormatted: %v, err: %v", action.InputType, startFormatted, endFormatted, err)
if err != nil {
action.StartedAt = startFormatted
action.CompletedAt = endFormatted
Expand Down
2 changes: 2 additions & 0 deletions x-pack/elastic-agent/pkg/fleetapi/action.go
Original file line number Diff line number Diff line change
Expand Up @@ -211,6 +211,7 @@ type ActionApp struct {
ActionID string `json:"id" mapstructure:"id"`
ActionType string `json:"type" mapstructure:"type"`
InputType string `json:"input_type" mapstructure:"input_type"`
Timeout int64 `json:"timeout,omitempty" mapstructure:"timeout,omitempty"`
Data json.RawMessage `json:"data" mapstructure:"data"`
StartedAt string `json:"started_at,omitempty" mapstructure:"started_at,omitempty"`
CompletedAt string `json:"completed_at,omitempty" mapstructure:"completed_at,omitempty"`
Expand Down Expand Up @@ -284,6 +285,7 @@ func (a *Actions) UnmarshalJSON(data []byte) error {
ActionID: response.ActionID,
ActionType: response.ActionType,
InputType: response.InputType,
Timeout: response.Timeout,
Data: response.Data,
}
case ActionTypeUnenroll:
Expand Down

0 comments on commit 99bc09f

Please sign in to comment.