move goroutine call into the NewTask function

This commit is contained in:
Jesse Duffield 2019-06-15 14:51:09 +10:00
parent 97ce1f14d5
commit 383f8a51e3
6 changed files with 36 additions and 44 deletions

View file

@ -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()

View file

@ -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 {

View file

@ -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)
}

View file

@ -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 {

View file

@ -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 {

View file

@ -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