Skip to content

Commit

Permalink
fix: playbook run duplicates
Browse files Browse the repository at this point in the history
  • Loading branch information
adityathebe authored and moshloop committed Sep 22, 2024
1 parent 8481e91 commit 4f45c27
Show file tree
Hide file tree
Showing 4 changed files with 7 additions and 7 deletions.
3 changes: 2 additions & 1 deletion playbook/run_consumer.go
Original file line number Diff line number Diff line change
Expand Up @@ -299,12 +299,13 @@ func RunConsumer(ctx context.Context) (int, error) {
FROM playbook_runs
INNER JOIN playbooks ON playbooks.id = playbook_runs.playbook_id
WHERE status IN (?, ?) AND scheduled_time <= NOW()
AND (agent_id IS NULL OR agent_id = ?)
ORDER BY scheduled_time
FOR UPDATE SKIP LOCKED
LIMIT 1
`
var run models.PlaybookRun
if err := tx.Raw(query, models.PlaybookRunStatusScheduled, models.PlaybookRunStatusSleeping).First(&run).Error; err != nil {
if err := tx.Raw(query, models.PlaybookRunStatusScheduled, models.PlaybookRunStatusSleeping, uuid.Nil).First(&run).Error; err != nil {
if errors.Is(err, gorm.ErrRecordNotFound) {
return nil
}
Expand Down
4 changes: 2 additions & 2 deletions playbook/runner/agent.go
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ func getActionForAgent(ctx context.Context, agent *models.Agent) (*ActionForAgen
return nil, ctx.Oops().Wrapf(err, "failed to template env")
}

spec, err := getActionSpec(ctx, playbook, step.Name)
spec, err := getActionSpec(playbook, step.Name)
if err != nil {
return nil, ctx.Oops().Wrap(err)
}
Expand All @@ -117,7 +117,7 @@ func getActionForAgent(ctx context.Context, agent *models.Agent) (*ActionForAgen
TemplateEnv: templateEnv,
}

if skip, err := filterAction(ctx, run.ID, spec.Filter); err != nil {
if skip, err := filterAction(ctx, spec.Filter); err != nil {
return nil, ctx.Oops().Wrap(err)
} else {
// We run the filter on the upstream and simply send the filter result to the agent.
Expand Down
2 changes: 1 addition & 1 deletion playbook/runner/exec.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ func executeAction(ctx context.Context, playbookID any, runID uuid.UUID, runActi
}

if actionSpec.Filter != "" {
if skipped, err := filterAction(ctx, runID, actionSpec.Filter); err != nil {
if skipped, err := filterAction(ctx, actionSpec.Filter); err != nil {
return executeActionResult{}, err
} else if skipped {
ctx.Debugf("skipping %s", actionSpec.Name)
Expand Down
5 changes: 2 additions & 3 deletions playbook/runner/runner.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ import (
v1 "github.com/flanksource/incident-commander/api/v1"
"github.com/flanksource/incident-commander/db"
"github.com/flanksource/incident-commander/playbook/actions"
"github.com/google/uuid"
"github.com/samber/lo"
"github.com/samber/oops"
"gorm.io/gorm"
Expand Down Expand Up @@ -87,7 +86,7 @@ func findNextActionWithFilter(actions []v1.PlaybookAction) *v1.PlaybookAction {
return nil
}

func getActionSpec(ctx context.Context, playbook *models.Playbook, name string) (*v1.PlaybookAction, error) {
func getActionSpec(playbook *models.Playbook, name string) (*v1.PlaybookAction, error) {
var spec v1.PlaybookSpec
if err := json.Unmarshal(playbook.Spec, &spec); err != nil {
return nil, err
Expand Down Expand Up @@ -326,7 +325,7 @@ func TemplateAndExecuteAction(ctx context.Context, spec v1.Playbook, playbook *m
return oops.Wrap(ExecuteAndSaveAction(ctx, run.PlaybookID, action, step))
}

func filterAction(ctx context.Context, runID uuid.UUID, filter string) (bool, error) {
func filterAction(ctx context.Context, filter string) (bool, error) {
if strings.TrimSpace(filter) == "" {
return false, nil
}
Expand Down

0 comments on commit 4f45c27

Please sign in to comment.