feat(actions): Add basic functionality for actions and secrets (with proper asynchronous enqueue and dequeue mechanism)
Some checks failed
/ build (pull_request) Failing after 3m25s

This commit is contained in:
Varakh 2024-04-21 23:09:25 +02:00
parent 82fa877b6f
commit 9d64e66125
10 changed files with 113 additions and 32 deletions

View file

@ -10,11 +10,9 @@ Changes adhere to [semantic versioning](https://semver.org).
* Switched to encrypting webhook tokens in database * Switched to encrypting webhook tokens in database
* Added _Actions_, a simple way to trigger notifications via [shoutrrr](https://containrrr.dev/shoutrrr) which supports secrets * Added _Actions_, a simple way to trigger notifications via [shoutrrr](https://containrrr.dev/shoutrrr) which supports secrets
* Switched to producing events only for _Updates_ * Switched to producing events only for _Updates_
## [1.1.0] - UNRELEASED
* Adapted logging which defaults to JSON encoding * Adapted logging which defaults to JSON encoding
* ... * Updated dependencies
* Updated build to use Go 1.22
## [1.0.3] - 2024/01/21 ## [1.0.3] - 2024/01/21

View file

@ -412,7 +412,7 @@ paths:
required: false required: false
schema: schema:
type: string type: string
default: desc default: asc
enum: enum:
- asc - asc
- desc - desc
@ -422,7 +422,7 @@ paths:
required: false required: false
schema: schema:
type: string type: string
default: updated_at default: label
enum: enum:
- id - id
- label - label
@ -1008,7 +1008,7 @@ paths:
required: false required: false
schema: schema:
type: string type: string
default: desc default: asc
enum: enum:
- asc - asc
- desc - desc
@ -1018,7 +1018,7 @@ paths:
required: false required: false
schema: schema:
type: string type: string
default: updated_at default: label
enum: enum:
- id - id
- label - label
@ -1675,7 +1675,7 @@ paths:
required: false required: false
schema: schema:
type: string type: string
default: updated_at default: created_at
enum: enum:
- id - id
- state - state
@ -2497,8 +2497,12 @@ components:
enum: enum:
- created - created
- running - running
- retrying
- error - error
- success - success
message:
type: string
nullable: true
actionId: actionId:
type: string type: string
eventId: eventId:

View file

@ -95,6 +95,7 @@ type ActionInvocationState string
const ( const (
ActionInvocationStateCreated ActionInvocationState = "created" ActionInvocationStateCreated ActionInvocationState = "created"
ActionInvocationStateRunning ActionInvocationState = "running" ActionInvocationStateRunning ActionInvocationState = "running"
ActionInvocationStateRetrying ActionInvocationState = "retrying"
ActionInvocationStateSuccess ActionInvocationState = "success" ActionInvocationStateSuccess ActionInvocationState = "success"
ActionInvocationStateError ActionInvocationState = "error" ActionInvocationStateError ActionInvocationState = "error"
) )

View file

@ -91,22 +91,22 @@ type PaginateUpdateRequest struct {
type PaginateWebhookRequest struct { type PaginateWebhookRequest struct {
PageSize int `form:"pageSize,default=5" binding:"numeric,gte=1"` PageSize int `form:"pageSize,default=5" binding:"numeric,gte=1"`
Page int `form:"page,default=1" binding:"numeric,gte=1"` Page int `form:"page,default=1" binding:"numeric,gte=1"`
Order string `form:"order,default=desc" binding:"oneof=asc desc"` Order string `form:"order,default=asc" binding:"oneof=asc desc"`
OrderBy string `form:"orderBy,default=updated_at" binding:"oneof=id label type created_at updated_at"` OrderBy string `form:"orderBy,default=label" binding:"oneof=id label type created_at updated_at"`
} }
type PaginateActionRequest struct { type PaginateActionRequest struct {
PageSize int `form:"pageSize,default=5" binding:"numeric,gte=1"` PageSize int `form:"pageSize,default=5" binding:"numeric,gte=1"`
Page int `form:"page,default=1" binding:"numeric,gte=1"` Page int `form:"page,default=1" binding:"numeric,gte=1"`
Order string `form:"order,default=desc" binding:"oneof=asc desc"` Order string `form:"order,default=asc" binding:"oneof=asc desc"`
OrderBy string `form:"orderBy,default=updated_at" binding:"oneof=id label type created_at updated_at"` OrderBy string `form:"orderBy,default=label" binding:"oneof=id label type created_at updated_at"`
} }
type PaginateActionInvocationRequest struct { type PaginateActionInvocationRequest struct {
PageSize int `form:"pageSize,default=5" binding:"numeric,gte=1"` PageSize int `form:"pageSize,default=5" binding:"numeric,gte=1"`
Page int `form:"page,default=1" binding:"numeric,gte=1"` Page int `form:"page,default=1" binding:"numeric,gte=1"`
Order string `form:"order,default=desc" binding:"oneof=asc desc"` Order string `form:"order,default=desc" binding:"oneof=asc desc"`
OrderBy string `form:"orderBy,default=updated_at" binding:"oneof=id state retry_count created_at updated_at"` OrderBy string `form:"orderBy,default=created_at" binding:"oneof=id state retry_count created_at updated_at"`
} }
type WebhookGenericRequest struct { type WebhookGenericRequest struct {
@ -390,10 +390,10 @@ type ActionResponse struct {
ID uuid.UUID `json:"id"` ID uuid.UUID `json:"id"`
Label string `json:"label"` Label string `json:"label"`
Type string `json:"type"` Type string `json:"type"`
MatchEvent *string `json:"matchEvent"` MatchEvent *string `json:"matchEvent,omitempty"`
MatchHost *string `json:"matchHost"` MatchHost *string `json:"matchHost,omitempty"`
MatchApplication *string `json:"matchApplication"` MatchApplication *string `json:"matchApplication,omitempty"`
MatchProvider *string `json:"matchProvider"` MatchProvider *string `json:"matchProvider,omitempty"`
Payload interface{} `json:"payload,omitempty"` Payload interface{} `json:"payload,omitempty"`
CreatedAt time.Time `json:"createdAt"` CreatedAt time.Time `json:"createdAt"`
UpdatedAt time.Time `json:"updatedAt"` UpdatedAt time.Time `json:"updatedAt"`
@ -460,6 +460,7 @@ type ActionInvocationResponse struct {
ID uuid.UUID `json:"id"` ID uuid.UUID `json:"id"`
RetryCount int `json:"retryCount"` RetryCount int `json:"retryCount"`
State string `json:"state"` State string `json:"state"`
Message *string `json:"message,omitempty"`
ActionID string `json:"actionId"` ActionID string `json:"actionId"`
EventID string `json:"eventId"` EventID string `json:"eventId"`
CreatedAt time.Time `json:"createdAt"` CreatedAt time.Time `json:"createdAt"`
@ -470,11 +471,12 @@ type ActionInvocationSingleResponse struct {
Data ActionInvocationResponse `json:"data"` Data ActionInvocationResponse `json:"data"`
} }
func NewActionInvocationSingleResponse(id uuid.UUID, retryCount int, state string, actionId string, eventId string, createdAt time.Time, updatedAt time.Time) *ActionInvocationSingleResponse { func NewActionInvocationSingleResponse(id uuid.UUID, retryCount int, state string, message *string, actionId string, eventId string, createdAt time.Time, updatedAt time.Time) *ActionInvocationSingleResponse {
e := new(ActionInvocationSingleResponse) e := new(ActionInvocationSingleResponse)
e.Data.ID = id e.Data.ID = id
e.Data.RetryCount = retryCount e.Data.RetryCount = retryCount
e.Data.State = state e.Data.State = state
e.Data.Message = message
e.Data.ActionID = actionId e.Data.ActionID = actionId
e.Data.EventID = eventId e.Data.EventID = eventId
e.Data.CreatedAt = createdAt e.Data.CreatedAt = createdAt

2
go.mod
View file

@ -1,6 +1,6 @@
module git.myservermanager.com/varakh/upda module git.myservermanager.com/varakh/upda
go 1.21 go 1.22
require ( require (
github.com/Depado/ginprom v1.8.1 github.com/Depado/ginprom v1.8.1

View file

@ -63,6 +63,7 @@ func (h *actionInvocationHandler) paginate(c *gin.Context) {
ID: e.ID, ID: e.ID,
RetryCount: e.RetryCount, RetryCount: e.RetryCount,
State: e.State, State: e.State,
Message: e.Message,
ActionID: e.ActionID, ActionID: e.ActionID,
EventID: e.EventID, EventID: e.EventID,
CreatedAt: e.CreatedAt, CreatedAt: e.CreatedAt,
@ -87,7 +88,7 @@ func (h *actionInvocationHandler) get(c *gin.Context) {
return return
} }
c.JSON(http.StatusOK, api.NewActionInvocationSingleResponse(e.ID, e.RetryCount, e.State, e.ActionID, e.EventID, e.CreatedAt, e.UpdatedAt)) c.JSON(http.StatusOK, api.NewActionInvocationSingleResponse(e.ID, e.RetryCount, e.State, e.Message, e.ActionID, e.EventID, e.CreatedAt, e.UpdatedAt))
} }
func (h *actionInvocationHandler) delete(c *gin.Context) { func (h *actionInvocationHandler) delete(c *gin.Context) {

View file

@ -167,6 +167,7 @@ type ActionInvocation struct {
ID uuid.UUID `gorm:"type:uuid;primary_key;unique;not null"` ID uuid.UUID `gorm:"type:uuid;primary_key;unique;not null"`
RetryCount int `gorm:"not null;default:1"` RetryCount int `gorm:"not null;default:1"`
State string `gorm:"not null"` State string `gorm:"not null"`
Message *string
Event Event `gorm:"foreignKey:EventID;references:ID;constraint:OnUpdate:CASCADE,OnDelete:CASCADE;"` Event Event `gorm:"foreignKey:EventID;references:ID;constraint:OnUpdate:CASCADE,OnDelete:CASCADE;"`
EventID string `gorm:"not null"` EventID string `gorm:"not null"`
Action Action `gorm:"foreignKey:ActionID;references:ID;constraint:OnUpdate:CASCADE,OnDelete:CASCADE;"` Action Action `gorm:"foreignKey:ActionID;references:ID;constraint:OnUpdate:CASCADE,OnDelete:CASCADE;"`

View file

@ -13,6 +13,7 @@ type ActionInvocationRepository interface {
findAllByState(limit int, maxRetries int, state ...api.ActionInvocationState) ([]*ActionInvocation, error) findAllByState(limit int, maxRetries int, state ...api.ActionInvocationState) ([]*ActionInvocation, error)
create(eventId string, actionId string, state api.ActionInvocationState) (*ActionInvocation, error) create(eventId string, actionId string, state api.ActionInvocationState) (*ActionInvocation, error)
updateState(id string, state api.ActionInvocationState) (*ActionInvocation, error) updateState(id string, state api.ActionInvocationState) (*ActionInvocation, error)
updateMessage(id string, message *string) (*ActionInvocation, error)
updateRetryCount(id string, retryCount int) (*ActionInvocation, error) updateRetryCount(id string, retryCount int) (*ActionInvocation, error)
delete(id string) (int64, error) delete(id string) (int64, error)
deleteByUpdatedAtBeforeAndStates(time time.Time, retryCount int, state ...api.ActionInvocationState) (int64, error) deleteByUpdatedAtBeforeAndStates(time time.Time, retryCount int, state ...api.ActionInvocationState) (int64, error)
@ -117,6 +118,31 @@ func (r *actionInvocationDbRepo) updateState(id string, state api.ActionInvocati
return e, nil return e, nil
} }
func (r *actionInvocationDbRepo) updateMessage(id string, message *string) (*ActionInvocation, error) {
if id == "" {
return nil, errorValidationNotBlank
}
var err error
var e *ActionInvocation
if e, err = r.find(id); err != nil {
return nil, err
}
e.Message = message
var res *gorm.DB
if res = r.db.Save(&e); res.Error != nil {
return nil, res.Error
}
if res.RowsAffected == 0 {
return e, errorDatabaseRowsExpected
}
return e, nil
}
func (r *actionInvocationDbRepo) delete(id string) (int64, error) { func (r *actionInvocationDbRepo) delete(id string) (int64, error) {
if id == "" { if id == "" {
return 0, errorValidationNotBlank return 0, errorValidationNotBlank

View file

@ -79,7 +79,6 @@ func (s *actionInvocationService) enqueueFromEvent(event *Event, actions []*Acti
if len(filteredActions) == 0 { if len(filteredActions) == 0 {
zap.L().Sugar().Debugf("No actions found which match event '%s', nothing to enqueue", event.Name) zap.L().Sugar().Debugf("No actions found which match event '%s', nothing to enqueue", event.Name)
return nil
} }
for _, action := range filteredActions { for _, action := range filteredActions {
@ -108,7 +107,7 @@ func (s *actionInvocationService) invoke(batchSize int, maxRetries int) error {
var err error var err error
var actionInvocations []*ActionInvocation var actionInvocations []*ActionInvocation
if actionInvocations, err = s.getByState(batchSize, maxRetries, api.ActionInvocationStateCreated, api.ActionInvocationStateError); err != nil { if actionInvocations, err = s.getByState(batchSize, maxRetries, api.ActionInvocationStateCreated, api.ActionInvocationStateRetrying); err != nil {
return err return err
} }
@ -147,20 +146,31 @@ func (s *actionInvocationService) invoke(batchSize int, maxRetries int) error {
} }
if err = s.execute(action, eventPayload); err != nil { if err = s.execute(action, eventPayload); err != nil {
var cause error
cause = err
zap.L().Sugar().Errorf("Could not invoke action '%s' (%v) for action invocation '%v'. Reason: %s", action.Label, action.ID, actionInvocation.ID, err.Error()) zap.L().Sugar().Errorf("Could not invoke action '%s' (%v) for action invocation '%v'. Reason: %s", action.Label, action.ID, actionInvocation.ID, err.Error())
if _, err = s.updateState(actionInvocation.ID.String(), api.ActionInvocationStateError); err != nil { var newState api.ActionInvocationState
zap.L().Sugar().Errorf("Could not mark action invocation '%v' as error. Reason: %s", actionInvocation.ID, err.Error()) newRetryCount := actionInvocation.RetryCount + 1
newState = api.ActionInvocationStateRetrying
if newRetryCount >= maxRetries {
zap.L().Sugar().Infof("Action invocation '%v' exceeded max retry count of '%d'. Not trying again.", actionInvocation.ID, newRetryCount)
newState = api.ActionInvocationStateError
} }
newRetryCount := actionInvocation.RetryCount + 1 if _, err = s.updateState(actionInvocation.ID.String(), newState); err != nil {
zap.L().Sugar().Errorf("Could not mark action invocation '%v' as '%v'. Reason: %s", actionInvocation.ID, newState, err.Error())
}
if _, err = s.updateRetryCount(actionInvocation.ID.String(), newRetryCount); err != nil { if _, err = s.updateRetryCount(actionInvocation.ID.String(), newRetryCount); err != nil {
zap.L().Sugar().Errorf("Could not update action invocation '%v' retry count to '%d'. Reason: %s", actionInvocation.ID, newRetryCount, err.Error()) zap.L().Sugar().Errorf("Could not update action invocation '%v' retry count to '%d'. Reason: %s", actionInvocation.ID, newRetryCount, err.Error())
} }
if newRetryCount >= maxRetries { msg := cause.Error()
zap.L().Sugar().Infof("Action invocation '%v' exceeded max retry count of '%d'. Not trying again.", actionInvocation.ID, newRetryCount) if _, err = s.updateMessage(actionInvocation.ID.String(), &msg); err != nil {
zap.L().Sugar().Errorf("Could not update action invocation '%v' message. Reason: %s", actionInvocation.ID, err.Error())
} }
continue continue
@ -170,6 +180,9 @@ func (s *actionInvocationService) invoke(batchSize int, maxRetries int) error {
if _, err = s.updateState(actionInvocation.ID.String(), api.ActionInvocationStateSuccess); err != nil { if _, err = s.updateState(actionInvocation.ID.String(), api.ActionInvocationStateSuccess); err != nil {
zap.L().Sugar().Errorf("Could not mark action invocation '%v' as success. Reason: %s", actionInvocation.ID, err.Error()) zap.L().Sugar().Errorf("Could not mark action invocation '%v' as success. Reason: %s", actionInvocation.ID, err.Error())
} }
if _, err = s.updateMessage(actionInvocation.ID.String(), nil); err != nil {
zap.L().Sugar().Errorf("Could not update action invocation '%v' message. Reason: %s", actionInvocation.ID, err.Error())
}
} }
return nil return nil
@ -322,6 +335,26 @@ func (s *actionInvocationService) updateState(id string, state api.ActionInvocat
return e, nil return e, nil
} }
func (s *actionInvocationService) updateMessage(id string, message *string) (*ActionInvocation, error) {
if id == "" {
return nil, errorValidationNotBlank
}
var e *ActionInvocation
var err error
if e, err = s.get(id); err != nil {
return nil, err
}
if e, err = s.repo.updateMessage(id, message); err != nil {
return nil, err
}
zap.L().Sugar().Infof("Modified action invocation '%v'", id)
return e, nil
}
func (s *actionInvocationService) updateRetryCount(id string, retryCount int) (*ActionInvocation, error) { func (s *actionInvocationService) updateRetryCount(id string, retryCount int) (*ActionInvocation, error) {
if id == "" { if id == "" {
return nil, errorValidationNotBlank return nil, errorValidationNotBlank

View file

@ -218,8 +218,23 @@ func (s *eventService) extractPayloadInfo(event *Event) (*eventPayloadInformatio
} }
return &eventPayloadInformationDto{Host: p.Host, Application: p.Application, Provider: p.Provider, Version: p.Version}, nil return &eventPayloadInformationDto{Host: p.Host, Application: p.Application, Provider: p.Provider, Version: p.Version}, nil
case api.EventNameUpdateUpdatedApproved.Value(): case api.EventNameUpdateUpdatedApproved.Value():
var p api.EventPayloadUpdateUpdatedDto
if p, err = util.UnmarshalGenericJSON[api.EventPayloadUpdateUpdatedDto](bytes); err != nil {
return nil, newServiceError(General, err)
}
return &eventPayloadInformationDto{Host: p.Host, Application: p.Application, Provider: p.Provider, Version: p.Version}, nil
case api.EventNameUpdateUpdatedPending.Value(): case api.EventNameUpdateUpdatedPending.Value():
var p api.EventPayloadUpdateUpdatedDto
if p, err = util.UnmarshalGenericJSON[api.EventPayloadUpdateUpdatedDto](bytes); err != nil {
return nil, newServiceError(General, err)
}
return &eventPayloadInformationDto{Host: p.Host, Application: p.Application, Provider: p.Provider, Version: p.Version}, nil
case api.EventNameUpdateUpdatedIgnored.Value(): case api.EventNameUpdateUpdatedIgnored.Value():
var p api.EventPayloadUpdateUpdatedDto
if p, err = util.UnmarshalGenericJSON[api.EventPayloadUpdateUpdatedDto](bytes); err != nil {
return nil, newServiceError(General, err)
}
return &eventPayloadInformationDto{Host: p.Host, Application: p.Application, Provider: p.Provider, Version: p.Version}, nil
case api.EventNameUpdateUpdated.Value(): case api.EventNameUpdateUpdated.Value():
var p api.EventPayloadUpdateUpdatedDto var p api.EventPayloadUpdateUpdatedDto
if p, err = util.UnmarshalGenericJSON[api.EventPayloadUpdateUpdatedDto](bytes); err != nil { if p, err = util.UnmarshalGenericJSON[api.EventPayloadUpdateUpdatedDto](bytes); err != nil {