Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: return error if ftl serve is already running #1040

Merged
merged 1 commit into from
Mar 7, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 @@ -69,6 +69,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 @@ -177,16 +181,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 @@ -375,3 +379,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
}