Skip to content

Commit

Permalink
catch ctl-c interrupt, log stack
Browse files Browse the repository at this point in the history
In all cases except watch commnad (which now handles ctrl-c) that is
sign of problem. Stack will tell us where we stuck.

Closes #63
  • Loading branch information
ianic committed Nov 4, 2021
1 parent f65cde2 commit 4a145cd
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 0 deletions.
7 changes: 7 additions & 0 deletions cli/controller/watch.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
package controller

import (
"os"
"os/signal"
"regexp"
"syscall"
"time"

"github.com/mantil-io/mantil/cli/log"
Expand Down Expand Up @@ -77,6 +80,8 @@ func runWatcher(onChange func(), path string) error {
r := regexp.MustCompile(`\.go$`)
w.AddFilterHook(watcher.RegexFilterHook(r, false))

ctrlc := make(chan os.Signal, 1)
signal.Notify(ctrlc, syscall.SIGINT)
go func() {
for {
select {
Expand All @@ -86,6 +91,8 @@ func runWatcher(onChange func(), path string) error {
ui.Error(err)
case <-w.Closed:
return
case <-ctrlc:
w.Close()
}
}
}()
Expand Down
14 changes: 14 additions & 0 deletions cli/log/log.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,12 @@
package log

import (
"bytes"
"fmt"
"io"
"log"
"os"
"runtime/pprof"
"strings"
"time"

Expand Down Expand Up @@ -97,6 +99,18 @@ func Printf(format string, v ...interface{}) {
logs.Output(2, fmt.Sprintf(format, v...))
}

func Signal(name string) {
if logFile == nil {
return
}
bb := bytes.NewBuffer(nil)
Printf("signal %s", name)
pprof.Lookup("goroutine").WriteTo(bb, 1)
buf := bb.Bytes()
logFile.Write(buf)
cliCommand.Add(domain.Event{Signal: &domain.Signal{Name: name, Stack: string(buf)}})
}

func PrintfWithCallDepth(calldepth int, format string, v ...interface{}) {
if logFile == nil {
return
Expand Down
17 changes: 17 additions & 0 deletions cli/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ package main
import (
"fmt"
"os"
"os/signal"
"syscall"

"github.com/mantil-io/mantil/cli/cmd"
"github.com/mantil-io/mantil/cli/log"
Expand Down Expand Up @@ -67,9 +69,24 @@ func run() error {
}
defer log.Close()
log.Printf("version: %s, args: %v", domain.Version(), os.Args)
if !isWatch() { // watch has it's own ctrlc handling
go catchInterupt()
}
if err := cmd.Execute(); err != nil {
log.Error(err)
return err
}
return nil
}

func isWatch() bool {
return len(os.Args) > 1 && os.Args[1] == "watch"
}

func catchInterupt() {
ctrlc := make(chan os.Signal, 1)
signal.Notify(ctrlc, syscall.SIGINT)
log.Signal((<-ctrlc).String())
log.Close()
os.Exit(1)
}
6 changes: 6 additions & 0 deletions domain/event.go
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@ type Event struct {
Timestamp int64 `short:"t,omitempty" json:"timestamp,omitempty"`
GoBuild *GoBuild `short:"g,omitempty" json:"goBuild,omitempty"`
Deploy *Deploy `short:"d,omitempty" json:"deploy,omitempty"`
Signal *Signal `short:"s,omitempty" json:"signal,omitempty"`
}

type GoBuild struct {
Expand All @@ -82,6 +83,11 @@ type Deploy struct {
UpdateDuration int64 `short:"d,omitempty" json:"updateDuration,omitempty"`
}

type Signal struct {
Name string `short:"n,omitempty" json:"name,omitempty"`
Stack string `short:"s,omitempty" json:"stack,omitempty"`
}

// marshal
var shortConfig = jsoniter.Config{
EscapeHTML: true,
Expand Down

0 comments on commit 4a145cd

Please sign in to comment.