Skip to content

Commit

Permalink
Merge pull request #5 from aurora-is-near/compose_fix
Browse files Browse the repository at this point in the history
Add Compose dependency, fix worker being stuck
  • Loading branch information
spilin authored Dec 18, 2024
2 parents 9316203 + d28d165 commit 62d2846
Show file tree
Hide file tree
Showing 4 changed files with 55 additions and 26 deletions.
5 changes: 3 additions & 2 deletions Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,12 @@ FROM alpine:3.17

WORKDIR /app

# Install runtime dependencies including docker cli
# Install runtime dependencies including docker and docker compose plugin
RUN apk add --no-cache \
ca-certificates \
curl \
docker-cli
docker-cli \
docker-cli-compose

# Copy the binary from the builder stage
COPY --from=builder /app/app /app/app
Expand Down
16 changes: 9 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -26,15 +26,17 @@ The service can be deployed using Docker Compose. Below is an example configurat
```yaml
services:
sidecar:
image: ghcr.io/aurora-is-near/blockscout-vc:latest
command:
- sh
- -c
- /app/app sidecar --config /app/config/local.yaml
container_name: sidecar
pull_policy: always
command: ["sh", "-c", "/app/app --config /app/config/local.yaml"]
volumes:
- ./config:/app/config
- /var/run/docker.sock:/var/run/docker.sock
- ./docker-compose.yaml:/app/config/docker-compose.yaml:ro
image: ghcr.io/aurora-is-near/blockscout-vc:latest
restart: unless-stopped
volumes:
- ./config:/app/config
- /var/run/docker.sock:/var/run/docker.sock
- ./docker-compose.yaml:/app/config/docker-compose.yaml
```
### Important Notes
Expand Down
37 changes: 28 additions & 9 deletions internal/docker/docker.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,18 +27,37 @@ func (d *Docker) RecreateContainers(containerNames []string) error {
pathToDockerCompose := viper.GetString("pathToDockerCompose")
uniqueContainers := d.UniqueContainerNames(containerNames)

args := []string{"compose", "-f", pathToDockerCompose, "up", "-d", "--force-recreate"}
args = append(args, uniqueContainers...)
// Define the sequence of commands to execute
commands := []struct {
args []string
desc string
errMessage string
}{
{
args: []string{"rm", "-f"},
desc: "Stopping and removing containers",
errMessage: "Error stopping and removing containers",
},
{
args: []string{"compose", "-f", pathToDockerCompose, "up", "-d", "--force-recreate"},
desc: "Recreating containers",
errMessage: "Error recreating containers",
},
}

cmd := exec.Command("docker", args...)
cmd.Stdout = os.Stdout
cmd.Stderr = os.Stderr
// Execute each command in sequence
for _, cmd := range commands {
args := append(cmd.args, uniqueContainers...)

fmt.Printf("Running docker-compose up -d --force-recreate %v\n", containerNames)
execCmd := exec.Command("docker", args...)
execCmd.Stdout = os.Stdout
execCmd.Stderr = os.Stderr

if err := cmd.Run(); err != nil {
fmt.Printf("Error: %v\n", err)
return err
fmt.Printf("%s: %s\n", cmd.desc, execCmd.String())
if err := execCmd.Run(); err != nil {
fmt.Printf("%s: %v\n", cmd.errMessage, err)
return err
}
}

fmt.Println("Docker containers recreated successfully!")
Expand Down
23 changes: 15 additions & 8 deletions internal/worker/worker.go
Original file line number Diff line number Diff line change
Expand Up @@ -68,15 +68,22 @@ func (w *Worker) process(ctx context.Context) {
case <-ctx.Done():
return
case job := <-w.jobs:
err := w.docker.RecreateContainers(job.ContainerNames)
if err != nil {
log.Printf("failed to recreate containers: %v", err)
continue
}
jobKey := w.makeKey(job.ContainerNames)
// Using an immediately invoked function to ensure defer runs after each job
// Without this, defer would only run when the process function returns
func() {
defer func() {
w.jobSetMux.Lock()
delete(w.jobSet, jobKey)
w.jobSetMux.Unlock()
}()

w.jobSetMux.Lock()
delete(w.jobSet, w.makeKey(job.ContainerNames))
w.jobSetMux.Unlock()
err := w.docker.RecreateContainers(job.ContainerNames)
if err != nil {
log.Printf("failed to recreate containers: %v", err)
return
}
}()
}
}
}
Expand Down

0 comments on commit 62d2846

Please sign in to comment.