2018-10-17 16:43:15 +00:00
|
|
|
package qemu
|
|
|
|
|
|
|
|
import (
|
2018-10-31 18:53:37 +00:00
|
|
|
"strconv"
|
2018-10-17 16:43:15 +00:00
|
|
|
"sync"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/hashicorp/go-hclog"
|
|
|
|
"github.com/hashicorp/go-plugin"
|
|
|
|
"github.com/hashicorp/nomad/client/driver/executor"
|
|
|
|
"github.com/hashicorp/nomad/plugins/drivers"
|
|
|
|
)
|
|
|
|
|
2018-10-31 18:53:37 +00:00
|
|
|
type taskHandle struct {
|
2018-10-17 16:43:15 +00:00
|
|
|
exec executor.Executor
|
|
|
|
pid int
|
|
|
|
pluginClient *plugin.Client
|
|
|
|
logger hclog.Logger
|
|
|
|
monitorPath string
|
2018-10-31 00:34:08 +00:00
|
|
|
|
2018-10-17 16:43:15 +00:00
|
|
|
// 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
|
|
|
|
}
|
|
|
|
|
2018-10-31 18:53:37 +00:00
|
|
|
func (h *taskHandle) TaskStatus() *drivers.TaskStatus {
|
2018-10-31 00:34:08 +00:00
|
|
|
h.stateLock.RLock()
|
|
|
|
defer h.stateLock.RUnlock()
|
2018-10-31 18:53:37 +00:00
|
|
|
|
|
|
|
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),
|
|
|
|
},
|
|
|
|
}
|
2018-10-17 16:43:15 +00:00
|
|
|
}
|
|
|
|
|
2018-10-31 18:53:37 +00:00
|
|
|
func (h *taskHandle) IsRunning() bool {
|
|
|
|
h.stateLock.RLock()
|
|
|
|
defer h.stateLock.RUnlock()
|
|
|
|
return h.procState == drivers.TaskStateRunning
|
|
|
|
}
|
2018-10-17 16:43:15 +00:00
|
|
|
|
2018-10-31 18:53:37 +00:00
|
|
|
func (h *taskHandle) run() {
|
2018-10-31 00:34:08 +00:00
|
|
|
h.stateLock.Lock()
|
2018-10-17 16:43:15 +00:00
|
|
|
if h.exitResult == nil {
|
|
|
|
h.exitResult = &drivers.ExitResult{}
|
|
|
|
}
|
2018-10-31 00:34:08 +00:00
|
|
|
h.stateLock.Unlock()
|
2018-10-17 16:43:15 +00:00
|
|
|
|
|
|
|
ps, err := h.exec.Wait()
|
2018-10-31 00:34:08 +00:00
|
|
|
|
2018-10-17 16:43:15 +00:00
|
|
|
h.stateLock.Lock()
|
|
|
|
defer h.stateLock.Unlock()
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
h.exitResult.Err = err
|
|
|
|
h.procState = drivers.TaskStateUnknown
|
|
|
|
h.completedAt = time.Now()
|
|
|
|
return
|
|
|
|
}
|
|
|
|
h.procState = drivers.TaskStateExited
|
|
|
|
h.exitResult.ExitCode = ps.ExitCode
|
|
|
|
h.exitResult.Signal = ps.Signal
|
|
|
|
h.completedAt = ps.Time
|
|
|
|
|
|
|
|
// TODO: detect if the taskConfig OOMed
|
|
|
|
}
|