2023-02-27 16:20:30 +00:00
|
|
|
// Copyright (c) HashiCorp, Inc.
|
|
|
|
// SPDX-License-Identifier: MPL-2.0
|
|
|
|
|
2019-11-29 23:58:03 +00:00
|
|
|
package hello
|
2019-09-30 15:26:20 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"strconv"
|
|
|
|
"sync"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/hashicorp/go-hclog"
|
2019-11-29 23:58:03 +00:00
|
|
|
"github.com/hashicorp/go-plugin"
|
|
|
|
"github.com/hashicorp/nomad/drivers/shared/executor"
|
2019-09-30 15:26:20 +00:00
|
|
|
"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 {
|
|
|
|
// stateLock syncs access to all fields below
|
|
|
|
stateLock sync.RWMutex
|
|
|
|
|
2019-11-29 23:58:03 +00:00
|
|
|
logger hclog.Logger
|
|
|
|
exec executor.Executor
|
|
|
|
pluginClient *plugin.Client
|
|
|
|
taskConfig *drivers.TaskConfig
|
|
|
|
procState drivers.TaskState
|
|
|
|
startedAt time.Time
|
|
|
|
completedAt time.Time
|
|
|
|
exitResult *drivers.ExitResult
|
|
|
|
|
|
|
|
// TODO: add any extra relevant information about the task.
|
|
|
|
pid int
|
2019-09-30 15:26:20 +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()
|
|
|
|
|
2019-11-29 23:58:03 +00:00
|
|
|
// TODO: wait for your task to complete and upate its state.
|
|
|
|
ps, err := h.exec.Wait(context.Background())
|
2019-09-30 15:26:20 +00:00
|
|
|
h.stateLock.Lock()
|
|
|
|
defer h.stateLock.Unlock()
|
|
|
|
|
2019-11-29 23:58:03 +00:00
|
|
|
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
|
2019-09-30 15:26:20 +00:00
|
|
|
}
|