80 lines
1.9 KiB
Go
80 lines
1.9 KiB
Go
|
package skeleton
|
||
|
|
||
|
import (
|
||
|
"context"
|
||
|
"strconv"
|
||
|
"sync"
|
||
|
"time"
|
||
|
|
||
|
"github.com/hashicorp/go-hclog"
|
||
|
"github.com/hashicorp/nomad/plugins/drivers"
|
||
|
)
|
||
|
|
||
|
// taskHandle should store all relevant runtime information
|
||
|
// such as process ID if this is a local task or other meta
|
||
|
// data if this driver deals with external APIs
|
||
|
type taskHandle struct {
|
||
|
pid int
|
||
|
logger hclog.Logger
|
||
|
|
||
|
// stateLock syncs access to all fields below
|
||
|
stateLock sync.RWMutex
|
||
|
|
||
|
taskConfig *drivers.TaskConfig
|
||
|
procState drivers.TaskState
|
||
|
startedAt time.Time
|
||
|
completedAt time.Time
|
||
|
exitResult *drivers.ExitResult
|
||
|
}
|
||
|
|
||
|
func (h *taskHandle) TaskStatus() *drivers.TaskStatus {
|
||
|
h.stateLock.RLock()
|
||
|
defer h.stateLock.RUnlock()
|
||
|
|
||
|
return &drivers.TaskStatus{
|
||
|
ID: h.taskConfig.ID,
|
||
|
Name: h.taskConfig.Name,
|
||
|
State: h.procState,
|
||
|
StartedAt: h.startedAt,
|
||
|
CompletedAt: h.completedAt,
|
||
|
ExitResult: h.exitResult,
|
||
|
DriverAttributes: map[string]string{
|
||
|
"pid": strconv.Itoa(h.pid),
|
||
|
},
|
||
|
}
|
||
|
}
|
||
|
|
||
|
func (h *taskHandle) IsRunning() bool {
|
||
|
h.stateLock.RLock()
|
||
|
defer h.stateLock.RUnlock()
|
||
|
return h.procState == drivers.TaskStateRunning
|
||
|
}
|
||
|
|
||
|
func (h *taskHandle) run() {
|
||
|
h.stateLock.Lock()
|
||
|
if h.exitResult == nil {
|
||
|
h.exitResult = &drivers.ExitResult{}
|
||
|
}
|
||
|
h.stateLock.Unlock()
|
||
|
|
||
|
h.stateLock.Lock()
|
||
|
defer h.stateLock.Unlock()
|
||
|
|
||
|
// Implement running the task here, this is driver specific
|
||
|
}
|
||
|
|
||
|
func (h *taskHandle) stats(ctx context.Context, interval time.Duration) (<-chan *drivers.TaskResourceUsage, error) {
|
||
|
ch := make(chan *drivers.TaskResourceUsage)
|
||
|
|
||
|
return ch, nil
|
||
|
}
|
||
|
|
||
|
// shutdown shuts down the container, with `timeout` grace period
|
||
|
// before killing the container with SIGKILL.
|
||
|
func (h *taskHandle) shutdown(timeout time.Duration) error {
|
||
|
// Implement shutdown of the task. Shutdown should be tried
|
||
|
// gracefully first. If its past the timeout, then this method
|
||
|
// should do a hard shutdown and return
|
||
|
return nil
|
||
|
}
|