Skip to content

Commit

Permalink
fix: duplicate cron expressions only cause a single trigger (#1101)
Browse files Browse the repository at this point in the history
  • Loading branch information
abelanger5 authored Dec 6, 2024
1 parent 33d10dc commit 1499668
Showing 1 changed file with 15 additions and 8 deletions.
23 changes: 15 additions & 8 deletions internal/services/ticker/cron.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,13 +37,13 @@ func (t *TickerImpl) runPollCronSchedules(ctx context.Context) func() {
})

for _, cron := range crons {
workflowVersionId := sqlchelpers.UUIDToStr(cron.WorkflowVersionId)
cronKey := getCronKey(cron)

t.l.Debug().Msgf("ticker: handling cron %s for version %s", cron.Cron, workflowVersionId)
t.l.Debug().Msgf("ticker: handling cron %s", cronKey)

// if the cron is already scheduled, mark it as existing
if _, ok := existingCrons[getCronKey(workflowVersionId, cron.Cron)]; ok {
existingCrons[getCronKey(workflowVersionId, cron.Cron)] = true
if _, ok := existingCrons[cronKey]; ok {
existingCrons[cronKey] = true
continue
}

Expand All @@ -52,7 +52,7 @@ func (t *TickerImpl) runPollCronSchedules(ctx context.Context) func() {
t.l.Err(err).Msg("could not schedule cron")
}

existingCrons[getCronKey(workflowVersionId, cron.Cron)] = true
existingCrons[cronKey] = true
}

// cancel any crons that are no longer assigned to this ticker
Expand Down Expand Up @@ -101,7 +101,7 @@ func (t *TickerImpl) handleScheduleCron(ctx context.Context, cron *dbsqlc.PollCr
}

// store the schedule in the cron map
t.crons.Store(getCronKey(workflowVersionId, cron.Cron), s)
t.crons.Store(getCronKey(cron), s)

s.Start()

Expand Down Expand Up @@ -178,6 +178,13 @@ func (t *TickerImpl) handleCancelCron(ctx context.Context, key string) error {
return nil
}

func getCronKey(workflowVersionId, schedule string) string {
return fmt.Sprintf("%s-%s", workflowVersionId, schedule)
func getCronKey(cron *dbsqlc.PollCronSchedulesRow) string {
workflowVersionId := sqlchelpers.UUIDToStr(cron.WorkflowVersionId)

switch cron.Method {
case dbsqlc.WorkflowTriggerCronRefMethodsAPI:
return fmt.Sprintf("API-%s-%s-%s", workflowVersionId, cron.Cron, cron.Name.String)
default:
return fmt.Sprintf("DEFAULT-%s-%s", workflowVersionId, cron.Cron)
}
}

0 comments on commit 1499668

Please sign in to comment.