Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Graceful shutdown #690

Merged
merged 3 commits into from
Mar 6, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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()
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

When is this going to happen?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I've tested it only with containers. Container exits, and the everything that was running inside dies also. So it doesn't matter in my case :)
But you're right, not everyone is using containers.
I'll add plugin.CleanupClients() somewhere below

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
}