Skip to content

Commit

Permalink
Add more debug information
Browse files Browse the repository at this point in the history
  • Loading branch information
fabpot committed May 9, 2024
1 parent 96d4e1d commit ba1504f
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 15 deletions.
1 change: 1 addition & 0 deletions commands/local_server_start.go
Expand Up @@ -325,6 +325,7 @@ var localServerStartCmd = &console.Command{
continue
}
pidFile.Watched = worker.Watch
pidFile.CustomName = name

// we run each worker in its own goroutine for several reasons:
// * to get things up and running faster
Expand Down
30 changes: 15 additions & 15 deletions local/runner.go
Expand Up @@ -126,7 +126,7 @@ func (r *Runner) Run() error {
continue
}

terminal.Logger.Debug().Msg("Got event: " + event.Event().String())
terminal.Logger.Debug().Str("cmd", r.pidFile.String()).Msg("Got event: " + event.Event().String())

select {
case restartChan <- true:
Expand All @@ -139,10 +139,10 @@ func (r *Runner) Run() error {
if fi, err := os.Stat(watched); err != nil {
continue
} else if fi.IsDir() {
terminal.Logger.Info().Msg("Watching directory " + watched)
terminal.Logger.Info().Str("cmd", r.pidFile.String()).Msg("Watching directory " + watched)
watched = filepath.Join(watched, "...")
} else {
terminal.Logger.Info().Msg("Watching file " + watched)
terminal.Logger.Info().Str("cmd", r.pidFile.String()).Msg("Watching file " + watched)
}
if err := inotify.Watch(watched, c, inotify.All); err != nil {
return errors.Wrapf(err, `could not watch "%s"`, watched)
Expand Down Expand Up @@ -179,10 +179,10 @@ func (r *Runner) Run() error {
reexec.NotifyForeground("started")
}

terminal.Logger.Debug().Msg("Waiting for channels (first boot)")
terminal.Logger.Debug().Str("cmd", r.pidFile.String()).Msg("Waiting for channels (first boot)")
select {
case err := <-cmdExitChan:
terminal.Logger.Debug().Msg("Received exit (first boot)")
terminal.Logger.Debug().Str("cmd", r.pidFile.String()).Msg("Received exit (first boot)")
if err != nil {
return errors.Wrapf(err, `command "%s" failed early`, r.pidFile)
}
Expand All @@ -193,7 +193,7 @@ func (r *Runner) Run() error {
// finished later one
go func() { cmdExitChan <- err }()
case <-timer.C:
terminal.Logger.Debug().Msg("Received timer message (first boot)")
terminal.Logger.Debug().Str("cmd", r.pidFile.String()).Msg("Received timer message (first boot)")
}
}

Expand All @@ -214,54 +214,54 @@ func (r *Runner) Run() error {

select {
case sig := <-sigChan:
terminal.Logger.Info().Msgf("Signal \"%s\" received, forwarding to command and exiting\n", sig)
terminal.Logger.Info().Str("cmd", r.pidFile.String()).Msgf("Signal \"%s\" received, forwarding and exiting\n", sig)
err := cmd.Process.Signal(sig)
if err != nil && runtime.GOOS == "windows" && strings.Contains(err.Error(), "not supported by windows") {
return exec.Command("CMD", "/C", "TASKKILL", "/F", "/PID", strconv.Itoa(cmd.Process.Pid)).Run()
}
return err
case <-restartChan:
terminal.Logger.Debug().Msg("Received restart")
terminal.Logger.Debug().Str("cmd", r.pidFile.String()).Msg("Received restart")
// We use SIGTERM here because it's nicer and thus when we use our
// wrappers, signal will be nicely forwarded
cmd.Process.Signal(syscall.SIGTERM)
// we need to drain cmdExit channel to unblock cmd channel receiver
<-cmdExitChan
// Command exited
case err := <-cmdExitChan:
terminal.Logger.Debug().Msg("Received exit")
terminal.Logger.Debug().Str("cmd", r.pidFile.String()).Msg("Received exit")
err = errors.Wrapf(err, `command "%s" failed`, r.pidFile)

if err == nil && r.SuccessHook != nil {
terminal.Logger.Debug().Msg("Running success hook")
terminal.Logger.Debug().Str("cmd", r.pidFile.String()).Msg("Running success hook")
r.SuccessHook(r, cmd)
}

// Command is NOT set up to loop, stop here and remove the pidFile
// if the command is successful
if !looping {
if err != nil {
terminal.Logger.Debug().Msg("Not looping, exiting with error")
terminal.Logger.Debug().Str("cmd", r.pidFile.String()).Msg("Not looping, exiting with error")
return err
}

terminal.Logger.Debug().Msg("Removing pid file")
terminal.Logger.Debug().Str("cmd", r.pidFile.String()).Msg("Removing pid file")
return r.pidFile.Remove()
}
terminal.Logger.Debug().Msg("Looping")

// Command is set up to restart on exit (usually PHP builtin
// server), so we restart immediately without waiting
if r.AlwaysRestartOnExit {
terminal.Logger.Error().Msgf(`command "%s" exited, restarting it immediately`, r.pidFile)
terminal.Logger.Error().Str("cmd", r.pidFile.String()).Msg("command exited, restarting it immediately")
continue
}

// In case of error we want to wait up-to 5 seconds before
// restarting the command, this avoids overloading the system with a
// failing command
if err != nil {
terminal.Logger.Error().Msgf("%s, waiting 5 seconds before restarting it", err)
terminal.Logger.Error().Str("cmd", r.pidFile.String()).Msgf(`command exited: %s, waiting 5 seconds before restarting it`, err)
timer.Reset(5 * time.Second)
}

Expand All @@ -280,7 +280,7 @@ func (r *Runner) Run() error {
}
}

terminal.Logger.Info().Msgf(`Restarting command "%s"`, r.pidFile)
terminal.Logger.Info().Str("cmd", r.pidFile.String()).Msg("Restarting command")
}
}

Expand Down

0 comments on commit ba1504f

Please sign in to comment.