2018-10-31 14:30:13 +00:00
|
|
|
package java
|
|
|
|
|
|
|
|
import (
|
2018-12-05 16:04:18 +00:00
|
|
|
"context"
|
2018-11-02 12:29:39 +00:00
|
|
|
"strconv"
|
2018-10-31 14:30:13 +00:00
|
|
|
"sync"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/hashicorp/go-hclog"
|
|
|
|
"github.com/hashicorp/go-plugin"
|
2018-12-05 16:04:18 +00:00
|
|
|
"github.com/hashicorp/nomad/drivers/shared/executor/structs"
|
2018-10-31 14:30:13 +00:00
|
|
|
"github.com/hashicorp/nomad/plugins/drivers"
|
|
|
|
)
|
|
|
|
|
|
|
|
type taskHandle struct {
|
2018-12-05 16:04:18 +00:00
|
|
|
exec structs.Executor
|
2018-10-31 14:30:13 +00:00
|
|
|
pid int
|
|
|
|
pluginClient *plugin.Client
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2018-11-02 12:29:39 +00:00
|
|
|
func (h *taskHandle) TaskStatus() *drivers.TaskStatus {
|
2018-10-31 14:30:13 +00:00
|
|
|
h.stateLock.RLock()
|
|
|
|
defer h.stateLock.RUnlock()
|
|
|
|
|
2018-11-02 12:29:39 +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),
|
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (h *taskHandle) IsRunning() bool {
|
|
|
|
h.stateLock.RLock()
|
|
|
|
defer h.stateLock.RUnlock()
|
2018-10-31 14:30:13 +00:00
|
|
|
return h.procState == drivers.TaskStateRunning
|
|
|
|
}
|
|
|
|
|
|
|
|
func (h *taskHandle) run() {
|
|
|
|
h.stateLock.Lock()
|
|
|
|
if h.exitResult == nil {
|
|
|
|
h.exitResult = &drivers.ExitResult{}
|
|
|
|
}
|
|
|
|
h.stateLock.Unlock()
|
|
|
|
|
2018-12-05 16:04:18 +00:00
|
|
|
ps, err := h.exec.Wait(context.Background())
|
2018-11-02 12:29:39 +00:00
|
|
|
|
2018-10-31 14:30:13 +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
|
|
|
|
}
|