Skip to content

Commit

Permalink
refactor: move signal capture to main.go
Browse files Browse the repository at this point in the history
  • Loading branch information
perangel committed Feb 12, 2019
1 parent 056a9ea commit 68fa3c2
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 17 deletions.
10 changes: 10 additions & 0 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,9 @@ import (
"fmt"
"log"
"os"
"os/signal"
"strconv"
"syscall"
"time"

"github.com/perangel/dtail/pkg/metrics"
Expand Down Expand Up @@ -127,6 +129,14 @@ func tailFile(cmd *cobra.Command, args []string) error {
Window: monitorAlertWindow,
})

shutdownCh := make(chan os.Signal, 1)
signal.Notify(shutdownCh, os.Interrupt, syscall.SIGTERM)
go func() {
<-shutdownCh
t.Stop()
requestRateMonitor.Stop()
}()

// TODO: Refactor this to pkg/dtail
go func() {

Expand Down
24 changes: 7 additions & 17 deletions pkg/tail/tail.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,6 @@ import (
"fmt"
"io"
"os"
"os/signal"
"syscall"
"time"

"github.com/fsnotify/fsnotify"
Expand All @@ -31,8 +29,7 @@ type Tail struct {
reader *bufio.Reader
watcher *fsnotify.Watcher

shutdownCh chan os.Signal
doneCh chan bool
doneCh chan bool
}

// waitForFile will attempt to os.Stat() a file every second until it is present
Expand Down Expand Up @@ -131,30 +128,23 @@ func (t *Tail) tail() {
<-t.doneCh
}

func (t *Tail) stop() error {
// Stop stops the tail and cleans up the channels
func (t *Tail) Stop() error {
err := t.watcher.Remove(t.file.Name())
close(t.Lines)
close(t.Errors)
close(t.shutdownCh)
t.doneCh <- true
return err
}

// TailFile configures a new Tail to follow the specified file.
func TailFile(filepath string, config *Config) (*Tail, error) {
t := &Tail{
Config: config,
Lines: make(chan string),
Errors: make(chan error),
shutdownCh: make(chan os.Signal, 1),
Config: config,
Lines: make(chan string),
Errors: make(chan error),
}

signal.Notify(t.shutdownCh, os.Interrupt, syscall.SIGTERM)
go func() {
<-t.shutdownCh
t.stop()
t.doneCh <- true
}()

err := t.openFile(filepath)
if err != nil {
return nil, err
Expand Down

0 comments on commit 68fa3c2

Please sign in to comment.