Skip to content

Commit

Permalink
worker: Use mutex for container management
Browse files Browse the repository at this point in the history
  • Loading branch information
yondonfu committed Jan 25, 2024
1 parent 880ae4c commit be04efd
Showing 1 changed file with 6 additions and 0 deletions.
6 changes: 6 additions & 0 deletions worker/worker.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
"log/slog"
"math"
"mime/multipart"
"sync"
"time"

"github.com/docker/cli/opts"
Expand Down Expand Up @@ -45,6 +46,7 @@ type Worker struct {

dockerClient *client.Client
containers map[string]*RunnerContainer
mu *sync.Mutex
}

func NewWorker(containerImageID string, gpus []string, modelDir string) (*Worker, error) {
Expand All @@ -60,6 +62,7 @@ func NewWorker(containerImageID string, gpus []string, modelDir string) (*Worker
modelDir: modelDir,
dockerClient: dockerClient,
containers: make(map[string]*RunnerContainer),
mu: &sync.Mutex{},
}, nil
}

Expand Down Expand Up @@ -200,6 +203,9 @@ func (w *Worker) Stop(ctx context.Context, containerName string) error {
}

func (w *Worker) getWarmContainer(ctx context.Context, containerName string, modelID string) (*RunnerContainer, error) {
w.mu.Lock()
defer w.mu.Unlock()

filters := filters.NewArgs(filters.Arg("name", "^"+containerName+"$"), filters.Arg("status", "running"))
containers, err := w.dockerClient.ContainerList(ctx, types.ContainerListOptions{Filters: filters})
if err != nil {
Expand Down

0 comments on commit be04efd

Please sign in to comment.