Skip to content

Commit

Permalink
fix: add a build.lock file to gate the buildengine
Browse files Browse the repository at this point in the history
  • Loading branch information
safeer committed Jun 5, 2024
1 parent f04a921 commit 5e31588
Show file tree
Hide file tree
Showing 5 changed files with 54 additions and 1 deletion.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -24,3 +24,4 @@ buildengine/.gitignore
go.work*
junit*.xml
/readme-tests
_ftl/
36 changes: 35 additions & 1 deletion buildengine/engine.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"crypto/sha256"
"errors"
"fmt"
"os"
"path/filepath"
"runtime"
"strings"
Expand All @@ -22,6 +23,7 @@ import (
"github.com/TBD54566975/ftl/backend/protos/xyz/block/ftl/v1/ftlv1connect"
"github.com/TBD54566975/ftl/backend/schema"
"github.com/TBD54566975/ftl/common/projectconfig"
"github.com/TBD54566975/ftl/internal"
"github.com/TBD54566975/ftl/internal/exec"
"github.com/TBD54566975/ftl/internal/log"
"github.com/TBD54566975/ftl/internal/rpc"
Expand Down Expand Up @@ -83,6 +85,13 @@ func WithListener(listener Listener) Option {
//
// "dirs" are directories to scan for local modules.
func New(ctx context.Context, client ftlv1connect.ControllerServiceClient, moduleDirs []string, externalDirs []string, options ...Option) (*Engine, error) {
logger := log.FromContext(ctx)
lockFile, err := createLockFile()
if err != nil {
return nil, err
}
logger.Infof("Created build.lock file at %s", lockFile)

ctx = rpc.ContextWithClient(ctx, client)
e := &Engine{
client: client,
Expand Down Expand Up @@ -161,12 +170,37 @@ func (e *Engine) startSchemaSync(ctx context.Context) func(ctx context.Context,
}
}

// Close stops the Engine's schema sync.
// Close stops the Engine's schema sync and deletes the build lock file.
func (e *Engine) Close() error {
if err := deleteLockFile(); err != nil {
fmt.Println("deleteLockFile: %w", err)
}
e.cancel()
return nil
}

func createLockFile() (string, error) {
lockFile := filepath.Join(internal.GitRoot(""), "_ftl", "build.lock")
if err := os.MkdirAll(filepath.Dir(lockFile), 0750); err != nil {
return "", fmt.Errorf("failed to create directory for build.lock file: %w", err)
}
if _, err := os.Stat(lockFile); err == nil || !errors.Is(err, os.ErrNotExist) {
return "", fmt.Errorf("build.lock file already exists at: %s", lockFile)
}
if err := os.WriteFile(lockFile, []byte(""), 0640); err != nil {
return "", fmt.Errorf("failed to write build.lock file: %w", err)
}
return lockFile, nil
}

func deleteLockFile() error {
lockFile := filepath.Join(internal.GitRoot(""), "_ftl", "build.lock")
if err := os.Remove(lockFile); err != nil {
return fmt.Errorf("failed to remove build.lock file: %w", err)
}
return nil
}

// Graph returns the dependency graph for the given modules.
//
// If no modules are provided, the entire graph is returned. An error is returned if
Expand Down
1 change: 1 addition & 0 deletions cmd/ftl/cmd_build.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,5 +29,6 @@ func (b *buildCmd) Run(ctx context.Context, projConfig projectconfig.Config) err
if err != nil {
return err
}
defer engine.Close()
return engine.Build(ctx)
}
1 change: 1 addition & 0 deletions cmd/ftl/cmd_deploy.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,5 +21,6 @@ func (d *deployCmd) Run(ctx context.Context) error {
if err != nil {
return err
}
defer engine.Close()
return engine.Deploy(ctx, d.Replicas, !d.NoWait)
}
16 changes: 16 additions & 0 deletions cmd/ftl/cmd_dev.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,10 @@ package main
import (
"context"
"errors"
"fmt"
"os"
"os/signal"
"syscall"
"time"

"golang.org/x/sync/errgroup"
Expand Down Expand Up @@ -58,6 +62,9 @@ func (d *devCmd) Run(ctx context.Context, projConfig projectconfig.Config) error
g.Go(func() error { return d.ServeCmd.Run(ctx) })
}

sigs := make(chan os.Signal, 1)
signal.Notify(sigs, syscall.SIGINT, syscall.SIGTERM)

g.Go(func() error {
err := waitForControllerOnline(ctx, d.ServeCmd.StartupTimeout, client)
if err != nil {
Expand All @@ -78,6 +85,15 @@ func (d *devCmd) Run(ctx context.Context, projConfig projectconfig.Config) error
if err != nil {
return err
}

// Close the engine on an interrupt signal.
go func() {
<-sigs
err = engine.Close()
fmt.Println("Interrupted, closing engine.")
}()

defer engine.Close()
return engine.Dev(ctx, d.Watch, projConfig.Commands)
})

Expand Down

0 comments on commit 5e31588

Please sign in to comment.