Skip to content

Commit

Permalink
fix: return error if ftl serve is already running (#1040)
Browse files Browse the repository at this point in the history
  • Loading branch information
wesbillman authored Mar 7, 2024
1 parent c94e185 commit e8b0a36
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 9 deletions.
14 changes: 9 additions & 5 deletions cmd/ftl/cmd_dev.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package main

import (
"context"
"errors"
"time"

"golang.org/x/sync/errgroup"
Expand All @@ -20,25 +21,28 @@ type devCmd struct {

func (d *devCmd) Run(ctx context.Context) error {
client := rpc.ClientFromContext[ftlv1connect.ControllerServiceClient](ctx)
engine, err := buildengine.New(ctx, client, d.Dirs...)
if err != nil {
return err
}

g, ctx := errgroup.WithContext(ctx)

if !d.NoServe {
if d.ServeCmd.isRunning(ctx, client) {
return errors.New("FTL is already running")
}
g.Go(func() error {
return d.ServeCmd.Run(ctx)
})
}

err = d.ServeCmd.pollControllerOnine(ctx, client)
err := d.ServeCmd.pollControllerOnine(ctx, client)
if err != nil {
return err
}

g.Go(func() error {
engine, err := buildengine.New(ctx, client, d.Dirs...)
if err != nil {
return err
}
return engine.Dev(ctx, d.Watch)
})

Expand Down
17 changes: 13 additions & 4 deletions cmd/ftl/cmd_serve.go
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,10 @@ func (s *serveCmd) Run(ctx context.Context) error {
return killBackgroundProcess(logger)
}

if s.isRunning(ctx, client) {
return errors.New("FTL is already running")
}

logger.Infof("Starting FTL with %d controller(s) and %d runner(s)", s.Controllers, s.Runners)

dsn, err := s.setupDB(ctx)
Expand Down Expand Up @@ -179,16 +183,16 @@ func killBackgroundProcess(logger *log.Logger) error {
return nil
}

if err := os.Remove(pidFilePath); err != nil {
logger.Errorf(err, "Failed to remove pid file: %v", err)
}

if err := syscall.Kill(pid, syscall.SIGTERM); err != nil {
if !errors.Is(err, syscall.ESRCH) {
return err
}
}

if err := os.Remove(pidFilePath); err != nil {
logger.Errorf(err, "Failed to remove pid file: %v", err)
}

logger.Infof("`ftl serve` stopped (pid: %d)", pid)
return nil
}
Expand Down Expand Up @@ -377,3 +381,8 @@ func (s *serveCmd) pollControllerOnine(ctx context.Context, client ftlv1connect.
}
}
}

func (s *serveCmd) isRunning(ctx context.Context, client ftlv1connect.ControllerServiceClient) bool {
_, err := client.Ping(ctx, connect.NewRequest(&ftlv1.PingRequest{}))
return err == nil
}

0 comments on commit e8b0a36

Please sign in to comment.