-
Notifications
You must be signed in to change notification settings - Fork 38
/
pending.go
58 lines (52 loc) · 1.35 KB
/
pending.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
package handlers
import (
"context"
"strings"
"time"
"github.com/pkg/errors"
"github.com/rs/zerolog/log"
"github.com/runabol/tork"
"github.com/runabol/tork/datastore"
"github.com/runabol/tork/internal/coordinator/scheduler"
"github.com/runabol/tork/middleware/task"
"github.com/runabol/tork/mq"
)
type pendingHandler struct {
sched scheduler.Scheduler
ds datastore.Datastore
broker mq.Broker
}
func NewPendingHandler(ds datastore.Datastore, b mq.Broker) task.HandlerFunc {
h := &pendingHandler{
ds: ds,
broker: b,
sched: *scheduler.NewScheduler(ds, b),
}
return h.handle
}
func (h *pendingHandler) handle(ctx context.Context, t *tork.Task) error {
log.Debug().
Str("task-id", t.ID).
Msg("handling pending task")
if strings.TrimSpace(t.If) == "false" {
return h.skipTask(ctx, t)
} else {
return h.sched.ScheduleTask(ctx, t)
}
}
func (h *pendingHandler) skipTask(ctx context.Context, t *tork.Task) error {
now := time.Now().UTC()
t.State = tork.TaskStateScheduled
t.ScheduledAt = &now
t.StartedAt = &now
t.CompletedAt = &now
if err := h.ds.UpdateTask(ctx, t.ID, func(u *tork.Task) error {
u.State = t.State
u.ScheduledAt = t.ScheduledAt
u.StartedAt = t.StartedAt
return nil
}); err != nil {
return errors.Wrapf(err, "error updating task in datastore")
}
return h.broker.PublishTask(ctx, mq.QUEUE_COMPLETED, t)
}