Skip to content

Commit

Permalink
fix: runInBackground returns error (#1332)
Browse files Browse the repository at this point in the history
Fixes #1170

The new terminal output, when I force an error to return from
`runInBackground`, looks like:

```
$ build/release/ftl serve --background
ftl: error: mock error!
```
  • Loading branch information
deniseli authored Apr 25, 2024
1 parent 27ceb12 commit 866e079
Showing 1 changed file with 9 additions and 9 deletions.
18 changes: 9 additions & 9 deletions cmd/ftl/cmd_serve.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,9 @@ func (s *serveCmd) Run(ctx context.Context) error {
_ = KillBackgroundServe(logger)
}

runInBackground(logger)
if err := runInBackground(logger); err != nil {
return err
}

err := waitForControllerOnline(ctx, s.StartupTimeout, client)
if err != nil {
Expand Down Expand Up @@ -125,10 +127,10 @@ func (s *serveCmd) Run(ctx context.Context) error {
return nil
}

func runInBackground(logger *log.Logger) {
func runInBackground(logger *log.Logger) error {
if running, _ := isServeRunning(logger); running {
logger.Warnf(ftlRunningErrorMsg)
return
return nil
}

args := make([]string, 0, len(os.Args))
Expand All @@ -144,22 +146,20 @@ func runInBackground(logger *log.Logger) {
cmd.SysProcAttr = &syscall.SysProcAttr{Setsid: true}

if err := cmd.Start(); err != nil {
logger.Errorf(err, "failed to start background process")
return
return fmt.Errorf("failed to start background process: %w", err)
}

pidFilePath, _ := pidFilePath()
if err := os.MkdirAll(filepath.Dir(pidFilePath), 0750); err != nil {
logger.Errorf(err, "failed to create directory for pid file")
return
return fmt.Errorf("failed to create directory for pid file: %w", err)
}

if err := os.WriteFile(pidFilePath, []byte(strconv.Itoa(cmd.Process.Pid)), 0600); err != nil {
logger.Errorf(err, "failed to write pid file")
return
return fmt.Errorf("failed to write pid file: %w", err)
}

logger.Infof("`ftl serve` running in background with pid: %d", cmd.Process.Pid)
return nil
}

func KillBackgroundServe(logger *log.Logger) error {
Expand Down

0 comments on commit 866e079

Please sign in to comment.