fix: UI buttons spin while waiting for tasks to complete

This commit is contained in:
garethgeorge
2023-12-31 11:36:36 -08:00
parent 85f70e64d1
commit c767fa7476
5 changed files with 42 additions and 19 deletions

View File

@@ -230,14 +230,18 @@ func (o *Orchestrator) Run(mainCtx context.Context) {
}
start := time.Now()
if err := t.task.Run(taskCtx); err != nil {
zap.L().Error("task failed", zap.String("task", t.task.Name()), zap.Error(err))
err := t.task.Run(taskCtx)
if err != nil {
zap.L().Error("task failed", zap.String("task", t.task.Name()), zap.Error(err), zap.Duration("duration", time.Since(start)))
} else {
zap.L().Info("task finished", zap.String("task", t.task.Name()), zap.Duration("duration", time.Since(start)))
}
o.runningTask.Store(nil)
for _, cb := range t.callbacks {
cb(err)
}
if nextTime := t.task.Next(o.curTime()); nextTime != nil {
o.taskQueue.Push(scheduledTask{
task: t.task,
@@ -247,16 +251,17 @@ func (o *Orchestrator) Run(mainCtx context.Context) {
}
}
func (o *Orchestrator) ScheduleTask(t Task, priority int) {
func (o *Orchestrator) ScheduleTask(t Task, priority int, callbacks ...func(error)) {
nextRun := t.Next(o.curTime())
if nextRun == nil {
return
}
zap.L().Info("scheduling task", zap.String("task", t.Name()), zap.String("runAt", nextRun.Format(time.RFC3339)))
o.taskQueue.Push(scheduledTask{
task: t,
runAt: *nextRun,
priority: priority,
task: t,
runAt: *nextRun,
priority: priority,
callbacks: callbacks,
})
}