github.com/zoomfoo/nomad@v0.8.5-0.20180907175415-f28fd3a1a056/nomad/plan_endpoint.go (about)

     1  package nomad
     2  
     3  import (
     4  	"time"
     5  
     6  	"github.com/armon/go-metrics"
     7  	"github.com/hashicorp/nomad/nomad/structs"
     8  )
     9  
    10  // Plan endpoint is used for plan interactions
    11  type Plan struct {
    12  	srv *Server
    13  }
    14  
    15  // Submit is used to submit a plan to the leader
    16  func (p *Plan) Submit(args *structs.PlanRequest, reply *structs.PlanResponse) error {
    17  	if done, err := p.srv.forward("Plan.Submit", args, args, reply); done {
    18  		return err
    19  	}
    20  	defer metrics.MeasureSince([]string{"nomad", "plan", "submit"}, time.Now())
    21  
    22  	// Pause the Nack timer for the eval as it is making progress as long as it
    23  	// is in the plan queue. We resume immediately after we get a result to
    24  	// handle the case that the receiving worker dies.
    25  	plan := args.Plan
    26  	id := plan.EvalID
    27  	token := plan.EvalToken
    28  	if err := p.srv.evalBroker.PauseNackTimeout(id, token); err != nil {
    29  		return err
    30  	}
    31  	defer p.srv.evalBroker.ResumeNackTimeout(id, token)
    32  
    33  	// Submit the plan to the queue
    34  	future, err := p.srv.planQueue.Enqueue(plan)
    35  	if err != nil {
    36  		return err
    37  	}
    38  
    39  	// Wait for the results
    40  	result, err := future.Wait()
    41  	if err != nil {
    42  		return err
    43  	}
    44  
    45  	// Package the result
    46  	reply.Result = result
    47  	reply.Index = result.AllocIndex
    48  	return nil
    49  }