github.com/kardianos/nomad@v0.1.3-0.20151022182107-b13df73ee850/nomad/alloc_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  // Alloc endpoint is used for manipulating allocations
    11  type Alloc struct {
    12  	srv *Server
    13  }
    14  
    15  // List is used to list the allocations in the system
    16  func (a *Alloc) List(args *structs.AllocListRequest, reply *structs.AllocListResponse) error {
    17  	if done, err := a.srv.forward("Alloc.List", args, args, reply); done {
    18  		return err
    19  	}
    20  	defer metrics.MeasureSince([]string{"nomad", "alloc", "list"}, time.Now())
    21  
    22  	// Capture all the allocations
    23  	snap, err := a.srv.fsm.State().Snapshot()
    24  	if err != nil {
    25  		return err
    26  	}
    27  	iter, err := snap.Allocs()
    28  	if err != nil {
    29  		return err
    30  	}
    31  
    32  	for {
    33  		raw := iter.Next()
    34  		if raw == nil {
    35  			break
    36  		}
    37  		alloc := raw.(*structs.Allocation)
    38  		reply.Allocations = append(reply.Allocations, alloc.Stub())
    39  	}
    40  
    41  	// Use the last index that affected the jobs table
    42  	index, err := snap.Index("allocs")
    43  	if err != nil {
    44  		return err
    45  	}
    46  	reply.Index = index
    47  
    48  	// Set the query response
    49  	a.srv.setQueryMeta(&reply.QueryMeta)
    50  	return nil
    51  }
    52  
    53  // GetAlloc is used to lookup a particular allocation
    54  func (a *Alloc) GetAlloc(args *structs.AllocSpecificRequest,
    55  	reply *structs.SingleAllocResponse) error {
    56  	if done, err := a.srv.forward("Alloc.GetAlloc", args, args, reply); done {
    57  		return err
    58  	}
    59  	defer metrics.MeasureSince([]string{"nomad", "alloc", "get_alloc"}, time.Now())
    60  
    61  	// Lookup the allocation
    62  	snap, err := a.srv.fsm.State().Snapshot()
    63  	if err != nil {
    64  		return err
    65  	}
    66  	out, err := snap.AllocByID(args.AllocID)
    67  	if err != nil {
    68  		return err
    69  	}
    70  
    71  	// Setup the output
    72  	if out != nil {
    73  		reply.Alloc = out
    74  		reply.Index = out.ModifyIndex
    75  	} else {
    76  		// Use the last index that affected the nodes table
    77  		index, err := snap.Index("allocs")
    78  		if err != nil {
    79  			return err
    80  		}
    81  		reply.Index = index
    82  	}
    83  
    84  	// Set the query response
    85  	a.srv.setQueryMeta(&reply.QueryMeta)
    86  	return nil
    87  }