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

running bass scripts waits on any concurrently started thunks #209

Merged
merged 4 commits into from
Jul 4, 2022
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
3 changes: 2 additions & 1 deletion cmd/bass/bump.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,14 @@ import (

"github.com/protocolbuffers/txtpbfmt/parser"
"github.com/vito/bass/pkg/bass"
"github.com/vito/bass/pkg/cli"
"github.com/vito/bass/pkg/proto"
"github.com/vito/progrock"
"google.golang.org/protobuf/encoding/prototext"
)

func bump(ctx context.Context) error {
return withProgress(ctx, "export", func(ctx context.Context, vertex *progrock.VertexRecorder) error {
return cli.Task(ctx, cmdline, func(ctx context.Context, vertex *progrock.VertexRecorder) error {
lockContent, err := os.ReadFile(bumpLock)
if err != nil {
return err
Expand Down
3 changes: 2 additions & 1 deletion cmd/bass/export.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,12 @@ import (
"github.com/mattn/go-isatty"
"github.com/tonistiigi/units"
"github.com/vito/bass/pkg/bass"
"github.com/vito/bass/pkg/cli"
"github.com/vito/progrock"
)

func export(ctx context.Context) error {
return withProgress(ctx, "export", func(ctx context.Context, vertex *progrock.VertexRecorder) error {
return cli.Task(ctx, cmdline, func(ctx context.Context, vertex *progrock.VertexRecorder) error {
dec := bass.NewRawDecoder(os.Stdin)

var msg json.RawMessage
Expand Down
22 changes: 11 additions & 11 deletions cmd/bass/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
_ "net/http/pprof"
"os"
"runtime/pprof"
"strings"

flag "github.com/spf13/pflag"
"github.com/vito/bass/pkg/bass"
Expand All @@ -18,6 +19,7 @@ import (
)

var flags = flag.NewFlagSet(os.Args[0], flag.ContinueOnError)
var cmdline = strings.Join(os.Args, " ")

var inputs []string

Expand Down Expand Up @@ -140,42 +142,40 @@ func root(ctx context.Context) error {
ctx = bass.WithRuntimePool(ctx, pool)

if runnerAddr != "" {
return runnerLoop(ctx, runnerAddr, pool.Runtimes)
return cli.WithProgress(ctx, func(ctx context.Context) error {
return runnerLoop(ctx, pool.Runtimes)
})
}

if runExport {
return export(ctx)
return cli.WithProgress(ctx, export)
}

if runPrune {
return prune(ctx)
return cli.WithProgress(ctx, prune)
}

if runLSP {
return langServer(ctx)
}

if bumpLock != "" {
return bump(ctx)
return cli.WithProgress(ctx, bump)
}

argv := flags.Args()

if len(argv) == 0 {
if flags.NArg() == 0 {
return repl(ctx)
}

return run(ctx, argv[0], argv[1:]...)
return cli.WithProgress(ctx, run)
}

func repl(ctx context.Context) error {
env := bass.ImportSystemEnv()

scope := bass.NewRunScope(bass.Ground, bass.RunState{
Dir: bass.NewHostDir("."),
Stdin: bass.Stdin,
Stdout: bass.Stdout,
Env: env,
Env: bass.ImportSystemEnv(),
})

return cli.Repl(ctx, scope)
Expand Down
67 changes: 0 additions & 67 deletions cmd/bass/progress.go

This file was deleted.

3 changes: 2 additions & 1 deletion cmd/bass/prune.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,12 @@ import (
"fmt"

"github.com/vito/bass/pkg/bass"
"github.com/vito/bass/pkg/cli"
"github.com/vito/progrock"
)

func prune(ctx context.Context) error {
return withProgress(ctx, "prune", func(ctx context.Context, vertex *progrock.VertexRecorder) error {
return cli.Task(ctx, cmdline, func(ctx context.Context, vertex *progrock.VertexRecorder) error {
pool, err := bass.RuntimePoolFromContext(ctx)
if err != nil {
return err
Expand Down
61 changes: 11 additions & 50 deletions cmd/bass/run.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,70 +3,31 @@ package main
import (
"context"
"os"
"path/filepath"

"github.com/mattn/go-isatty"
"github.com/vito/bass/pkg/bass"
"github.com/vito/bass/pkg/cli"
"github.com/vito/progrock"
)

func run(ctx context.Context, filePath string, argv ...string) error {
args := []bass.Value{}
for _, arg := range argv {
args = append(args, bass.String(arg))
}

analogousThunk := bass.Thunk{
Cmd: bass.ThunkCmd{
Host: &bass.HostPath{
ContextDir: filepath.Dir(filePath),
Path: bass.FileOrDirPath{
File: &bass.FilePath{Path: filepath.Base(filePath)},
},
},
},
Args: args,
}

return withProgress(ctx, analogousThunk.Cmdline(), func(ctx context.Context, bassVertex *progrock.VertexRecorder) (err error) {
isTerm := isatty.IsTerminal(os.Stdout.Fd())

if !isTerm {
defer func() {
// ensure a chained unix pipeline exits
if err != nil && !isTerm {
os.Stdout.Close()
}
}()
}
func run(ctx context.Context) error {
return cli.Task(ctx, cmdline, func(ctx context.Context, vtx *progrock.VertexRecorder) error {
isTty := isatty.IsTerminal(os.Stdout.Fd())

stdout := bass.Stdout
if isTerm {
stdout = bass.NewSink(bass.NewJSONSink("stdout vertex", bassVertex.Stdout()))
if isTty {
stdout = bass.NewSink(bass.NewJSONSink("stdout vertex", vtx.Stdout()))
}

env := bass.ImportSystemEnv()

stdin := bass.Stdin
if len(inputs) > 0 {
stdin = cli.InputsSource(inputs)
}
argv := flags.Args()

scope := bass.NewRunScope(bass.Ground, bass.RunState{
Dir: bass.NewHostDir(filepath.Dir(filePath)),
Stdin: stdin,
Stdout: stdout,
Env: env,
})
err := cli.Run(ctx, bass.ImportSystemEnv(), inputs, argv[0], argv[1:], stdout)

source := bass.NewHostPath(filepath.Dir(filePath), bass.ParseFileOrDirPath(filepath.Base(filePath)))
_, err = bass.EvalFile(ctx, scope, filePath, source)
if err != nil {
return
if !isTty {
// ensure a chained unix pipeline exits
os.Stdout.Close()
}

err = bass.RunMain(ctx, scope, args...)
return
return err
})
}
10 changes: 4 additions & 6 deletions cmd/bass/runner.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import (
"strings"

"github.com/cenkalti/backoff/v4"
"github.com/vito/bass/pkg/cli"
"github.com/vito/bass/pkg/runtimes"
"github.com/vito/bass/pkg/zapctx"
"github.com/vito/progrock"
Expand All @@ -30,15 +31,12 @@ var defaultKeys = []string{
"id_rsa",
}

func runnerLoop(ctx context.Context, sshAddr string, assoc []runtimes.Assoc) error {
ctx, stop := signal.NotifyContext(ctx, os.Interrupt)
defer stop()

return withProgress(ctx, "runner", func(ctx context.Context, bassVertex *progrock.VertexRecorder) (err error) {
func runnerLoop(ctx context.Context, assoc []runtimes.Assoc) error {
return cli.Task(ctx, cmdline, func(ctx context.Context, bassVertex *progrock.VertexRecorder) (err error) {
exp := backoff.NewExponentialBackOff()
exp.MaxElapsedTime = 0 // https://www.youtube.com/watch?v=6BtuqUX934U
return backoff.Retry(func() error {
return runner(ctx, sshAddr, assoc)
return runner(ctx, runnerAddr, assoc)
}, backoff.WithContext(exp, ctx))
})
}
Expand Down
14 changes: 14 additions & 0 deletions pkg/bass/cache_path.go
Original file line number Diff line number Diff line change
Expand Up @@ -133,3 +133,17 @@ func (path CachePath) Extend(ext Path) (Path, error) {

return extended, nil
}

func (value CachePath) Dir() CachePath {
cp := value

if value.Path.Dir != nil {
parent := value.Path.Dir.Dir()
cp.Path = FileOrDirPath{Dir: &parent}
} else {
parent := value.Path.File.Dir()
cp.Path = FileOrDirPath{Dir: &parent}
}

return cp
}
14 changes: 14 additions & 0 deletions pkg/bass/host_path.go
Original file line number Diff line number Diff line change
Expand Up @@ -163,6 +163,20 @@ func (path HostPath) Open(context.Context) (io.ReadCloser, error) {
return os.Open(realPath)
}

func (value HostPath) Dir() HostPath {
cp := value

if value.Path.Dir != nil {
parent := value.Path.Dir.Dir()
cp.Path = FileOrDirPath{Dir: &parent}
} else {
parent := value.Path.File.Dir()
cp.Path = FileOrDirPath{Dir: &parent}
}

return cp
}

func (value HostPath) fpath() string {
return filepath.Join(value.ContextDir, value.Path.FilesystemPath().FromSlash())
}
Expand Down
Loading