gracefully shutdown agent when builds running

This commit is contained in:
Brad Rydzewski 2016-07-18 00:06:34 -07:00
parent 71de0d9408
commit 88df2520c8
2 changed files with 27 additions and 0 deletions

View file

@ -1,7 +1,10 @@
package agent
import (
"os"
"os/signal"
"sync"
"syscall"
"time"
"github.com/drone/drone/client"
@ -201,5 +204,25 @@ func start(c *cli.Context) {
}
}()
}
handleSignals()
wg.Wait()
}
// tracks running builds
var running sync.WaitGroup
func handleSignals() {
// Graceful shut-down on SIGINT/SIGTERM
c := make(chan os.Signal, 1)
signal.Notify(c, os.Interrupt)
signal.Notify(c, syscall.SIGTERM)
go func() {
<-c
logrus.Debugln("SIGTERM received.")
logrus.Debugln("wait for running builds to finish.")
running.Wait()
logrus.Debugln("done.")
os.Exit(0)
}()
}

View file

@ -33,6 +33,10 @@ func (r *pipeline) run() error {
if err != nil {
return err
}
running.Add(1)
defer func() {
running.Done()
}()
logrus.Infof("Starting build %s/%s#%d.%d",
w.Repo.Owner, w.Repo.Name, w.Build.Number, w.Job.Number)