Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 2de56c0

Browse files
authored
force UI teardown when event is sourced from a signal interrupt (anchore#453)
Signed-off-by: Alex Goodman <[email protected]>
1 parent fb0857f commit 2de56c0

File tree

5 files changed

+14
-10
lines changed

5 files changed

+14
-10
lines changed

cmd/event_loop.go

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ import (
1313
// eventLoop listens to worker errors (from execution path), worker events (from a partybus subscription), and
1414
// signal interrupts. Is responsible for handling each event relative to a given UI an to coordinate eventing until
1515
// an eventual graceful exit.
16-
// nolint:gocognit
16+
// nolint:gocognit,funlen
1717
func eventLoop(workerErrs <-chan error, signals <-chan os.Signal, subscription *partybus.Subscription, ux ui.UI, cleanupFn func()) error {
1818
defer cleanupFn()
1919
events := subscription.Events()
@@ -23,6 +23,7 @@ func eventLoop(workerErrs <-chan error, signals <-chan os.Signal, subscription *
2323
}
2424

2525
var retErr error
26+
var forceTeardown bool
2627

2728
for {
2829
if workerErrs == nil && events == nil {
@@ -66,10 +67,11 @@ func eventLoop(workerErrs <-chan error, signals <-chan os.Signal, subscription *
6667
// of processing.
6768
events = nil
6869
workerErrs = nil
70+
forceTeardown = true
6971
}
7072
}
7173

72-
if err := ux.Teardown(); err != nil {
74+
if err := ux.Teardown(forceTeardown); err != nil {
7375
retErr = multierror.Append(retErr, err)
7476
}
7577

cmd/event_loop_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ func (u *uiMock) Handle(event partybus.Event) error {
3737
return u.Called(event).Error(0)
3838
}
3939

40-
func (u *uiMock) Teardown() error {
40+
func (u *uiMock) Teardown(_ bool) error {
4141
u.t.Logf("UI Teardown called")
4242
return u.Called().Error(0)
4343
}

internal/ui/ephemeral_terminal_ui.go

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@ func (h *ephemeralTerminalUI) Handle(event partybus.Event) error {
7777
case event.Type == syftEvent.PresenterReady:
7878
// we need to close the screen now since signaling the the presenter is ready means that we
7979
// are about to write bytes to stdout, so we should reset the terminal state first
80-
h.closeScreen()
80+
h.closeScreen(false)
8181

8282
if err := handleCatalogerPresenterReady(event); err != nil {
8383
log.Errorf("unable to show %s event: %+v", event.Type, err)
@@ -105,11 +105,13 @@ func (h *ephemeralTerminalUI) openScreen() error {
105105
return nil
106106
}
107107

108-
func (h *ephemeralTerminalUI) closeScreen() {
108+
func (h *ephemeralTerminalUI) closeScreen(force bool) {
109109
// we may have other background processes still displaying progress, wait for them to
110110
// finish before discontinuing dynamic content and showing the final report
111111
if !h.frame.IsClosed() {
112-
h.waitGroup.Wait()
112+
if !force {
113+
h.waitGroup.Wait()
114+
}
113115
h.frame.Close()
114116
// TODO: there is a race condition within frame.Close() that sometimes leads to an extra blank line being output
115117
frame.Close()
@@ -130,8 +132,8 @@ func (h *ephemeralTerminalUI) flushLog() {
130132
}
131133
}
132134

133-
func (h *ephemeralTerminalUI) Teardown() error {
134-
h.closeScreen()
135+
func (h *ephemeralTerminalUI) Teardown(force bool) error {
136+
h.closeScreen(force)
135137
ansi.CursorShow()
136138
return nil
137139
}

internal/ui/logger_ui.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,6 @@ func (l loggerUI) Handle(event partybus.Event) error {
3333
return l.unsubscribe()
3434
}
3535

36-
func (l loggerUI) Teardown() error {
36+
func (l loggerUI) Teardown(_ bool) error {
3737
return nil
3838
}

internal/ui/ui.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,5 +7,5 @@ import (
77
type UI interface {
88
Setup(unsubscribe func() error) error
99
partybus.Handler
10-
Teardown() error
10+
Teardown(force bool) error
1111
}

0 commit comments

Comments
 (0)