InspectTask API.

This commit is contained in:
Shishir Mahajan 2020-05-18 11:05:00 -07:00
parent 541883b1c2
commit 9955ee8a50
No known key found for this signature in database
GPG Key ID: D41782E7688DEC4A
2 changed files with 27 additions and 5 deletions

View File

@ -452,7 +452,12 @@ func (d *Driver) DestroyTask(taskID string, force bool) error {
return drivers.ErrTaskNotFound
}
if handle.IsRunning() && !force {
isRunning, err := handle.IsRunning(d.ctxContainerd)
if err != nil {
return err
}
if isRunning && !force {
return fmt.Errorf("cannot destroy running task")
}
@ -471,7 +476,7 @@ func (d *Driver) InspectTask(taskID string) (*drivers.TaskStatus, error) {
return nil, drivers.ErrTaskNotFound
}
return handle.TaskStatus(), nil
return handle.TaskStatus(d.ctxContainerd), nil
}
// TaskStats returns a channel which the driver should send stats to at the given interval.

View File

@ -2,6 +2,7 @@ package containerd
import (
"context"
"fmt"
"os"
"sync"
"syscall"
@ -30,10 +31,19 @@ type taskHandle struct {
task containerd.Task
}
func (h *taskHandle) TaskStatus() *drivers.TaskStatus {
func (h *taskHandle) TaskStatus(ctxContainerd context.Context) *drivers.TaskStatus {
h.stateLock.RLock()
defer h.stateLock.RUnlock()
h.procState = drivers.TaskStateExited
isRunning, err := h.IsRunning(ctxContainerd)
if err != nil {
h.procState = drivers.TaskStateUnknown
} else if isRunning {
h.procState = drivers.TaskStateRunning
}
return &drivers.TaskStatus{
ID: h.taskConfig.ID,
Name: h.taskConfig.Name,
@ -47,10 +57,16 @@ func (h *taskHandle) TaskStatus() *drivers.TaskStatus {
}
}
func (h *taskHandle) IsRunning() bool {
func (h *taskHandle) IsRunning(ctxContainerd context.Context) (bool, error) {
h.stateLock.RLock()
defer h.stateLock.RUnlock()
return h.procState == drivers.TaskStateRunning
status, err := h.task.Status(ctxContainerd)
if err != nil {
return false, fmt.Errorf("Error in getting task status: %v", err)
}
return (status.Status == containerd.Running), nil
}
func (h *taskHandle) run(ctxContainerd context.Context) {
@ -58,6 +74,7 @@ func (h *taskHandle) run(ctxContainerd context.Context) {
defer h.stateLock.Unlock()
// Sleep for 5 seconds to allow h.task.Wait() to kick in.
// TODO: Use goroutine and a channel to synchronize this, instead of sleep.
time.Sleep(5 * time.Second)
h.task.Start(ctxContainerd)