2018-07-12 23:15:33 +00:00
|
|
|
package taskrunner
|
|
|
|
|
2018-07-16 21:37:27 +00:00
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"os"
|
2018-07-12 23:15:33 +00:00
|
|
|
|
2018-07-16 21:37:27 +00:00
|
|
|
"github.com/hashicorp/nomad/nomad/structs"
|
|
|
|
)
|
|
|
|
|
|
|
|
func (tr *TaskRunner) Restart(ctx context.Context, event *structs.TaskEvent, failure bool) error {
|
|
|
|
// Grab the handle
|
2018-07-30 23:37:26 +00:00
|
|
|
handle := tr.getDriverHandle()
|
2018-07-16 21:37:27 +00:00
|
|
|
|
|
|
|
// Check it is running
|
|
|
|
if handle == nil {
|
|
|
|
return ErrTaskNotRunning
|
|
|
|
}
|
|
|
|
|
2018-09-11 03:24:53 +00:00
|
|
|
// Emit the event since it may take a long time to kill
|
|
|
|
tr.EmitEvent(event)
|
2018-07-16 21:37:27 +00:00
|
|
|
|
|
|
|
// Tell the restart tracker that a restart triggered the exit
|
|
|
|
tr.restartTracker.SetRestartTriggered(failure)
|
|
|
|
|
|
|
|
// Kill the task using an exponential backoff in-case of failures.
|
|
|
|
destroySuccess, err := tr.handleDestroy(handle)
|
|
|
|
if !destroySuccess {
|
|
|
|
// We couldn't successfully destroy the resource created.
|
|
|
|
tr.logger.Error("failed to kill task. Resources may have been leaked", "error", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Drain the wait channel or wait for the request context to be cancelled
|
|
|
|
select {
|
|
|
|
case <-handle.WaitCh():
|
|
|
|
case <-ctx.Done():
|
|
|
|
return ctx.Err()
|
|
|
|
}
|
2018-07-12 23:15:33 +00:00
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2018-07-16 21:37:27 +00:00
|
|
|
func (tr *TaskRunner) Signal(event *structs.TaskEvent, s os.Signal) error {
|
|
|
|
// Grab the handle
|
2018-07-30 23:37:26 +00:00
|
|
|
handle := tr.getDriverHandle()
|
2018-07-16 21:37:27 +00:00
|
|
|
|
|
|
|
// Check it is running
|
|
|
|
if handle == nil {
|
|
|
|
return ErrTaskNotRunning
|
|
|
|
}
|
|
|
|
|
|
|
|
// Emit the event
|
|
|
|
tr.EmitEvent(event)
|
|
|
|
|
|
|
|
// Send the signal
|
|
|
|
return handle.Signal(s)
|
|
|
|
}
|
|
|
|
|
2018-08-23 19:03:17 +00:00
|
|
|
// Kill a task. Blocks until task exits or context is canceled. State is set to
|
|
|
|
// dead.
|
2018-07-16 21:37:27 +00:00
|
|
|
func (tr *TaskRunner) Kill(ctx context.Context, event *structs.TaskEvent) error {
|
|
|
|
// Grab the handle
|
2018-07-30 23:37:26 +00:00
|
|
|
handle := tr.getDriverHandle()
|
2018-07-16 21:37:27 +00:00
|
|
|
|
|
|
|
// Check if the handle is running
|
|
|
|
if handle == nil {
|
|
|
|
return ErrTaskNotRunning
|
|
|
|
}
|
|
|
|
|
2018-09-11 03:24:53 +00:00
|
|
|
// Emit the event since it may take a long time to kill
|
|
|
|
tr.EmitEvent(event)
|
2018-07-16 21:37:27 +00:00
|
|
|
|
|
|
|
// Run the hooks prior to killing the task
|
|
|
|
tr.kill()
|
|
|
|
|
|
|
|
// Tell the restart tracker that the task has been killed
|
|
|
|
tr.restartTracker.SetKilled()
|
|
|
|
|
|
|
|
// Kill the task using an exponential backoff in-case of failures.
|
|
|
|
destroySuccess, destroyErr := tr.handleDestroy(handle)
|
|
|
|
if !destroySuccess {
|
|
|
|
// We couldn't successfully destroy the resource created.
|
|
|
|
tr.logger.Error("failed to kill task. Resources may have been leaked", "error", destroyErr)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Drain the wait channel or wait for the request context to be cancelled
|
|
|
|
select {
|
|
|
|
case <-handle.WaitCh():
|
|
|
|
case <-ctx.Done():
|
|
|
|
}
|
|
|
|
|
|
|
|
// Store that the task has been destroyed and any associated error.
|
2018-09-11 00:34:45 +00:00
|
|
|
tr.UpdateState(structs.TaskStateDead, structs.NewTaskEvent(structs.TaskKilled).SetKillError(destroyErr))
|
2018-07-16 21:37:27 +00:00
|
|
|
|
|
|
|
if destroyErr != nil {
|
|
|
|
return destroyErr
|
|
|
|
} else if err := ctx.Err(); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
2018-07-12 23:15:33 +00:00
|
|
|
}
|