mirror of
https://github.com/jesseduffield/lazydocker.git
synced 2026-07-25 08:31:03 +00:00
Merge pull request #148 from jesseduffield/147_child_processes
ensure the current task is stopped before exit
This commit is contained in:
commit
54609a5d99
3 changed files with 40 additions and 3 deletions
|
|
@ -163,7 +163,7 @@ func NewGui(log *logrus.Entry, dockerCommand *commands.DockerCommand, oSCommand
|
||||||
Config: config,
|
Config: config,
|
||||||
Tr: tr,
|
Tr: tr,
|
||||||
statusManager: &statusManager{},
|
statusManager: &statusManager{},
|
||||||
T: tasks.NewTaskManager(log),
|
T: tasks.NewTaskManager(log, tr),
|
||||||
ErrorChan: errorChan,
|
ErrorChan: errorChan,
|
||||||
CyclableViews: cyclableViews,
|
CyclableViews: cyclableViews,
|
||||||
}
|
}
|
||||||
|
|
@ -247,6 +247,9 @@ func (gui *Gui) goEvery(interval time.Duration, function func() error) {
|
||||||
|
|
||||||
// Run setup the gui with keybindings and start the mainloop
|
// Run setup the gui with keybindings and start the mainloop
|
||||||
func (gui *Gui) Run() error {
|
func (gui *Gui) Run() error {
|
||||||
|
// closing our task manager which in turn closes the current task if there is any, so we aren't leaving processes lying around after closing lazydocker
|
||||||
|
defer gui.T.Close()
|
||||||
|
|
||||||
g, err := gocui.NewGui(gocui.OutputNormal, OverlappingEdges)
|
g, err := gocui.NewGui(gocui.OutputNormal, OverlappingEdges)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
|
|
|
||||||
|
|
@ -24,6 +24,7 @@ type TranslationSet struct {
|
||||||
UnattachableContainerError string
|
UnattachableContainerError string
|
||||||
CannotAttachStoppedContainerError string
|
CannotAttachStoppedContainerError string
|
||||||
CannotAccessDockerSocketError string
|
CannotAccessDockerSocketError string
|
||||||
|
CannotKillChildError string
|
||||||
|
|
||||||
Donate string
|
Donate string
|
||||||
Cancel string
|
Cancel string
|
||||||
|
|
@ -110,6 +111,7 @@ func englishSet() TranslationSet {
|
||||||
UnattachableContainerError: "Container does not support attaching. You must either run the service with the '-it' flag or use `stdin_open: true, tty: true` in the docker-compose.yml file",
|
UnattachableContainerError: "Container does not support attaching. You must either run the service with the '-it' flag or use `stdin_open: true, tty: true` in the docker-compose.yml file",
|
||||||
CannotAttachStoppedContainerError: "You cannot attach to a stopped container, you need to start it first (which you can actually do with the 'r' key) (yes I'm too lazy to do this automatically for you) (pretty cool that I get to communicate one-on-one with you in the form of an error message though)",
|
CannotAttachStoppedContainerError: "You cannot attach to a stopped container, you need to start it first (which you can actually do with the 'r' key) (yes I'm too lazy to do this automatically for you) (pretty cool that I get to communicate one-on-one with you in the form of an error message though)",
|
||||||
CannotAccessDockerSocketError: "Can't access docker socket at: unix:///var/run/docker.sock\nRun lazydocker as root or read https://docs.docker.com/install/linux/linux-postinstall/",
|
CannotAccessDockerSocketError: "Can't access docker socket at: unix:///var/run/docker.sock\nRun lazydocker as root or read https://docs.docker.com/install/linux/linux-postinstall/",
|
||||||
|
CannotKillChildError: "Waited three seconds for child process to stop. There may be an orphan process that continues to run on your system.",
|
||||||
|
|
||||||
Donate: "Donate",
|
Donate: "Donate",
|
||||||
Confirm: "Confirm",
|
Confirm: "Confirm",
|
||||||
|
|
|
||||||
|
|
@ -1,9 +1,11 @@
|
||||||
package tasks
|
package tasks
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"fmt"
|
||||||
"sync"
|
"sync"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
|
"github.com/jesseduffield/lazydocker/pkg/i18n"
|
||||||
"github.com/sirupsen/logrus"
|
"github.com/sirupsen/logrus"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
@ -13,19 +15,43 @@ type TaskManager struct {
|
||||||
waitingMutex sync.Mutex
|
waitingMutex sync.Mutex
|
||||||
taskIDMutex sync.Mutex
|
taskIDMutex sync.Mutex
|
||||||
Log *logrus.Entry
|
Log *logrus.Entry
|
||||||
|
Tr *i18n.TranslationSet
|
||||||
waitingTaskAlerts chan struct{}
|
waitingTaskAlerts chan struct{}
|
||||||
newTaskId int
|
newTaskId int
|
||||||
}
|
}
|
||||||
|
|
||||||
type Task struct {
|
type Task struct {
|
||||||
stop chan struct{}
|
stop chan struct{}
|
||||||
|
stopped bool
|
||||||
|
stopMutex sync.Mutex
|
||||||
notifyStopped chan struct{}
|
notifyStopped chan struct{}
|
||||||
Log *logrus.Entry
|
Log *logrus.Entry
|
||||||
f func(chan struct{})
|
f func(chan struct{})
|
||||||
}
|
}
|
||||||
|
|
||||||
func NewTaskManager(log *logrus.Entry) *TaskManager {
|
func NewTaskManager(log *logrus.Entry, translationSet *i18n.TranslationSet) *TaskManager {
|
||||||
return &TaskManager{Log: log}
|
return &TaskManager{Log: log, Tr: translationSet}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Close closes the task manager, killing whatever task may currently be running
|
||||||
|
func (t *TaskManager) Close() {
|
||||||
|
if t.currentTask == nil {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
c := make(chan struct{}, 1)
|
||||||
|
|
||||||
|
go func() {
|
||||||
|
t.currentTask.Stop()
|
||||||
|
c <- struct{}{}
|
||||||
|
}()
|
||||||
|
|
||||||
|
select {
|
||||||
|
case <-c:
|
||||||
|
return
|
||||||
|
case <-time.After(3 * time.Second):
|
||||||
|
fmt.Println(t.Tr.CannotKillChildError)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (t *TaskManager) NewTask(f func(stop chan struct{})) error {
|
func (t *TaskManager) NewTask(f func(stop chan struct{})) error {
|
||||||
|
|
@ -68,10 +94,16 @@ func (t *TaskManager) NewTask(f func(stop chan struct{})) error {
|
||||||
}
|
}
|
||||||
|
|
||||||
func (t *Task) Stop() {
|
func (t *Task) Stop() {
|
||||||
|
t.stopMutex.Lock()
|
||||||
|
defer t.stopMutex.Unlock()
|
||||||
|
if t.stopped {
|
||||||
|
return
|
||||||
|
}
|
||||||
close(t.stop)
|
close(t.stop)
|
||||||
t.Log.Info("closed stop channel, waiting for notifyStopped message")
|
t.Log.Info("closed stop channel, waiting for notifyStopped message")
|
||||||
<-t.notifyStopped
|
<-t.notifyStopped
|
||||||
t.Log.Info("received notifystopped message")
|
t.Log.Info("received notifystopped message")
|
||||||
|
t.stopped = true
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Add table
Reference in a new issue