Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: change session print loop to block until all events are handled #2920

Merged
merged 3 commits into from
Oct 14, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions changelog.md
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
- Seal the capability keeper in the `app.go` template
- Change faucet to allow C.O.R.S. preflight requests.
- Fix config file migration to void leaving end of file content chunks
- Change session print loop to block until all events are handled.
- Handle "No records were found in keyring" message when checking keys.

### Features
Expand Down
15 changes: 12 additions & 3 deletions ignite/pkg/cliui/cliui.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"fmt"
"io"
"os"
"sync"

"github.com/manifoldco/promptui"

Expand All @@ -29,6 +30,7 @@ type Session struct {
ev events.Bus
spinner *clispinner.Spinner
out uilog.Output
wg *sync.WaitGroup
}

// Option configures session options.
Expand Down Expand Up @@ -73,6 +75,7 @@ func StartSpinner() Option {
func New(options ...Option) Session {
session := Session{
ev: events.NewBus(),
wg: &sync.WaitGroup{},
options: sessionOptions{
stdin: os.Stdin,
stdout: os.Stdout,
Expand All @@ -99,7 +102,10 @@ func New(options ...Option) Session {
session.spinner = clispinner.New(clispinner.WithWriter(session.out.Stdout()))
}

go session.handleEvents()
// The main loop that prints the events uses a wait group to block
// the session end until all the events are printed.
session.wg.Add(1)
go session.handleEvents(session.wg)

return session
}
Expand Down Expand Up @@ -215,9 +221,12 @@ func (s Session) PrintTable(header []string, entries ...[]string) error {
func (s Session) End() {
s.StopSpinner()
s.ev.Stop()
s.wg.Wait()
}

func (s Session) handleEvents() {
func (s Session) handleEvents(wg *sync.WaitGroup) {
defer wg.Done()

stdout := s.out.Stdout()

for e := range s.ev.Events() {
Expand All @@ -227,7 +236,7 @@ func (s Session) handleEvents() {
case events.IndicationFinish:
s.StopSpinner()
fmt.Fprintf(stdout, "%s\n", e)
case events.IndicationNone:
default:
resume := s.PauseSpinner()
fmt.Fprintf(stdout, "%s\n", e)
resume()
Expand Down
4 changes: 2 additions & 2 deletions ignite/services/network/publish.go
Original file line number Diff line number Diff line change
Expand Up @@ -307,11 +307,11 @@ func (n Network) sendAccountRequest(
}

if requestRes.AutoApproved {
n.ev.Send("Account added to the network by the coordinator!", events.ProgressStarted())
n.ev.Send("Account added to the network by the coordinator!", events.ProgressFinished())
} else {
n.ev.Send(
fmt.Sprintf("Request %d to add account to the network has been submitted!", requestRes.RequestID),
events.ProgressStarted(),
events.ProgressFinished(),
)
}
return nil
Expand Down