open-nomad/drivers/qemu/handle.go
2018-10-17 11:43:15 -05:00

59 lines
1.3 KiB
Go

package qemu
import (
"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"
)
type qemuTaskHandle struct {
exec executor.Executor
pid int
pluginClient *plugin.Client
logger hclog.Logger
monitorPath string
// 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 *qemuTaskHandle) IsRunning() bool {
return h.procState == drivers.TaskStateRunning
}
func (h *qemuTaskHandle) run() {
// since run is called immediately after the handle is created this
// ensures the exitResult is initialized so we avoid a nil pointer
// thus it does not need to be included in the lock
if h.exitResult == nil {
h.exitResult = &drivers.ExitResult{}
}
ps, err := h.exec.Wait()
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
}