Skip to content

Commit 74afcea

Browse files
committed
fix(deisctl): "deisctl scale router=N" uses async interface
The scale command uses a similar async WaitGroup interface to other commands now, which fixes the scale command. Also disallows scale operations on anything but router, since other unit types do not expect the "unit@1" naming scheme.
1 parent 101301b commit 74afcea

3 files changed

Lines changed: 24 additions & 21 deletions

File tree

deisctl/backend/backend.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ type Backend interface {
88
Destroy([]string, *sync.WaitGroup, chan string, chan error)
99
Start([]string, *sync.WaitGroup, chan string, chan error)
1010
Stop([]string, *sync.WaitGroup, chan string, chan error)
11-
Scale(string, int) error
11+
Scale(string, int, *sync.WaitGroup, chan string, chan error)
1212
ListUnits() error
1313
ListUnitFiles() error
1414
Status(string) error

deisctl/backend/fleet/scale.go

Lines changed: 11 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -9,48 +9,45 @@ import (
99
)
1010

1111
// Scale creates or destroys units to match the desired number
12-
func (c *FleetClient) Scale(component string, requested int) error {
13-
14-
outchan := make(chan string)
15-
errchan := make(chan error)
16-
var wg sync.WaitGroup
12+
func (c *FleetClient) Scale(
13+
component string, requested int, wg *sync.WaitGroup, outchan chan string, errchan chan error) {
1714

1815
if requested < 0 {
19-
return errors.New("cannot scale below 0")
16+
errchan <- errors.New("cannot scale below 0")
2017
}
2118
// check how many currently exist
2219
components, err := c.Units(component)
2320
if err != nil {
2421
// skip checking the first time; we just want a tally
2522
if !strings.Contains(err.Error(), "could not find unit") {
26-
return err
23+
errchan <- err
24+
return
2725
}
2826
}
2927

3028
timesToScale := int(math.Abs(float64(requested - len(components))))
3129
if timesToScale == 0 {
32-
return nil
30+
return
3331
}
3432
if requested-len(components) > 0 {
35-
return scaleUp(c, component, len(components), timesToScale, &wg, outchan, errchan)
33+
scaleUp(c, component, len(components), timesToScale, wg, outchan, errchan)
34+
} else {
35+
scaleDown(c, component, len(components), timesToScale, wg, outchan, errchan)
3636
}
37-
return scaleDown(c, component, len(components), timesToScale, &wg, outchan, errchan)
3837
}
3938

4039
func scaleUp(c *FleetClient, component string, numExistingContainers, numTimesToScale int,
41-
wg *sync.WaitGroup, outchan chan string, errchan chan error) error {
40+
wg *sync.WaitGroup, outchan chan string, errchan chan error) {
4241
for i := 0; i < numTimesToScale; i++ {
4342
target := component + "@" + strconv.Itoa(numExistingContainers+i+1)
4443
c.Create([]string{target}, wg, outchan, errchan)
4544
}
46-
return nil
4745
}
4846

4947
func scaleDown(c *FleetClient, component string, numExistingContainers, numTimesToScale int,
50-
wg *sync.WaitGroup, outchan chan string, errchan chan error) error {
48+
wg *sync.WaitGroup, outchan chan string, errchan chan error) {
5149
for i := 0; i < numTimesToScale; i++ {
5250
target := component + "@" + strconv.Itoa(numExistingContainers-i)
5351
c.Destroy([]string{target}, wg, outchan, errchan)
5452
}
55-
return nil
5653
}

deisctl/cmd/cmd.go

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -44,19 +44,25 @@ func ListUnitFiles(b backend.Backend) error {
4444
}
4545

4646
func Scale(b backend.Backend, targets []string) error {
47+
outchan := make(chan string)
48+
errchan := make(chan error)
49+
var wg sync.WaitGroup
50+
51+
go printState(outchan, errchan, 500*time.Millisecond)
52+
4753
for _, target := range targets {
4854
component, num, err := splitScaleTarget(target)
4955
if err != nil {
5056
return err
5157
}
52-
// the router is the only component that can scale past 1 at the moment
53-
if num > 1 && !strings.Contains(component, "router") {
54-
return fmt.Errorf("cannot scale %s past 1", component)
55-
}
56-
if err := b.Scale(component, num); err != nil {
57-
return err
58+
// the router is the only component that can scale at the moment
59+
if !strings.Contains(component, "router") {
60+
return fmt.Errorf("cannot scale %s components", component)
5861
}
62+
b.Scale(component, num, &wg, outchan, errchan)
63+
wg.Wait()
5964
}
65+
close(outchan)
6066
return nil
6167
}
6268

0 commit comments

Comments
 (0)