nomad-driver-nix2/exec2/handle.go

80 lines
1.7 KiB
Go
Raw Normal View History

2022-11-28 11:26:41 +00:00
package exec
2022-11-28 10:33:28 +00:00
import (
"context"
"strconv"
"sync"
"time"
2022-11-28 11:26:41 +00:00
hclog "github.com/hashicorp/go-hclog"
plugin "github.com/hashicorp/go-plugin"
2022-11-28 10:33:28 +00:00
"github.com/hashicorp/nomad/drivers/shared/executor"
"github.com/hashicorp/nomad/plugins/drivers"
)
type taskHandle struct {
exec executor.Executor
2022-11-28 11:26:41 +00:00
pid int
2022-11-28 10:33:28 +00:00
pluginClient *plugin.Client
2022-11-28 11:26:41 +00:00
logger hclog.Logger
2022-11-28 10:33:28 +00:00
2022-11-28 11:26:41 +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
2022-11-28 10:33:28 +00:00
}
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()
2022-11-28 11:26:41 +00:00
// Block until process exits
2022-11-28 10:33:28 +00:00
ps, err := h.exec.Wait(context.Background())
2022-11-28 11:26:41 +00:00
2022-11-28 10:33:28 +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
2022-11-28 11:26:41 +00:00
// TODO: detect if the task OOMed
2022-11-28 10:33:28 +00:00
}