Skip to content

Commit

Permalink
Refactor schedule promises (#161)
Browse files Browse the repository at this point in the history
  • Loading branch information
dfarr authored Dec 20, 2023
1 parent e6729b2 commit f141c0b
Showing 1 changed file with 6 additions and 8 deletions.
14 changes: 6 additions & 8 deletions internal/app/coroutines/schedulePromises.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,15 +67,14 @@ func schedulePromise(tid string, schedule *schedule.Schedule) *scheduler.Corouti
// handle creating promise (schedule run) and updating schedule record.

return scheduler.NewCoroutine(metadata, func(c *scheduler.Coroutine[*t_aio.Completion, *t_aio.Submission]) {
crontime := schedule.NextRunTime
next, err := util.Next(crontime, schedule.Cron)
next, err := util.Next(schedule.NextRunTime, schedule.Cron)
if err != nil {
slog.Error("failed to calculate next run time", "err", err)
return
}

id, err := generatePromiseId(schedule.PromiseId, map[string]string{
"timestamp": fmt.Sprintf("%d", crontime),
"timestamp": fmt.Sprintf("%d", schedule.NextRunTime),
})
if err != nil {
slog.Error("failed to generate promise id", "err", err)
Expand All @@ -91,9 +90,8 @@ func schedulePromise(tid string, schedule *schedule.Schedule) *scheduler.Corouti

// calculate timeout for promise

now := c.Time()
state := promise.Pending
if schedule.PromiseTimeout+schedule.NextRunTime < now {
if c.Time() >= schedule.PromiseTimeout+schedule.NextRunTime {
state = promise.Timedout
}

Expand All @@ -113,14 +111,14 @@ func schedulePromise(tid string, schedule *schedule.Schedule) *scheduler.Corouti
Tags: map[string]string{
"resonate:invocation": "true",
},
CreatedOn: crontime,
CreatedOn: schedule.NextRunTime,
},
},
{
Kind: t_aio.UpdateSchedule,
UpdateSchedule: &t_aio.UpdateScheduleCommand{
Id: schedule.Id,
LastRunTime: &crontime,
LastRunTime: &schedule.NextRunTime,
NextRunTime: next,
},
},
Expand Down Expand Up @@ -149,7 +147,7 @@ func schedulePromise(tid string, schedule *schedule.Schedule) *scheduler.Corouti
// helpers

func generatePromiseId(id string, vars map[string]string) (string, error) {
t := template.Must(template.New("promiseID").Parse(id))
t := template.Must(template.New("promiseId").Parse(id))

var replaced strings.Builder
if err := t.Execute(&replaced, vars); err != nil {
Expand Down

0 comments on commit f141c0b

Please sign in to comment.