mirror of
https://github.com/yusing/godoxy.git
synced 2025-05-20 12:42:34 +02:00
52 lines
1.3 KiB
Go
52 lines
1.3 KiB
Go
package idlewatcher
|
|
|
|
import (
|
|
"context"
|
|
"errors"
|
|
|
|
"github.com/docker/docker/api/types/container"
|
|
)
|
|
|
|
type (
|
|
containerMeta struct {
|
|
ContainerID, ContainerName string
|
|
}
|
|
containerState struct {
|
|
running bool
|
|
ready bool
|
|
err error
|
|
}
|
|
)
|
|
|
|
func (w *Watcher) containerStop(ctx context.Context) error {
|
|
return w.client.ContainerStop(ctx, w.ContainerID, container.StopOptions{
|
|
Signal: string(w.StopSignal),
|
|
Timeout: &w.StopTimeout,
|
|
})
|
|
}
|
|
|
|
func (w *Watcher) containerPause(ctx context.Context) error {
|
|
return w.client.ContainerPause(ctx, w.ContainerID)
|
|
}
|
|
|
|
func (w *Watcher) containerKill(ctx context.Context) error {
|
|
return w.client.ContainerKill(ctx, w.ContainerID, string(w.StopSignal))
|
|
}
|
|
|
|
func (w *Watcher) containerUnpause(ctx context.Context) error {
|
|
return w.client.ContainerUnpause(ctx, w.ContainerID)
|
|
}
|
|
|
|
func (w *Watcher) containerStart(ctx context.Context) error {
|
|
return w.client.ContainerStart(ctx, w.ContainerID, container.StartOptions{})
|
|
}
|
|
|
|
func (w *Watcher) containerStatus() (string, error) {
|
|
ctx, cancel := context.WithTimeoutCause(w.task.Context(), dockerReqTimeout, errors.New("docker request timeout"))
|
|
defer cancel()
|
|
json, err := w.client.ContainerInspect(ctx, w.ContainerID)
|
|
if err != nil {
|
|
return "", err
|
|
}
|
|
return json.State.Status, nil
|
|
}
|