Skip to content

Commit

Permalink
Graceful shutdown (#690)
Browse files Browse the repository at this point in the history
refactor: Graceful shutdown. Exiting cluster, then waiting for all ongoing jobs to finish before dying.

Co-authored-by: Andrey Golev <[email protected]>
  • Loading branch information
andreygolev and Andrey Golev authored Mar 6, 2020
1 parent 1eb623c commit 43c994f
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 4 deletions.
20 changes: 16 additions & 4 deletions cmd/agent.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ var agent *dkron.Agent

const (
// gracefulTimeout controls how long we wait before forcefully terminating
gracefulTimeout = 3 * time.Second
gracefulTimeout = 3 * time.Hour
)

// agentCmd represents the agent command
Expand Down Expand Up @@ -110,18 +110,30 @@ WAIT:
}

// Attempt a graceful leave
gracefulCh := make(chan struct{})
log.Info("agent: Gracefully shutting down agent...")
go func() {
plugin.CleanupClients()
if err := agent.Stop(); err != nil {
fmt.Printf("Error: %s", err)
log.Error(fmt.Sprintf("Error: %s", err))
return
}
close(gracefulCh)
}()

gracefulCh := make(chan struct{})

for {
log.Info("Waiting for jobs to finish...")
if agent.GetRunningJobs() < 1 {
log.Info("No jobs left. Exiting.")
break
}
time.Sleep(1 * time.Second)
}

plugin.CleanupClients()

close(gracefulCh)

// Wait for leave or another signal
select {
case <-signalCh:
Expand Down
10 changes: 10 additions & 0 deletions dkron/agent.go
Original file line number Diff line number Diff line change
Expand Up @@ -881,3 +881,13 @@ func (a *Agent) applySetJob(job *proto.Job) error {
func (a *Agent) RaftApply(cmd []byte) raft.ApplyFuture {
return a.raft.Apply(cmd, raftTimeout)
}

// GetRunningJobs returns amount of active jobs
func (a *Agent) GetRunningJobs() int {
job := 0
runningExecutions.Range(func(k, v interface{}) bool {
job = job + 1
return true
})
return job
}

0 comments on commit 43c994f

Please sign in to comment.