diff --git a/client/alloc_context.go b/client/alloc_context.go index 577e9dd6a..d8579e47a 100644 --- a/client/alloc_context.go +++ b/client/alloc_context.go @@ -24,10 +24,11 @@ type AllocContext struct { // NewAllocContext is used to create a new allocation context func NewAllocContext(client *Client, alloc *structs.Allocation) *AllocContext { ctx := &AllocContext{ - client: client, - logger: client.logger, - alloc: alloc, - updateCh: make(chan *structs.Allocation, 8), + client: client, + logger: client.logger, + alloc: alloc, + updateCh: make(chan *structs.Allocation, 8), + destroyCh: make(chan struct{}), } return ctx } diff --git a/client/task_context.go b/client/task_context.go new file mode 100644 index 000000000..6a0816b1f --- /dev/null +++ b/client/task_context.go @@ -0,0 +1,72 @@ +package client + +import ( + "log" + "sync" + + "github.com/hashicorp/nomad/nomad/structs" +) + +// TaskContext is used to wrap a task within an allocation and provide the execution context. +type TaskContext struct { + ctx *AllocContext + logger *log.Logger + + task *structs.Task + + updateCh chan *structs.Task + + destroy bool + destroyCh chan struct{} + destroyLock sync.Mutex +} + +// NewTaskContext is used to create a new task context +func NewTaskContext(ctx *AllocContext, task *structs.Task) *TaskContext { + tc := &TaskContext{ + ctx: ctx, + logger: ctx.logger, + task: task, + updateCh: make(chan *structs.Task, 8), + destroyCh: make(chan struct{}), + } + return tc +} + +// Run is a long running routine used to manage the task +func (t *TaskContext) Run() { + t.logger.Printf("[DEBUG] client: starting task context for '%s' (alloc '%s')", t.task.Name, t.ctx.Alloc().ID) + + // TODO: Start + for { + select { + case update := <-t.updateCh: + // TODO: Update + t.task = update + case <-t.destroyCh: + // TODO: Destroy + return + } + } +} + +// Update is used to update the task of the context +func (t *TaskContext) Update(update *structs.Task) { + select { + case t.updateCh <- update: + default: + t.logger.Printf("[ERR] client: dropping task update '%s' (alloc '%s')", update.Name, t.ctx.Alloc().ID) + } +} + +// Destroy is used to indicate that the task context should be destroyed +func (t *TaskContext) Destroy() { + t.destroyLock.Lock() + defer t.destroyLock.Unlock() + + if t.destroy { + return + } + t.destroy = true + close(t.destroyCh) +}