From 36ea8a9e5b8f528eb4939d719f3f3da8a74b98a2 Mon Sep 17 00:00:00 2001 From: Armon Dadgar Date: Sun, 23 Aug 2015 15:15:48 -0700 Subject: [PATCH] client: working on alloc context --- client/alloc_context.go | 57 ++++++++++++++++++++++++++++++++++------- 1 file changed, 48 insertions(+), 9 deletions(-) diff --git a/client/alloc_context.go b/client/alloc_context.go index 8dc588bed..577e9dd6a 100644 --- a/client/alloc_context.go +++ b/client/alloc_context.go @@ -1,37 +1,76 @@ package client -import "github.com/hashicorp/nomad/nomad/structs" +import ( + "log" + "sync" + + "github.com/hashicorp/nomad/nomad/structs" +) // AllocContext is used to wrap an allocation and provide the execution context. type AllocContext struct { client *Client - alloc *structs.Allocation + logger *log.Logger + + alloc *structs.Allocation + + updateCh chan *structs.Allocation + + destroy bool + destroyCh chan struct{} + destroyLock sync.Mutex } // NewAllocContext is used to create a new allocation context func NewAllocContext(client *Client, alloc *structs.Allocation) *AllocContext { ctx := &AllocContext{ - client: client, - alloc: alloc, + client: client, + logger: client.logger, + alloc: alloc, + updateCh: make(chan *structs.Allocation, 8), } return ctx } // Alloc returns the associated allocation -func (ctx *AllocContext) Alloc() *structs.Allocation { - return ctx.alloc +func (c *AllocContext) Alloc() *structs.Allocation { + return c.alloc } // Run is a long running goroutine used to manage an allocation -func (ctx *AllocContext) Run() { +func (c *AllocContext) Run() { + c.logger.Printf("[DEBUG] client: starting context for alloc '%s'", c.alloc.ID) + + // TODO: Start for { + select { + case update := <-c.updateCh: + // TODO: Update + c.alloc = update + case <-c.destroyCh: + // TODO: Destroy + return + } } } // Update is used to update the allocation of the context -func (ctx *AllocContext) Update(update *structs.Allocation) { +func (c *AllocContext) Update(update *structs.Allocation) { + select { + case c.updateCh <- update: + default: + c.logger.Printf("[ERR] client: dropping update to alloc '%s'", update.ID) + } } // Destroy is used to indicate that the allocation context should be destroyed -func (ctx *AllocContext) Destroy() { +func (c *AllocContext) Destroy() { + c.destroyLock.Lock() + defer c.destroyLock.Unlock() + + if c.destroy { + return + } + c.destroy = true + close(c.destroyCh) }