-
Notifications
You must be signed in to change notification settings - Fork 159
/
scheduler.go
76 lines (64 loc) · 1.74 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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
package empire
import (
"fmt"
"sync"
"github.com/remind101/empire/pkg/timex"
"github.com/remind101/empire/twelvefactor"
"golang.org/x/net/context"
)
type Scheduler twelvefactor.Scheduler
type FakeScheduler struct {
sync.Mutex
apps map[string]*twelvefactor.Manifest
}
func NewFakeScheduler() *FakeScheduler {
return &FakeScheduler{
apps: make(map[string]*twelvefactor.Manifest),
}
}
func (m *FakeScheduler) Submit(ctx context.Context, app *twelvefactor.Manifest, ss twelvefactor.StatusStream) error {
m.Lock()
defer m.Unlock()
m.apps[app.AppID] = app
return nil
}
func (m *FakeScheduler) Restart(ctx context.Context, appID string, ss twelvefactor.StatusStream) error {
return nil
}
func (m *FakeScheduler) Remove(ctx context.Context, appID string) error {
delete(m.apps, appID)
return nil
}
func (m *FakeScheduler) Tasks(ctx context.Context, appID string) ([]*twelvefactor.Task, error) {
var instances []*twelvefactor.Task
if a, ok := m.apps[appID]; ok {
for _, p := range a.Processes {
pp := *p
pp.Env = twelvefactor.Env(a, p)
for i := 1; i <= p.Quantity; i++ {
instances = append(instances, &twelvefactor.Task{
ID: fmt.Sprintf("%d", i),
Host: twelvefactor.Host{ID: "i-aa111aa1"},
State: "running",
Process: &pp,
UpdatedAt: timex.Now(),
})
}
}
}
return instances, nil
}
func (m *FakeScheduler) Stop(ctx context.Context, instanceID string) error {
return nil
}
func (m *FakeScheduler) Run(ctx context.Context, app *twelvefactor.Manifest) error {
for _, p := range app.Processes {
if p.Stderr != nil {
fmt.Fprintf(p.Stdout, "Attaching to container\n")
}
if p.Stdout != nil {
fmt.Fprintf(p.Stdout, "Fake output for `%s` on %s\n", p.Command, app.Name)
}
}
return nil
}