Skip to content

Commit

Permalink
Wait for process completion on SIGTERM
Browse files Browse the repository at this point in the history
  • Loading branch information
F1bonacc1 committed Jan 31, 2023
1 parent db93aa4 commit 72f8e35
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 6 deletions.
13 changes: 12 additions & 1 deletion src/app/project_runner.go
Original file line number Diff line number Diff line change
Expand Up @@ -241,9 +241,20 @@ func (p *ProjectRunner) ShutDownProject() {
for _, proc := range runProc {
proc.prepareForShutDown()
}
wg := sync.WaitGroup{}
for _, proc := range runProc {
_ = proc.shutDown()
err := proc.shutDown()
if err != nil {
log.Err(err).Msgf("failed to shutdown %s", proc.getName())
continue
}
wg.Add(1)
go func(pr *Process) {
pr.waitForCompletion()
wg.Done()
}(proc)
}
wg.Wait()
}

func (p *ProjectRunner) getProcessLog(name string) (*pclog.ProcessLogBuffer, error) {
Expand Down
2 changes: 1 addition & 1 deletion src/cmd/project_runner.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ func runProject(process []string, noDeps bool) {

func setSignal(signalHandler func()) {
cancelChan := make(chan os.Signal, 1)
signal.Notify(cancelChan, syscall.SIGTERM, syscall.SIGINT, syscall.SIGHUP)
signal.Notify(cancelChan, syscall.SIGTERM, os.Interrupt, syscall.SIGHUP)
go func() {
sig := <-cancelChan
log.Info().Msgf("Caught %v - Shutting down the running processes...", sig)
Expand Down
11 changes: 7 additions & 4 deletions src/pclog/logger_facade.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ type PCLog struct {
file *os.File
logEventChan chan logEvent
wg sync.WaitGroup
closer sync.Once
}

type logEvent struct {
Expand Down Expand Up @@ -73,10 +74,12 @@ func (l *PCLog) error(message string, process string, replica int) {
}

func (l *PCLog) Close() {
close(l.logEventChan)
l.wg.Wait()
l.writer.Flush()
l.file.Close()
l.closer.Do(func() {
close(l.logEventChan)
l.wg.Wait()
l.writer.Flush()
l.file.Close()
})
}

func (l *PCLog) runCollector() {
Expand Down

0 comments on commit 72f8e35

Please sign in to comment.