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

addition of signadot job submit -f ... --wait #152

Merged
merged 2 commits into from
Jan 7, 2025
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
56 changes: 54 additions & 2 deletions internal/command/jobs/submit.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"errors"
"fmt"
"io"
"time"

"github.com/signadot/cli/internal/config"
"github.com/signadot/cli/internal/print"
Expand Down Expand Up @@ -36,6 +37,9 @@ func submit(ctx context.Context, cfg *config.JobSubmit, outW, errW io.Writer) er
if cfg.Filename == "" {
return errors.New("must specify job request file with '-f' flag")
}
if cfg.Wait && cfg.Attach {
return errors.New("cannot specify both --attach and --wait")
}
req, err := loadJob(cfg.Filename, cfg.TemplateVals, false /*forDelete */)
if err != nil {
return err
Expand All @@ -48,15 +52,63 @@ func submit(ctx context.Context, cfg *config.JobSubmit, outW, errW io.Writer) er
return err
}
resp := result.Payload
if cfg.Wait {
job, err := waitJob(ctx, cfg, resp.Name)
if err != nil {
writeOutput(ctx, cfg, outW, errW, resp)
return fmt.Errorf("error waiting for job %q: %w", resp.Name, err)
}
resp = job
}

return writeOutput(ctx, cfg, outW, errW, resp)
err = writeOutput(ctx, cfg, outW, errW, resp)
if err != nil {
return err
}
if cfg.Wait {
switch ph := resp.Status.Attempts[0].Phase; ph {
case "canceled", "failed":
return fmt.Errorf("job %q %s", resp.Name, ph)
}
}
return nil
}

func waitJob(ctx context.Context, cfg *config.JobSubmit, name string) (*models.Job, error) {

ticker := time.NewTicker(time.Second / 5)
scott-cotton marked this conversation as resolved.
Show resolved Hide resolved
defer ticker.Stop()
params := &jobs.GetJobParams{
JobName: name,
OrgName: cfg.Org,
Context: ctx,
}
for {
res, err := cfg.Client.Jobs.GetJob(params, nil)
if err != nil {
return nil, err
}
attempts := res.Payload.Status.Attempts
if len(attempts) == 0 {
return nil, fmt.Errorf("no attempts in job %q", name)
}
switch attempts[0].Phase {
case "failed", "succeeded", "canceled":
return res.Payload, nil
}
select {
case <-ticker.C:
case <-ctx.Done():
return nil, ctx.Err()
}
}
}

func writeOutput(ctx context.Context, cfg *config.JobSubmit, outW, errW io.Writer, resp *models.Job) error {
switch cfg.OutputFormat {
case config.OutputFormatDefault:
// Print info on how to access the job.
fmt.Fprintf(outW, "Job %s queued on Job Runner Group: %s\n", resp.Name, resp.Spec.RunnerGroup)
fmt.Fprintf(outW, "Job %s %s on Job Runner Group: %s\n", resp.Name, resp.Status.Attempts[0].Phase, resp.Spec.RunnerGroup)
fmt.Fprintf(outW, "\nDashboard page: %v\n\n", cfg.JobDashboardUrl(resp.Name))

var err error
Expand Down
2 changes: 2 additions & 0 deletions internal/config/job.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ type JobSubmit struct {
Attach bool
Timeout time.Duration
TemplateVals TemplateVals
Wait bool
}

func (c *JobSubmit) AddFlags(cmd *cobra.Command) {
Expand All @@ -26,6 +27,7 @@ func (c *JobSubmit) AddFlags(cmd *cobra.Command) {
cmd.Flags().Var(&c.TemplateVals, "set", "--set var=val")
cmd.Flags().BoolVar(&c.Attach, "attach", false, "waits until the job is completed, displaying the stdout and stderr streams")
cmd.Flags().DurationVar(&c.Timeout, "timeout", 0, "timeout when waiting for the job to be started, if 0 is specified, no timeout will be applied and the command will wait until completion or cancellation of the job (default 0)")
Copy link
Member

@foxish foxish Jan 6, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In sb apply, we have --wait and --wait-timeout. Here, we have this timeout property that looks sort of related to wait. I'm curious whether this timeout is the equivalent of --wait-timeout when using with --wait?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yes, it is. I guess we could add an alias or something to make it more uniform

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I double checked this and

  1. I corrected the description, which was improperly worded before this PR, now it applies to both --attach and --wait
  2. I fixed how --timeout applies to --wait; I had tested that but I guess I got the test wrong or it was on a dev version. Now it actually checks the timeout when non-zero during the --wait poll.

cmd.Flags().BoolVar(&c.Wait, "wait", false, "waits until the job is completed")
}

type JobDelete struct {
Expand Down