mirror of
https://github.com/simon987/beemer.git
synced 2025-04-20 00:16:41 +00:00
Gracefully handle SIGINT
This commit is contained in:
parent
dc954bc85b
commit
1362aae61a
16
beemer.go
16
beemer.go
@ -6,6 +6,8 @@ import (
|
|||||||
"os"
|
"os"
|
||||||
"os/exec"
|
"os/exec"
|
||||||
"path/filepath"
|
"path/filepath"
|
||||||
|
"sync"
|
||||||
|
"syscall"
|
||||||
"time"
|
"time"
|
||||||
)
|
)
|
||||||
|
|
||||||
@ -16,6 +18,7 @@ type Beemer struct {
|
|||||||
beemChan chan string
|
beemChan chan string
|
||||||
watcher *fsnotify.Watcher
|
watcher *fsnotify.Watcher
|
||||||
inactiveDelay time.Duration
|
inactiveDelay time.Duration
|
||||||
|
globalWg *sync.WaitGroup
|
||||||
}
|
}
|
||||||
|
|
||||||
type File struct {
|
type File struct {
|
||||||
@ -132,12 +135,11 @@ func (b Beemer) handleWatcherEvents() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (b Beemer) work() {
|
func (b Beemer) work() {
|
||||||
for {
|
b.globalWg.Add(1)
|
||||||
select {
|
for name := range b.beemChan {
|
||||||
case name := <-b.beemChan:
|
|
||||||
b.beemFile(name)
|
b.beemFile(name)
|
||||||
}
|
}
|
||||||
}
|
b.globalWg.Done()
|
||||||
}
|
}
|
||||||
|
|
||||||
func (b Beemer) handleFileInactive(t *time.Timer, name string) {
|
func (b Beemer) handleFileInactive(t *time.Timer, name string) {
|
||||||
@ -159,6 +161,12 @@ func (b Beemer) beemFile(filename string) {
|
|||||||
name, args := b.beemCommand(newName, filepath.Dir(filename))
|
name, args := b.beemCommand(newName, filepath.Dir(filename))
|
||||||
|
|
||||||
cmd := exec.Command(name, args...)
|
cmd := exec.Command(name, args...)
|
||||||
|
|
||||||
|
// Don't send SIGINT to child processes
|
||||||
|
cmd.SysProcAttr = &syscall.SysProcAttr{
|
||||||
|
Setpgid: true,
|
||||||
|
}
|
||||||
|
|
||||||
out, err := cmd.CombinedOutput()
|
out, err := cmd.CombinedOutput()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
logrus.WithField("name", filename).WithError(err).Error(string(out))
|
logrus.WithField("name", filename).WithError(err).Error(string(out))
|
||||||
|
16
cli.go
16
cli.go
@ -6,10 +6,14 @@ import (
|
|||||||
"github.com/sirupsen/logrus"
|
"github.com/sirupsen/logrus"
|
||||||
"github.com/urfave/cli"
|
"github.com/urfave/cli"
|
||||||
"os"
|
"os"
|
||||||
|
"os/signal"
|
||||||
|
"sync"
|
||||||
|
"syscall"
|
||||||
"time"
|
"time"
|
||||||
)
|
)
|
||||||
|
|
||||||
func globalInit() {
|
func globalInit() {
|
||||||
|
//TODO cmdline flag
|
||||||
logrus.SetLevel(logrus.TraceLevel)
|
logrus.SetLevel(logrus.TraceLevel)
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -66,14 +70,13 @@ func main() {
|
|||||||
beemChan: make(chan string, transfers),
|
beemChan: make(chan string, transfers),
|
||||||
beemCommand: parseCommand(cmdString),
|
beemCommand: parseCommand(cmdString),
|
||||||
inactiveDelay: inactiveDelay,
|
inactiveDelay: inactiveDelay,
|
||||||
|
globalWg: &sync.WaitGroup{},
|
||||||
}
|
}
|
||||||
|
|
||||||
beemer.initTempDir()
|
beemer.initTempDir()
|
||||||
|
|
||||||
beemer.watcher, _ = fsnotify.NewWatcher()
|
beemer.watcher, _ = fsnotify.NewWatcher()
|
||||||
|
|
||||||
defer beemer.dispose()
|
|
||||||
|
|
||||||
go beemer.handleWatcherEvents()
|
go beemer.handleWatcherEvents()
|
||||||
|
|
||||||
beemer.initWatchDir(watchDir)
|
beemer.initWatchDir(watchDir)
|
||||||
@ -82,10 +85,13 @@ func main() {
|
|||||||
go beemer.work()
|
go beemer.work()
|
||||||
}
|
}
|
||||||
|
|
||||||
//TODO gracefully handle SIGINT
|
sigChan := make(chan os.Signal, 1)
|
||||||
done := make(chan bool)
|
signal.Notify(sigChan, syscall.SIGINT)
|
||||||
<-done
|
|
||||||
|
|
||||||
|
defer beemer.dispose()
|
||||||
|
|
||||||
|
<-sigChan
|
||||||
|
logrus.Info("Received SIGINT")
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
11
util.go
11
util.go
@ -108,8 +108,19 @@ func isDir(name string) bool {
|
|||||||
|
|
||||||
func (b Beemer) dispose() {
|
func (b Beemer) dispose() {
|
||||||
b.watcher.Close()
|
b.watcher.Close()
|
||||||
|
close(b.beemChan)
|
||||||
|
|
||||||
|
logrus.WithField("chanLen", len(b.beemChan)).Info("Waiting for beem queue to drain...")
|
||||||
|
<-b.beemChan
|
||||||
|
|
||||||
|
logrus.Info("Waiting for current commands to finish...")
|
||||||
|
b.globalWg.Wait()
|
||||||
|
|
||||||
|
logrus.Info("Cleaning up temp dir...")
|
||||||
err := os.RemoveAll(b.tempDir)
|
err := os.RemoveAll(b.tempDir)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
logrus.Fatal(err)
|
logrus.Fatal(err)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
logrus.Info("Goodbye.")
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user