-
Notifications
You must be signed in to change notification settings - Fork 158
/
tasks.go
86 lines (68 loc) · 1.79 KB
/
tasks.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
package empire
import (
"fmt"
"time"
"github.com/remind101/empire/pkg/constraints"
"github.com/remind101/empire/twelvefactor"
"golang.org/x/net/context"
)
// Host represents the host of the task
type Host struct {
// the host id
ID string
}
// Task represents a running process.
type Task struct {
// The name of the task.
Name string
// The name of the process that this task is for.
Type string
// The task id
ID string
// The host of the task
Host Host
// The command that this task is running.
Command Command
// The state of the task.
State string
// The time that the state was recorded.
UpdatedAt time.Time
// The constraints of the Process.
Constraints Constraints
}
type tasksService struct {
*Empire
}
func (s *tasksService) Tasks(ctx context.Context, app *App) ([]*Task, error) {
var tasks []*Task
instances, err := s.Scheduler.Tasks(ctx, app.ID)
if err != nil {
return tasks, err
}
for _, i := range instances {
tasks = append(tasks, taskFromInstance(i))
}
return tasks, nil
}
// taskFromInstance converts a scheduler.Instance into a Task.
// It pulls some of its data from empire specific environment variables if they have been set.
// Once ECS supports this data natively, we can stop doing this.
func taskFromInstance(i *twelvefactor.Task) *Task {
version := i.Process.Env["EMPIRE_RELEASE"]
if version == "" {
version = "v0"
}
return &Task{
Name: fmt.Sprintf("%s.%s.%s", version, i.Process.Type, i.ID),
Type: string(i.Process.Type),
Host: Host{ID: i.Host.ID},
Command: Command(i.Process.Command),
Constraints: Constraints{
CPUShare: constraints.CPUShare(i.Process.CPUShares),
Memory: constraints.Memory(i.Process.Memory),
Nproc: constraints.Nproc(i.Process.Nproc),
},
State: i.State,
UpdatedAt: i.UpdatedAt,
}
}