Skip to content

Commit

Permalink
introduce a :done state for tasks and fix tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Andre Medeiros committed Jul 12, 2020
1 parent b02fb28 commit f4d524d
Show file tree
Hide file tree
Showing 7 changed files with 76 additions and 62 deletions.
1 change: 1 addition & 0 deletions Gemfile
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
source 'https://rubygems.org'

gem 'byebug', '~> 11.1.3'
gem 'os', '~> 1.1.0'
gem 'rspec', '~> 3.9.0'
2 changes: 2 additions & 0 deletions Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ GEM
specs:
byebug (11.1.3)
diff-lcs (1.4.4)
os (1.1.0)
rspec (3.9.0)
rspec-core (~> 3.9.0)
rspec-expectations (~> 3.9.0)
Expand All @@ -22,6 +23,7 @@ PLATFORMS

DEPENDENCIES
byebug (~> 11.1.3)
os (~> 1.1.0)
rspec (~> 3.9.0)

BUNDLED WITH
Expand Down
2 changes: 1 addition & 1 deletion internal/task/networking.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,5 +64,5 @@ func init() {
RunsAfter("command:up", "networking:start")
RunsAfter("command:task", "networking:start")
RunsAfter("command:exec", "networking:start")
RunsAfter("command:down", "networking:stop")
RunsAfter("command:down:done", "networking:stop")
}
5 changes: 3 additions & 2 deletions internal/task/networking_linux.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package task

import (
"context"
"fmt"

"github.com/andremedeiros/loon/internal/executor"
"github.com/andremedeiros/loon/internal/project"
Expand All @@ -13,7 +14,7 @@ func (*NetworkingStart) Resolve(_ context.Context, p *project.Project) error {
"ip",
"addr",
"add",
p.IP.String(),
fmt.Sprintf("%s/32", p.IP),
"dev",
"lo",
})
Expand All @@ -25,7 +26,7 @@ func (*NetworkingStop) Resolve(_ context.Context, p *project.Project) error {
"ip",
"addr",
"del",
p.IP.String(),
fmt.Sprintf("%s/32", p.IP),
"dev",
"lo",
})
Expand Down
118 changes: 61 additions & 57 deletions internal/task/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,75 +21,79 @@ func Run(ctx context.Context, ui ui.UI, p *project.Project, name string, fun fun
wg := &sync.WaitGroup{}
wgs := make(map[string]*sync.WaitGroup)
errs := make(chan error)
ts, _ := graph.TopSort(name)
// Do loop twice to ensure we're setup
for _, t := range ts {
wgs[t] = &sync.WaitGroup{}
wgs[t].Add(1)
wg.Add(1)
}
sg := ui.NewSpinnerGroup()
for _, t := range ts {
go func(t string) {
defer func() {
wgs[t].Done()
wg.Done()
}()
te, ok := tasks[t]
if !ok {
return
}
// This waits for all the dependencies to be done.
// It will last the longest amount of time a dependency
// took to finish.
for _, dep := range deps[t] {
if dwg, ok := wgs[dep]; ok {
dwg.Wait()
bts, _ := graph.TopSort(name)
ets, _ := graph.TopSort(name + ":done")
// Do loop twice to ensure we're setup
for _, ts := range [][]string{bts, ets} {
for _, t := range ts {
wgs[t] = &sync.WaitGroup{}
wgs[t].Add(1)
wg.Add(1)
}
for _, t := range ts {
go func(t string) {
defer func() {
wgs[t].Done()
wg.Done()
}()
te, ok := tasks[t]
if !ok {
return
}
// This waits for all the dependencies to be done.
// It will last the longest amount of time a dependency
// took to finish.
for _, dep := range deps[t] {
if dwg, ok := wgs[dep]; ok {
dwg.Wait()
}
}
}
done, err := te.Check(ctx, p)
if err != nil {
errs <- err
return
}
e, b := te.Environ(ctx, p)
envs = append(envs, e...)
bins = append(bins, b...)
if !done {
s := sg.NewSpinner(te.Header())
if err := te.Resolve(ctx, p); err != nil {
s.Fail()
done, err := te.Check(ctx, p)
if err != nil {
errs <- err
return
} else {
done, err := te.Check(ctx, p)
if err != nil {
}
e, b := te.Environ(ctx, p)
envs = append(envs, e...)
bins = append(bins, b...)
if !done {
s := sg.NewSpinner(te.Header())
if err := te.Resolve(ctx, p); err != nil {
s.Fail()
errs <- err
} else if !done {
s.Fail()
errs <- fmt.Errorf("could not satisfy condition for %s", t)
return
} else {
done, err := te.Check(ctx, p)
if err != nil {
s.Fail()
errs <- err
} else if !done {
s.Fail()
errs <- fmt.Errorf("could not satisfy condition for %s", t)
}
}
s.Succeed()
}
s.Succeed()
}
}(t)
}
go func() {
// Wait for everyone to be done
wg.Wait()
// Set up environment
sg.Finish()
path := os.Getenv("PATH")
envs = append(envs, fmt.Sprintf("PATH=%s:%s", strings.Join(bins, ":"), path))
if err := fun(envs); err != nil {
errs <- err
}(t)
}
close(errs)
}()
// Wait for this particular group to be done. We want to wait for the
// "before done" things to be finished before doing the "done" things
// so that components like networking stop at the right time.
wg.Wait()
}
// Set up environment
sg.Finish()
path := os.Getenv("PATH")
envs = append(envs, fmt.Sprintf("PATH=%s:%s", strings.Join(bins, ":"), path))
if err := fun(envs); err != nil {
errs <- err
}
close(errs)
for {
select {
case err := <-errs:
sg.Finish()
return err
}
}
Expand Down
9 changes: 7 additions & 2 deletions spec/commands/down_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,13 @@
ip = project_ip
loon %(down)

expect(`ifconfig`).not_to include(ip)
ifconfig = if OS.linux?
`ip addr list lo`
else
`ifconfig lo0`
end

expect(ifconfig).not_to include(ip)
end
end
end

1 change: 1 addition & 0 deletions spec/spec_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
require 'tempfile'
require 'fileutils'
require 'open3'
require 'os'
require 'pathname'
require 'yaml'

Expand Down

0 comments on commit f4d524d

Please sign in to comment.