Skip to content

Commit

Permalink
refactor: remove Engine.Add, dirs are passed to the constructor (#992)
Browse files Browse the repository at this point in the history
  • Loading branch information
alecthomas authored Feb 27, 2024
1 parent c3f98c3 commit 8c03e7b
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 41 deletions.
46 changes: 20 additions & 26 deletions buildengine/engine.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,21 +32,34 @@ type Engine struct {
// Completely offline builds are possible if the full dependency graph is
// locally available. If the FTL controller is available, it will be used to
// pull in missing schemas.
func New(ctx context.Context, client ftlv1connect.ControllerServiceClient) (*Engine, error) {
//
// "dirs" are directories to scan for local modules.
func New(ctx context.Context, client ftlv1connect.ControllerServiceClient, dirs ...string) (*Engine, error) {
ctx = rpc.ContextWithClient(ctx, client)
engine := &Engine{
e := &Engine{
modules: map[string]Module{},
controllerSchema: xsync.NewMapOf[*schema.Module](),
}
engine.controllerSchema.Store("builtin", schema.Builtins())
e.controllerSchema.Store("builtin", schema.Builtins())
ctx, cancel := context.WithCancel(ctx)
engine.cancel = cancel
e.cancel = cancel
configs, err := DiscoverModules(ctx, dirs...)
if err != nil {
return nil, err
}
modules, err := UpdateAllDependencies(ctx, configs...)
if err != nil {
return nil, err
}
for _, module := range modules {
e.modules[module.Module] = module
}
if client == nil {
return engine, nil
return e, nil
}
schemaSync := engine.startSchemaSync(ctx, client)
schemaSync := e.startSchemaSync(ctx, client)
go rpc.RetryStreamingServerStream(ctx, backoff.Backoff{Max: time.Second}, &ftlv1.PullSchemaRequest{}, client.PullSchema, schemaSync)
return engine, nil
return e, nil
}

// Sync module schema changes from the FTL controller, as well as from manual
Expand Down Expand Up @@ -128,25 +141,6 @@ func (e *Engine) buildGraph(name string, out map[string][]string) error {
return nil
}

// Add a directory of local modules to the engine.
//
// Existing modules will be replaced. A local module will take precedence over
// any schema from the FTL cluster, building as required.
func (e *Engine) Add(ctx context.Context, dir string) error {
configs, err := DiscoverModules(ctx, dir)
if err != nil {
return err
}
modules, err := UpdateAllDependencies(ctx, configs...)
if err != nil {
return err
}
for _, module := range modules {
e.modules[module.Module] = module
}
return nil
}

// Import manually imports a schema for a module as if it were retrieved from
// the FTL controller.
func (e *Engine) Import(ctx context.Context, schema *schema.Module) {
Expand Down
8 changes: 1 addition & 7 deletions buildengine/engine_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,17 +13,11 @@ import (

func TestEngine(t *testing.T) {
ctx := log.ContextWithNewDefaultLogger(context.Background())
engine, err := buildengine.New(ctx, nil)
engine, err := buildengine.New(ctx, nil, "testdata/modules/alpha", "testdata/modules/another")
assert.NoError(t, err)

defer engine.Close()

// Explicitly add only two of the modules.
err = engine.Add(ctx, "testdata/modules/alpha")
assert.NoError(t, err)
err = engine.Add(ctx, "testdata/modules/another")
assert.NoError(t, err)

// Import the schema from the third module, simulating a remote schema.
otherSchema := &schema.Module{
Name: "other",
Expand Down
9 changes: 1 addition & 8 deletions cmd/ftl/cmd_build.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package main

import (
"context"
"fmt"

"github.com/TBD54566975/ftl/backend/protos/xyz/block/ftl/v1/ftlv1connect"
"github.com/TBD54566975/ftl/buildengine"
Expand All @@ -15,15 +14,9 @@ type buildCmd struct {

func (b *buildCmd) Run(ctx context.Context) error {
client := rpc.ClientFromContext[ftlv1connect.ControllerServiceClient](ctx)
engine, err := buildengine.New(ctx, client)
engine, err := buildengine.New(ctx, client, b.Dirs...)
if err != nil {
return err
}
for _, dir := range b.Dirs {
err = engine.Add(ctx, dir)
if err != nil {
return fmt.Errorf("%s: %w", dir, err)
}
}
return engine.Build(ctx)
}

0 comments on commit 8c03e7b

Please sign in to comment.