Skip to content

Commit

Permalink
Add support for a docker_compose worker and prioritize its startup
Browse files Browse the repository at this point in the history
  • Loading branch information
tucksaun committed Nov 3, 2023
1 parent 8dd5d02 commit 549bdbf
Show file tree
Hide file tree
Showing 3 changed files with 60 additions and 1 deletion.
43 changes: 42 additions & 1 deletion commands/local_server_start.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ import (
"os/exec"
"os/signal"
"path/filepath"
"sync"
"syscall"

"github.com/pkg/errors"
Expand Down Expand Up @@ -309,6 +310,13 @@ var localServerStartCmd = &console.Command{

if fileConfig != nil {
reexec.NotifyForeground("workers")

_, isDockerComposeWorkerConfigured := fileConfig.Workers[project.DockerComposeWorkerKey]
var dockerWg sync.WaitGroup
if isDockerComposeWorkerConfigured {
dockerWg.Add(1)
}

for name, worker := range fileConfig.Workers {
pidFile := pid.New(projectDir, worker.Cmd)
if pidFile.IsRunning() {
Expand All @@ -335,10 +343,43 @@ var localServerStartCmd = &console.Command{

runner.BuildCmdHook = func(cmd *exec.Cmd) error {
cmd.Env = append(cmd.Env, envs.AsSlice(env)...)

return nil
}

if name == project.DockerComposeWorkerKey {
originalBuildCmdHook := runner.BuildCmdHook

runner.BuildCmdHook = func(cmd *exec.Cmd) error {
cmd.Args = append(cmd.Args, "--detach")

return originalBuildCmdHook(cmd)
}

runner.SuccessHook = func(runner *local.Runner, cmd *exec.Cmd) {
terminal.Eprintln("<info>INFO</> Docker Compose is now up, switching to non detached mode")

// set up the worker for an immediate restart so
// that it starts monitoring the containers as soon
// as possible after the initial startup
runner.AlwaysRestartOnExit = true
// but next time this process is successful we don't
// have to do anything specific
runner.SuccessHook = nil
// and we move back AlwaysRestartOnExit to false

runner.BuildCmdHook = func(cmd *exec.Cmd) error {
runner.AlwaysRestartOnExit = false

return originalBuildCmdHook(cmd)
}

dockerWg.Done()
}
} else if isDockerComposeWorkerConfigured {
terminal.Eprintfln("<info>INFO</> Worker \"%s\" waiting for Docker Compose to be up", name)
dockerWg.Wait()
}

ui.Success(fmt.Sprintf("Started worker \"%s\"", name))
if err := runner.Run(); err != nil {
terminal.Eprintfln("<warning>WARNING</> Worker \"%s\" exited with an error: %s", name, err)
Expand Down
13 changes: 13 additions & 0 deletions local/project/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@ import (
"gopkg.in/yaml.v2"
)

const DockerComposeWorkerKey = "docker_compose"

// Config is the struct taken by New (should not be used for anything else)
type Config struct {
HomeDir string
Expand Down Expand Up @@ -143,6 +145,17 @@ func (c *FileConfig) parseWorkers() error {
return nil
}

if v, ok := c.Workers[DockerComposeWorkerKey]; ok && v == nil {
c.Workers[DockerComposeWorkerKey] = &Worker{
Cmd: []string{"docker", "compose", "up"},
Watch: []string{
"compose.yaml", "compose.override.yaml",
"compose.yml", "compose.override.yml",
"docker-compose.yml", "docker-compose.override.yml",
"docker-compose.yaml", "docker-compose.override.yaml",
},
}
}
if v, ok := c.Workers["yarn_encore_watch"]; ok && v == nil {
c.Workers["yarn_encore_watch"] = &Worker{
Cmd: []string{"yarn", "encore", "dev", "--watch"},
Expand Down
5 changes: 5 additions & 0 deletions local/runner.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ type Runner struct {
pidFile *pid.PidFile

BuildCmdHook func(*exec.Cmd) error
SuccessHook func(*Runner, *exec.Cmd)
AlwaysRestartOnExit bool
}

Expand Down Expand Up @@ -226,6 +227,10 @@ func (r *Runner) Run() error {
case err := <-cmdExitChan:
err = errors.Wrapf(err, `command "%s" failed`, r.pidFile)

if err == nil && r.SuccessHook != nil {
r.SuccessHook(r, cmd)
}

// Command is NOT set up to loop, stop here and remove the pidFile
// if the command is successful
if !looping {
Expand Down

0 comments on commit 549bdbf

Please sign in to comment.