Skip to content

Commit

Permalink
feat: retry playbook actions
Browse files Browse the repository at this point in the history
  • Loading branch information
adityathebe authored and moshloop committed Feb 18, 2025
1 parent 1a7f5eb commit a44d0b7
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 0 deletions.
25 changes: 25 additions & 0 deletions models/playbooks.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ const (
PlaybookRunStatusRunning PlaybookRunStatus = "running"
PlaybookRunStatusScheduled PlaybookRunStatus = "scheduled"
PlaybookRunStatusSleeping PlaybookRunStatus = "sleeping"
PlaybookRunStatusRetrying PlaybookRunStatus = "retrying"
PlaybookRunStatusWaiting PlaybookRunStatus = "waiting" // waiting for a consumer
)

Expand Down Expand Up @@ -220,6 +221,14 @@ func (p PlaybookRun) Schedule(db *gorm.DB) error {
})
}

func (p PlaybookRun) Retry(db *gorm.DB, delay time.Duration) error {
return p.Update(db, map[string]any{
"status": PlaybookRunStatusRetrying,
"start_time": gorm.Expr("CASE WHEN start_time IS NULL THEN CLOCK_TIMESTAMP() ELSE start_time END"),
"scheduled_time": gorm.Expr(fmt.Sprintf("CLOCK_TIMESTAMP() + INTERVAL '%d SECONDS'", int(delay.Seconds()))),
})
}

func (p PlaybookRun) Delay(db *gorm.DB, delay time.Duration) error {
return p.Update(db, map[string]any{
"status": PlaybookRunStatusSleeping,
Expand Down Expand Up @@ -291,6 +300,19 @@ func (p PlaybookRun) Assign(db *gorm.DB, agent *Agent, action string) error {
return p.Waiting(db)
}

func (p PlaybookRun) RetryAction(db *gorm.DB, action string, retryCount int) (*PlaybookRunAction, error) {
runAction := PlaybookRunAction{
PlaybookRunID: p.ID,
Name: action,
Status: PlaybookActionStatusScheduled,
RetryCount: retryCount,
}
if err := db.Save(&runAction).Error; err != nil {
return nil, oops.Tags("db").Wrap(err)
}
return &runAction, p.Running(db)
}

func (p PlaybookRun) StartAction(db *gorm.DB, action string) (*PlaybookRunAction, error) {
runAction := PlaybookRunAction{
PlaybookRunID: p.ID,
Expand Down Expand Up @@ -468,6 +490,9 @@ type PlaybookRunAction struct {
Error *string `json:"error,omitempty" gorm:"default:null"`
IsPushed bool `json:"is_pushed"`
AgentID *uuid.UUID `json:"agent_id,omitempty"`

// RetryCount represents the Nth retry of this action
RetryCount int `json:"attempt,omitempty" gorm:"default:NULL"`
}

func (p PlaybookRunAction) JSON() (out map[string]any) {
Expand Down
5 changes: 5 additions & 0 deletions schema/playbooks.hcl
Original file line number Diff line number Diff line change
Expand Up @@ -319,6 +319,11 @@ table "playbook_run_actions" {
type = text
default = "running"
}
column "retry_count" {
null = true
type = integer
comment = "the nth retry of this action"
}
column "playbook_run_id" {
null = true
type = uuid
Expand Down

0 comments on commit a44d0b7

Please sign in to comment.