-
Notifications
You must be signed in to change notification settings - Fork 124
/
scheduler.go
62 lines (58 loc) · 1.92 KB
/
scheduler.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
package data
import (
"context"
"github.com/evergreen-ci/evergreen/model/distro"
"github.com/evergreen-ci/evergreen/model/task"
"github.com/evergreen-ci/evergreen/scheduler"
"github.com/pkg/errors"
)
// CompareTasks returns the order that the given tasks would be scheduled, along with the scheduling logic.
func CompareTasks(ctx context.Context, taskIds []string, useLegacy bool) ([]string, map[string]map[string]string, error) {
if len(taskIds) == 0 {
return nil, nil, nil
}
tasks, err := task.Find(task.ByIds(taskIds))
if err != nil {
return nil, nil, errors.Wrap(err, "finding tasks to compare")
}
if len(tasks) != len(taskIds) {
return nil, nil, errors.Errorf("%d/%d tasks do not exist", len(taskIds)-len(tasks), len(taskIds))
}
distroId := tasks[0].DistroId
for _, t := range tasks {
if t.DistroId != distroId {
return nil, nil, errors.New("all tasks must have the same distro ID")
}
}
tasks, versions, err := scheduler.FilterTasksWithVersionCache(tasks)
if err != nil {
return nil, nil, errors.Wrap(err, "finding versions for tasks")
}
cmp := scheduler.CmpBasedTaskPrioritizer{}
logic := map[string]map[string]string{}
if useLegacy {
tasks, logic, err = cmp.PrioritizeTasks(distroId, tasks, versions)
if err != nil {
return nil, nil, errors.Wrap(err, "prioritizing tasks")
}
prioritizedIds := []string{}
for _, t := range tasks {
prioritizedIds = append(prioritizedIds, t.Id)
}
} else { // this is temporary: logic should be added in EVG-13795
d, err := distro.FindOneId(ctx, distroId)
if err != nil {
return nil, nil, errors.Wrapf(err, "finding distro '%s'", distroId)
}
if d == nil {
return nil, nil, errors.Errorf("distro '%s' not found", distroId)
}
taskPlan := scheduler.PrepareTasksForPlanning(d, tasks)
tasks = taskPlan.Export()
}
prioritizedIds := []string{}
for _, t := range tasks {
prioritizedIds = append(prioritizedIds, t.Id)
}
return prioritizedIds, logic, nil
}