From 383f8a51e38a14622f4568579ce8c3d868affbd5 Mon Sep 17 00:00:00 2001 From: Jesse Duffield Date: Sat, 15 Jun 2019 14:51:09 +1000 Subject: [PATCH] move goroutine call into the NewTask function --- pkg/gui/containers_panel.go | 16 +++++----------- pkg/gui/images_panel.go | 4 +--- pkg/gui/services_panel.go | 7 +++++-- pkg/gui/status_panel.go | 12 +++--------- pkg/gui/volumes_panel.go | 4 +--- pkg/tasks/tasks.go | 37 +++++++++++++++++++++---------------- 6 files changed, 36 insertions(+), 44 deletions(-) diff --git a/pkg/gui/containers_panel.go b/pkg/gui/containers_panel.go index ad1d4608..3f684c3e 100644 --- a/pkg/gui/containers_panel.go +++ b/pkg/gui/containers_panel.go @@ -122,11 +122,9 @@ func (gui *Gui) renderContainerConfig(container *commands.Container) error { return err } - go gui.T.NewTask(func(stop chan struct{}) { + return gui.T.NewTask(func(stop chan struct{}) { gui.renderString(gui.g, "main", string(data)) }) - - return nil } func (gui *Gui) renderContainerStats(container *commands.Container) error { @@ -173,17 +171,15 @@ func (gui *Gui) renderContainerLogs(container *commands.Container) error { } func (gui *Gui) renderLogsForRegularContainer(container *commands.Container) error { - gui.renderLogs(container, gui.Config.UserConfig.CommandTemplates.ContainerLogs, func(cmd *exec.Cmd) { + return gui.renderLogs(container, gui.Config.UserConfig.CommandTemplates.ContainerLogs, func(cmd *exec.Cmd) { mainView := gui.getMainView() cmd.Stdout = mainView cmd.Stderr = mainView }) - - return nil } func (gui *Gui) renderLogsForTTYContainer(container *commands.Container) error { - gui.renderLogs(container, gui.Config.UserConfig.CommandTemplates.ContainerTTYLogs, func(cmd *exec.Cmd) { + return gui.renderLogs(container, gui.Config.UserConfig.CommandTemplates.ContainerTTYLogs, func(cmd *exec.Cmd) { // for some reason just saying cmd.Stdout = mainView does not work here as it does for non-tty containers, so we feed it through line by line r, err := cmd.StdoutPipe() if err != nil { @@ -200,12 +196,10 @@ func (gui *Gui) renderLogsForTTYContainer(container *commands.Container) error { } }() }) - - return nil } -func (gui *Gui) renderLogs(container *commands.Container, template string, setup func(*exec.Cmd)) { - gui.T.NewTickerTask(time.Millisecond*200, nil, func(stop, notifyStopped chan struct{}) { +func (gui *Gui) renderLogs(container *commands.Container, template string, setup func(*exec.Cmd)) error { + return gui.T.NewTickerTask(time.Millisecond*200, nil, func(stop, notifyStopped chan struct{}) { gui.clearMainView() cmd := container.TTYLogsCommand() diff --git a/pkg/gui/images_panel.go b/pkg/gui/images_panel.go index 1dabb5c7..7179e702 100644 --- a/pkg/gui/images_panel.go +++ b/pkg/gui/images_panel.go @@ -97,7 +97,7 @@ func (gui *Gui) handleImageSelect(g *gocui.Gui, v *gocui.View) error { } func (gui *Gui) renderImageConfig(mainView *gocui.View, image *commands.Image) error { - go gui.T.NewTask(func(stop chan struct{}) { + return gui.T.NewTask(func(stop chan struct{}) { padding := 10 output := "" output += utils.WithPadding("Name: ", padding) + image.Name + "\n" @@ -118,8 +118,6 @@ func (gui *Gui) renderImageConfig(mainView *gocui.View, image *commands.Image) e gui.renderString(gui.g, "main", output) }) - - return nil } func (gui *Gui) refreshImages() error { diff --git a/pkg/gui/services_panel.go b/pkg/gui/services_panel.go index 1e46456e..b2dfb5e6 100644 --- a/pkg/gui/services_panel.go +++ b/pkg/gui/services_panel.go @@ -137,12 +137,15 @@ func (gui *Gui) renderServiceLogs(service *commands.Service) error { } if service.Container == nil { - go gui.T.NewTask(func(stop chan struct{}) { + gui.Log.Warn("container is nil") + return gui.T.NewTask(func(stop chan struct{}) { + gui.Log.Warn("about to clear main view") gui.clearMainView() + gui.Log.Warn("cleared main view") }) - return nil } + gui.Log.Warn("about to render container logs for service ", service.Name) return gui.renderContainerLogs(service.Container) } diff --git a/pkg/gui/status_panel.go b/pkg/gui/status_panel.go index f10525b7..306e8cf2 100644 --- a/pkg/gui/status_panel.go +++ b/pkg/gui/status_panel.go @@ -79,7 +79,7 @@ func (gui *Gui) handleStatusSelect(g *gocui.Gui, v *gocui.View) error { } func (gui *Gui) renderCredits() error { - go gui.T.NewTask(func(stop chan struct{}) { + return gui.T.NewTask(func(stop chan struct{}) { mainView := gui.getMainView() mainView.Autoscroll = false mainView.Wrap = true @@ -95,12 +95,10 @@ func (gui *Gui) renderCredits() error { gui.renderString(gui.g, "main", dashboardString) }) - - return nil } func (gui *Gui) renderAllLogs() error { - go gui.T.NewTask(func(stop chan struct{}) { + return gui.T.NewTask(func(stop chan struct{}) { mainView := gui.getMainView() mainView.Autoscroll = true mainView.Wrap = true @@ -129,12 +127,10 @@ func (gui *Gui) renderAllLogs() error { cmd.Wait() }) - - return nil } func (gui *Gui) renderDockerComposeConfig() error { - go gui.T.NewTask(func(stop chan struct{}) { + return gui.T.NewTask(func(stop chan struct{}) { mainView := gui.getMainView() mainView.Autoscroll = false mainView.Wrap = true @@ -142,8 +138,6 @@ func (gui *Gui) renderDockerComposeConfig() error { config := gui.DockerCommand.DockerComposeConfig() gui.renderString(gui.g, "main", config) }) - - return nil } func (gui *Gui) handleOpenConfig(g *gocui.Gui, v *gocui.View) error { diff --git a/pkg/gui/volumes_panel.go b/pkg/gui/volumes_panel.go index 88e94092..0532ebb0 100644 --- a/pkg/gui/volumes_panel.go +++ b/pkg/gui/volumes_panel.go @@ -95,7 +95,7 @@ func (gui *Gui) handleVolumeSelect(g *gocui.Gui, v *gocui.View) error { } func (gui *Gui) renderVolumeConfig(mainView *gocui.View, volume *commands.Volume) error { - go gui.T.NewTask(func(stop chan struct{}) { + return gui.T.NewTask(func(stop chan struct{}) { mainView.Autoscroll = false mainView.Wrap = true @@ -142,8 +142,6 @@ func (gui *Gui) renderVolumeConfig(mainView *gocui.View, volume *commands.Volume gui.renderString(gui.g, "main", output) }) - - return nil } func formatMapItem(padding int, k string, v interface{}) string { diff --git a/pkg/tasks/tasks.go b/pkg/tasks/tasks.go index 81abf8dc..9519eeed 100644 --- a/pkg/tasks/tasks.go +++ b/pkg/tasks/tasks.go @@ -25,25 +25,30 @@ func NewTaskManager(log *logrus.Entry) *TaskManager { } func (t *TaskManager) NewTask(f func(stop chan struct{})) error { - t.waitingMutex.Lock() - defer t.waitingMutex.Unlock() - if t.currentTask != nil { - t.currentTask.Stop() - } - - stop := make(chan struct{}, 1) // we don't want to block on this in case the task already returned - notifyStopped := make(chan struct{}) - - t.currentTask = &Task{ - stop: stop, - notifyStopped: notifyStopped, - Log: t.Log, - } + // TODO: topple the previous task if there is a new one go func() { - f(stop) - close(notifyStopped) + t.waitingMutex.Lock() + defer t.waitingMutex.Unlock() + + if t.currentTask != nil { + t.currentTask.Stop() + } + + stop := make(chan struct{}, 1) // we don't want to block on this in case the task already returned + notifyStopped := make(chan struct{}) + + t.currentTask = &Task{ + stop: stop, + notifyStopped: notifyStopped, + Log: t.Log, + } + + go func() { + f(stop) + close(notifyStopped) + }() }() return nil