Skip to content

Commit 3121f02

Browse files
author
lijianguo
committed
feat(ps):add ps:stop/start command
1 parent 82230b1 commit 3121f02

4 files changed

Lines changed: 215 additions & 61 deletions

File tree

api/ps.go

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -10,11 +10,12 @@ type ProcessType map[string]string
1010

1111
// Pods defines the structure of a process.
1212
type Pods struct {
13-
Release string `json:"release"`
14-
Type string `json:"type"`
15-
Name string `json:"name"`
16-
State string `json:"state"`
17-
Started time.Time `json:"started"`
13+
Release string `json:"release,omitempty"`
14+
Type string `json:"type,omitempty"`
15+
Name string `json:"name,omitempty"`
16+
State string `json:"state,omitempty"`
17+
Started time.Time `json:"started,omitempty"`
18+
Replicas int `json:"replicas,omitempty"`
1819
}
1920

2021
// PodsList defines a collection of app pods.
@@ -28,11 +29,18 @@ func (p PodsList) Less(i, j int) bool { return p[i].Name < p[j].Name }
2829
type PodType struct {
2930
Type string
3031
PodsList PodsList
32+
Replicas int
33+
Status string
3134
}
3235

3336
// PodTypes holds groups of pods organized by type.
3437
type PodTypes []PodType
3538

39+
// Start is the definition of POST /v2/apps/<app_id>/stop or POST /v2/apps/<app_id>/start.
40+
type Types struct {
41+
Types []string `json:"types,omitempty"`
42+
}
43+
3644
func (p PodTypes) Len() int { return len(p) }
3745
func (p PodTypes) Swap(i, j int) { p[i], p[j] = p[j], p[i] }
3846
func (p PodTypes) Less(i, j int) bool { return p[i].Type < p[j].Type }

api/ps_test.go

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,10 @@ import (
99

1010
func TestPodsListSorted(t *testing.T) {
1111
pods := PodsList{
12-
{"", "web", "web.fsdfgh4", "up", time.Time{}},
13-
{"", "web", "web.asdfgh1", "up", time.Time{}},
14-
{"", "web", "web.csdfgh3", "up", time.Time{}},
15-
{"", "web", "web.bsdfgh2", "up", time.Time{}},
12+
{"", "web", "web.fsdfgh4", "up", time.Time{}, 4},
13+
{"", "web", "web.asdfgh1", "up", time.Time{}, 4},
14+
{"", "web", "web.csdfgh3", "up", time.Time{}, 4},
15+
{"", "web", "web.bsdfgh2", "up", time.Time{}, 4},
1616
}
1717

1818
sort.Sort(pods)
@@ -28,9 +28,9 @@ func TestPodsListSorted(t *testing.T) {
2828

2929
func TestPodTypesSorted(t *testing.T) {
3030
podTypes := PodTypes{
31-
{"worker", PodsList{}},
32-
{"web", PodsList{}},
33-
{"clock", PodsList{}},
31+
{"worker", PodsList{}, 4, "started"},
32+
{"web", PodsList{}, 4, "started"},
33+
{"clock", PodsList{}, 4, "started"},
3434
}
3535

3636
sort.Sort(podTypes)

ps/ps.go

Lines changed: 46 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,40 @@ func Scale(c *drycc.Client, appID string, targets map[string]int) error {
4444
return err
4545
}
4646

47+
// Stop decreases an app's processes to 0. The processes are specified in the target argument
48+
func Stop(c *drycc.Client, appID string, targets map[string][]string) error {
49+
u := fmt.Sprintf("/v2/apps/%s/stop/", appID)
50+
51+
body, err := json.Marshal(targets)
52+
53+
if err != nil {
54+
return err
55+
}
56+
57+
res, err := c.Request("POST", u, body)
58+
if err == nil {
59+
return res.Body.Close()
60+
}
61+
return err
62+
}
63+
64+
// Start increases an app's processes to setting. The processes are specified in the target argument
65+
func Start(c *drycc.Client, appID string, targets map[string][]string) error {
66+
u := fmt.Sprintf("/v2/apps/%s/start/", appID)
67+
68+
body, err := json.Marshal(targets)
69+
70+
if err != nil {
71+
return err
72+
}
73+
74+
res, err := c.Request("POST", u, body)
75+
if err == nil {
76+
return res.Body.Close()
77+
}
78+
return err
79+
}
80+
4781
// Restart restarts an app's processes. To restart all app processes, pass empty strings for
4882
// procType and name. To restart an specific process, pass an procType by leave name empty.
4983
// To restart a specific instance, pass a procType and a name.
@@ -84,16 +118,26 @@ func ByType(processes api.PodsList) api.PodTypes {
84118
for i, pt := range pts {
85119
if pt.Type == process.Type {
86120
exists = true
87-
pts[i].PodsList = append(pts[i].PodsList, process)
121+
if process.Name != "" {
122+
pts[i].PodsList = append(pts[i].PodsList, process)
123+
}
88124
break
89125
}
90126
}
91127

92128
// Is processtype for process doesn't exist, create a new one
93129
if !exists {
130+
p := api.PodsList{}
131+
status := "stopped"
132+
if process.Name != "" {
133+
p = api.PodsList{process}
134+
status = "started"
135+
}
94136
pts = append(pts, api.PodType{
95137
Type: process.Type,
96-
PodsList: api.PodsList{process},
138+
PodsList: p,
139+
Replicas: process.Replicas,
140+
Status: status,
97141
})
98142
}
99143
}

0 commit comments

Comments
 (0)