2015-08-23 22:06:47 +00:00
|
|
|
package client
|
|
|
|
|
2015-08-23 22:15:48 +00:00
|
|
|
import (
|
|
|
|
"log"
|
|
|
|
"sync"
|
|
|
|
|
|
|
|
"github.com/hashicorp/nomad/nomad/structs"
|
|
|
|
)
|
2015-08-23 22:06:47 +00:00
|
|
|
|
2015-08-23 22:32:46 +00:00
|
|
|
// AllocRunner is used to wrap an allocation and provide the execution context.
|
|
|
|
type AllocRunner struct {
|
2015-08-23 22:06:47 +00:00
|
|
|
client *Client
|
2015-08-23 22:15:48 +00:00
|
|
|
logger *log.Logger
|
|
|
|
|
|
|
|
alloc *structs.Allocation
|
|
|
|
|
|
|
|
updateCh chan *structs.Allocation
|
|
|
|
|
|
|
|
destroy bool
|
|
|
|
destroyCh chan struct{}
|
|
|
|
destroyLock sync.Mutex
|
2015-08-23 22:06:47 +00:00
|
|
|
}
|
|
|
|
|
2015-08-23 22:32:46 +00:00
|
|
|
// NewAllocRunner is used to create a new allocation context
|
|
|
|
func NewAllocRunner(client *Client, alloc *structs.Allocation) *AllocRunner {
|
|
|
|
ctx := &AllocRunner{
|
2015-08-23 22:30:16 +00:00
|
|
|
client: client,
|
|
|
|
logger: client.logger,
|
|
|
|
alloc: alloc,
|
|
|
|
updateCh: make(chan *structs.Allocation, 8),
|
|
|
|
destroyCh: make(chan struct{}),
|
2015-08-23 22:06:47 +00:00
|
|
|
}
|
|
|
|
return ctx
|
|
|
|
}
|
|
|
|
|
|
|
|
// Alloc returns the associated allocation
|
2015-08-23 22:32:46 +00:00
|
|
|
func (c *AllocRunner) Alloc() *structs.Allocation {
|
2015-08-23 22:15:48 +00:00
|
|
|
return c.alloc
|
2015-08-23 22:06:47 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Run is a long running goroutine used to manage an allocation
|
2015-08-23 22:32:46 +00:00
|
|
|
func (c *AllocRunner) Run() {
|
2015-08-23 22:15:48 +00:00
|
|
|
c.logger.Printf("[DEBUG] client: starting context for alloc '%s'", c.alloc.ID)
|
|
|
|
|
|
|
|
// TODO: Start
|
2015-08-23 22:06:47 +00:00
|
|
|
for {
|
2015-08-23 22:15:48 +00:00
|
|
|
select {
|
|
|
|
case update := <-c.updateCh:
|
|
|
|
// TODO: Update
|
|
|
|
c.alloc = update
|
|
|
|
case <-c.destroyCh:
|
|
|
|
// TODO: Destroy
|
|
|
|
return
|
|
|
|
}
|
2015-08-23 22:06:47 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Update is used to update the allocation of the context
|
2015-08-23 22:32:46 +00:00
|
|
|
func (c *AllocRunner) Update(update *structs.Allocation) {
|
2015-08-23 22:15:48 +00:00
|
|
|
select {
|
|
|
|
case c.updateCh <- update:
|
|
|
|
default:
|
|
|
|
c.logger.Printf("[ERR] client: dropping update to alloc '%s'", update.ID)
|
|
|
|
}
|
2015-08-23 22:06:47 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Destroy is used to indicate that the allocation context should be destroyed
|
2015-08-23 22:32:46 +00:00
|
|
|
func (c *AllocRunner) Destroy() {
|
2015-08-23 22:15:48 +00:00
|
|
|
c.destroyLock.Lock()
|
|
|
|
defer c.destroyLock.Unlock()
|
|
|
|
|
|
|
|
if c.destroy {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
c.destroy = true
|
|
|
|
close(c.destroyCh)
|
2015-08-23 22:06:47 +00:00
|
|
|
}
|