-
Notifications
You must be signed in to change notification settings - Fork 124
/
task_queue_service.go
124 lines (104 loc) · 3.59 KB
/
task_queue_service.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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
package model
import (
"context"
"sync"
"time"
"github.com/evergreen-ci/evergreen"
"github.com/evergreen-ci/evergreen/model/distro"
"github.com/pkg/errors"
)
type TaskQueueItemDispatcher interface {
FindNextTask(context.Context, string, TaskSpec, time.Time) (*TaskQueueItem, error)
Refresh(context.Context, string) error
RefreshFindNextTask(context.Context, string, TaskSpec, time.Time) (*TaskQueueItem, error)
}
type CachedDispatcher interface {
Refresh() error
FindNextTask(context.Context, TaskSpec, time.Time) *TaskQueueItem
Type() string
CreatedAt() time.Time
}
type taskDispatchService struct {
cachedDispatchers map[string]CachedDispatcher
mu sync.RWMutex
ttl time.Duration
useAliases bool
}
func NewTaskDispatchService(ttl time.Duration) TaskQueueItemDispatcher {
return &taskDispatchService{
ttl: ttl,
cachedDispatchers: map[string]CachedDispatcher{},
}
}
func NewTaskDispatchAliasService(ttl time.Duration) TaskQueueItemDispatcher {
return &taskDispatchService{
ttl: ttl,
useAliases: true,
cachedDispatchers: map[string]CachedDispatcher{},
}
}
func (s *taskDispatchService) FindNextTask(ctx context.Context, distroID string, spec TaskSpec, amiUpdatedTime time.Time) (*TaskQueueItem, error) {
distroDispatchService, err := s.ensureQueue(ctx, distroID)
if err != nil {
return nil, errors.WithStack(err)
}
return distroDispatchService.FindNextTask(ctx, spec, amiUpdatedTime), nil
}
func (s *taskDispatchService) RefreshFindNextTask(ctx context.Context, distroID string, spec TaskSpec, amiUpdatedTime time.Time) (*TaskQueueItem, error) {
distroDispatchService, err := s.ensureQueue(ctx, distroID)
if err != nil {
return nil, errors.WithStack(err)
}
if err := distroDispatchService.Refresh(); err != nil {
return nil, errors.WithStack(err)
}
return distroDispatchService.FindNextTask(ctx, spec, amiUpdatedTime), nil
}
func (s *taskDispatchService) Refresh(ctx context.Context, distroID string) error {
distroDispatchService, err := s.ensureQueue(ctx, distroID)
if err != nil {
return errors.WithStack(err)
}
if err := distroDispatchService.Refresh(); err != nil {
return errors.WithStack(err)
}
return nil
}
func (s *taskDispatchService) ensureQueue(ctx context.Context, distroID string) (CachedDispatcher, error) {
d := distro.Distro{}
foundDistro, err := distro.FindOneId(ctx, distroID)
if err != nil {
return nil, errors.Wrapf(err, "finding distro '%s'", distroID)
}
if foundDistro != nil {
d = *foundDistro
}
// If there is a "distro": *basicCachedDispatcherImpl in the cachedDispatchers map, return that.
// Otherwise, get the "distro"'s taskQueue from the database; seed its cachedDispatcher; put that in the map and return it.
s.mu.Lock()
defer s.mu.Unlock()
distroDispatchService, ok := s.cachedDispatchers[distroID]
if ok && distroDispatchService.Type() == d.DispatcherSettings.Version {
return distroDispatchService, nil
}
var taskQueue TaskQueue
if s.useAliases {
taskQueue, err = FindDistroSecondaryTaskQueue(distroID)
} else {
taskQueue, err = FindDistroTaskQueue(distroID)
}
if err != nil {
return nil, errors.WithStack(errors.Wrap(err, "finding task queue"))
}
switch d.DispatcherSettings.Version {
case evergreen.DispatcherVersionRevisedWithDependencies:
distroDispatchService, err = newDistroTaskDAGDispatchService(taskQueue, s.ttl)
if err != nil {
return nil, err
}
default:
return nil, errors.Errorf("invalid dispatcher version '%s'", d.DispatcherSettings.Version)
}
s.cachedDispatchers[distroID] = distroDispatchService
return distroDispatchService, nil
}