tr: pass context to hooks

This commit is contained in:
Michael Schurter
2018-07-11 15:42:35 -07:00
parent d3d590f175
commit eea820ecf1
2 changed files with 12 additions and 8 deletions

View File

@@ -1,6 +1,8 @@
package interfaces
import (
"context"
"github.com/hashicorp/nomad/client/driver/env"
"github.com/hashicorp/nomad/nomad/structs"
)
@@ -49,7 +51,7 @@ type TaskPrerunResponse struct {
type TaskPrerunHook interface {
TaskHook
Prerun(*TaskPrerunRequest, *TaskPrerunResponse) error
Prerun(context.Context, *TaskPrerunRequest, *TaskPrerunResponse) error
}
// XXX If we want consul style hooks, need to have something that runs after the
@@ -62,7 +64,7 @@ type TaskPostrunResponse struct{}
type TaskPostrunHook interface {
TaskHook
Postrun() error
//Postrun(*TaskPostrunRequest, *TaskPostrunResponse) error
//Postrun(context.Context, *TaskPostrunRequest, *TaskPostrunResponse) error
}
type TaskPoststopRequest struct{}
@@ -71,7 +73,7 @@ type TaskPoststopResponse struct{}
type TaskPoststopHook interface {
TaskHook
Postrun() error
//Postrun(*TaskPostrunRequest, *TaskPostrunResponse) error
//Postrun(context.Context, *TaskPostrunRequest, *TaskPostrunResponse) error
}
type TaskDestroyRequest struct{}
@@ -80,7 +82,7 @@ type TaskDestroyResponse struct{}
type TaskDestroyHook interface {
TaskHook
Destroy() error
//Destroy(*TaskDestroyRequest, *TaskDestroyResponse) error
//Destroy(context.Context, *TaskDestroyRequest, *TaskDestroyResponse) error
}
type TaskUpdateRequest struct {
@@ -92,5 +94,5 @@ type TaskUpdateResponse struct{}
type TaskUpdateHook interface {
TaskHook
Update() error
//Update(*TaskUpdateRequest, *TaskUpdateResponse) error
//Update(context.Context, *TaskUpdateRequest, *TaskUpdateResponse) error
}

View File

@@ -1,6 +1,7 @@
package taskrunner
import (
"context"
"fmt"
"time"
@@ -79,7 +80,7 @@ func (tr *TaskRunner) prerun() error {
// Run the pre-run hook
var resp interfaces.TaskPrerunResponse
if err := pre.Prerun(&req, &resp); err != nil {
if err := pre.Prerun(tr.ctx, &req, &resp); err != nil {
return structs.WrapRecoverable(fmt.Sprintf("pre-run hook %q failed: %v", name, err), err)
}
@@ -211,7 +212,7 @@ func (h *taskDirHook) Name() string {
return "task_dir"
}
func (h *taskDirHook) Prerun(req *interfaces.TaskPrerunRequest, resp *interfaces.TaskPrerunResponse) error {
func (h *taskDirHook) Prerun(ctx context.Context, req *interfaces.TaskPrerunRequest, resp *interfaces.TaskPrerunResponse) error {
cc := h.runner.clientConfig
chroot := cconfig.DefaultChrootEnv
if len(cc.ChrootEnv) > 0 {
@@ -256,10 +257,11 @@ func (*artifactHook) Name() string {
return "artifacts"
}
func (h *artifactHook) Prerun(req *interfaces.TaskPrerunRequest, resp *interfaces.TaskPrerunResponse) error {
func (h *artifactHook) Prerun(ctx context.Context, req *interfaces.TaskPrerunRequest, resp *interfaces.TaskPrerunResponse) error {
h.eventEmitter.SetState(structs.TaskStatePending, structs.NewTaskEvent(structs.TaskDownloadingArtifacts))
for _, artifact := range req.Task.Artifacts {
//XXX add ctx to GetArtifact to allow cancelling long downloads
if err := getter.GetArtifact(req.TaskEnv, artifact, req.TaskDir); err != nil {
wrapped := fmt.Errorf("failed to download artifact %q: %v", artifact.GetterSource, err)
h.logger.Debug(wrapped.Error())