Skip to content

Commit

Permalink
feat: Batch support (#779)
Browse files Browse the repository at this point in the history
Co-authored-by: Ryan Cartwright <[email protected]>
Co-authored-by: David Moore <[email protected]>
  • Loading branch information
3 people authored Oct 3, 2024
1 parent b605a39 commit 47630a9
Show file tree
Hide file tree
Showing 39 changed files with 2,139 additions and 311 deletions.
20 changes: 12 additions & 8 deletions cmd/debug.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import (
"os"
"strings"

"github.com/samber/lo"
"github.com/spf13/afero"
"github.com/spf13/cobra"
"google.golang.org/protobuf/encoding/protojson"
Expand Down Expand Up @@ -64,6 +65,11 @@ var specCmd = &cobra.Command{
buildUpdates, err := proj.BuildServices(fs)
tui.CheckErr(err)

batchBuildUpdates, err := proj.BuildBatches(fs)
tui.CheckErr(err)

allBuildUpdates := lo.FanIn(10, buildUpdates, batchBuildUpdates)

if isNonInteractive() {
fmt.Println("building project services")
for _, service := range proj.GetServices() {
Expand All @@ -77,7 +83,7 @@ var specCmd = &cobra.Command{
}
}
} else {
prog := teax.NewProgram(build.NewModel(buildUpdates, "Building Services"))
prog := teax.NewProgram(build.NewModel(allBuildUpdates, "Building Services"))
// blocks but quits once the above updates channel is closed by the build process
buildModel, err := prog.Run()
tui.CheckErr(err)
Expand All @@ -91,6 +97,9 @@ var specCmd = &cobra.Command{
serviceRequirements, err := proj.CollectServicesRequirements()
tui.CheckErr(err)

batchRequirements, err := proj.CollectBatchRequirements()
tui.CheckErr(err)

additionalEnvFiles := []string{}

if debugEnvFile != "" {
Expand All @@ -106,12 +115,7 @@ var specCmd = &cobra.Command{
envVariables = map[string]string{}
}

defaultImageName, ok := proj.DefaultMigrationImage(fs)
if !ok {
defaultImageName = ""
}

migrationImageContexts, err := collector.GetMigrationImageBuildContexts(serviceRequirements, fs)
migrationImageContexts, err := collector.GetMigrationImageBuildContexts(serviceRequirements, batchRequirements, fs)
tui.CheckErr(err)
// Build images from contexts and provide updates on the builds

Expand Down Expand Up @@ -143,7 +147,7 @@ var specCmd = &cobra.Command{
outputFile = "./nitric-spec.json"
}

spec, err := collector.ServiceRequirementsToSpec(proj.Name, envVariables, serviceRequirements, defaultImageName)
spec, err := collector.ServiceRequirementsToSpec(proj.Name, envVariables, serviceRequirements, batchRequirements)
tui.CheckErr(err)

marshaler := protojson.MarshalOptions{
Expand Down
17 changes: 16 additions & 1 deletion cmd/run.go
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,12 @@ var runCmd = &cobra.Command{
updates, err := proj.BuildServices(fs)
tui.CheckErr(err)

prog := teax.NewProgram(build.NewModel(updates, "Building Services"))
batchBuildUpdates, err := proj.BuildBatches(fs)
tui.CheckErr(err)

allBuildUpdates := lo.FanIn(10, updates, batchBuildUpdates)

prog := teax.NewProgram(build.NewModel(allBuildUpdates, "Building Services"))
// blocks but quits once the above updates channel is closed by the build process
_, err = prog.Run()
tui.CheckErr(err)
Expand All @@ -145,6 +150,16 @@ var runCmd = &cobra.Command{
}
}()

go func() {
err := proj.RunBatches(localCloud, stopChan, updatesChan, loadEnv)
if err != nil {
localCloud.Stop()

tui.CheckErr(err)
}
}()

tui.CheckErr(err)
// FIXME: This is a hack to get labelled logs into the TUI
// We should refactor the system logs to be more generic
systemChan := make(chan project.ServiceRunUpdate)
Expand Down
22 changes: 13 additions & 9 deletions cmd/stack.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import (
"strings"

"github.com/charmbracelet/lipgloss"
"github.com/samber/lo"
"github.com/spf13/afero"
"github.com/spf13/cobra"
"google.golang.org/protobuf/types/known/structpb"
Expand Down Expand Up @@ -198,20 +199,25 @@ var stackUpdateCmd = &cobra.Command{
buildUpdates, err := proj.BuildServices(fs)
tui.CheckErr(err)

batchBuildUpdates, err := proj.BuildBatches(fs)
tui.CheckErr(err)

allBuildUpdates := lo.FanIn(10, buildUpdates, batchBuildUpdates)

if isNonInteractive() {
fmt.Println("building project services")
for _, service := range proj.GetServices() {
fmt.Printf("service matched '%s', auto-naming this service '%s'\n", service.GetFilePath(), service.Name)
}

// non-interactive environment
for update := range buildUpdates {
for update := range allBuildUpdates {
for _, line := range strings.Split(strings.TrimSuffix(update.Message, "\n"), "\n") {
fmt.Printf("%s [%s]: %s\n", update.ServiceName, update.Status, line)
}
}
} else {
prog := teax.NewProgram(build.NewModel(buildUpdates, "Building Services"))
prog := teax.NewProgram(build.NewModel(allBuildUpdates, "Building Services"))
// blocks but quits once the above updates channel is closed by the build process
buildModel, err := prog.Run()
tui.CheckErr(err)
Expand All @@ -225,6 +231,9 @@ var stackUpdateCmd = &cobra.Command{
serviceRequirements, err := proj.CollectServicesRequirements()
tui.CheckErr(err)

batchRequirements, err := proj.CollectBatchRequirements()
tui.CheckErr(err)

additionalEnvFiles := []string{}

if envFile != "" {
Expand All @@ -245,12 +254,7 @@ var stackUpdateCmd = &cobra.Command{
envVariables["NITRIC_BETA_PROVIDERS"] = "true"
}

defaultImageName, ok := proj.DefaultMigrationImage(fs)
if !ok {
defaultImageName = ""
}

migrationImageContexts, err := collector.GetMigrationImageBuildContexts(serviceRequirements, fs)
migrationImageContexts, err := collector.GetMigrationImageBuildContexts(serviceRequirements, batchRequirements, fs)
tui.CheckErr(err)
// Build images from contexts and provide updates on the builds

Expand All @@ -277,7 +281,7 @@ var stackUpdateCmd = &cobra.Command{
}
}

spec, err := collector.ServiceRequirementsToSpec(proj.Name, envVariables, serviceRequirements, defaultImageName)
spec, err := collector.ServiceRequirementsToSpec(proj.Name, envVariables, serviceRequirements, batchRequirements)
tui.CheckErr(err)

providerStdout := make(chan string)
Expand Down
9 changes: 9 additions & 0 deletions cmd/start.go
Original file line number Diff line number Diff line change
Expand Up @@ -223,6 +223,15 @@ var startCmd = &cobra.Command{
tui.CheckErr(err)
}
}()
// FIXME: Duplicate code
go func() {
err := proj.RunBatchesWithCommand(localCloud, stopChan, updatesChan, localEnv)
if err != nil {
localCloud.Stop()

tui.CheckErr(err)
}
}()

// FIXME: This is a hack to get labelled logs into the TUI
// We should refactor the system logs to be more generic
Expand Down
Loading

0 comments on commit 47630a9

Please sign in to comment.