-
Notifications
You must be signed in to change notification settings - Fork 2
feat(workspace): support async, durable workspace up execution #798
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
Draft
Draft
Changes from all commits
Commits
Show all changes
21 commits
Select commit
Hold shift + click to select a range
791867d
feat(workspace): support async, durable workspace up execution
skevetter 65ce40f
fix(lint): satisfy golangci-lint on the async workspace-up changes
skevetter 5f6e271
fix(lint): drop forbidigo nolint by writing to os.Stdout directly
skevetter 6078bae
fix(e2e): implement the async task protocol in the mock devsy CLI
skevetter d212862
style: update comments
skevetter 5b95539
fix(task): preserve terminal task state and tighten status reporting
skevetter 480427b
fix(desktop): keep detached up tasks cancellable
skevetter 545ad00
refactor(status): move status package out of devcontainer
skevetter c7952dc
fix(task): reconcile abandoned tasks and close up/stop races
skevetter e742a9d
style: cleanup comments
skevetter eda77d1
style: fix formatting
skevetter deaeb7c
fix(task): use a worker-held lock for task liveness
skevetter 6807530
fix(task): hold the worker lock across the abandoned-task transition
skevetter 2b4ba67
style: update comments
skevetter 0d202d4
fix(task): fsync task state so a crash can't lose a terminal result
skevetter a9694b9
style: update comments
skevetter 416d00d
style: update podman variable
skevetter 6bee286
fix: apply testfile naming convention
skevetter 3f63b4d
fix(task): make the worker-lock test seam visible across packages
skevetter 183d5e7
style: trim explanatory comments to essentials
skevetter 4206641
fix(desktop): handle a log follower that fails to start
skevetter File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,341 @@ | ||
| package workspace | ||
|
|
||
| import ( | ||
| "context" | ||
| "encoding/json" | ||
| "fmt" | ||
| "os" | ||
| "time" | ||
|
|
||
| "github.com/devsy-org/devsy/cmd/flags" | ||
| config2 "github.com/devsy-org/devsy/pkg/devcontainer/config" | ||
| cliflags "github.com/devsy-org/devsy/pkg/flags" | ||
| "github.com/devsy-org/devsy/pkg/flags/names" | ||
| "github.com/devsy-org/devsy/pkg/output" | ||
| "github.com/devsy-org/devsy/pkg/status" | ||
| "github.com/devsy-org/devsy/pkg/task" | ||
| "github.com/spf13/cobra" | ||
| ) | ||
|
|
||
| // NewTaskCmd builds the workspace task parent command. | ||
| func NewTaskCmd(globalFlags *flags.GlobalFlags) *cobra.Command { | ||
| taskCmd := &cobra.Command{ | ||
| Use: "task", | ||
| Short: "Manage background tasks (e.g. from 'up --detach')", | ||
| } | ||
| taskCmd.AddCommand(newTaskListCmd(globalFlags)) | ||
| taskCmd.AddCommand(newTaskGetCmd(globalFlags)) | ||
| taskCmd.AddCommand(newTaskLogsCmd(globalFlags)) | ||
| taskCmd.AddCommand(newTaskCancelCmd(globalFlags)) | ||
| taskCmd.AddCommand(newTaskRmCmd(globalFlags)) | ||
| return taskCmd | ||
| } | ||
|
|
||
| type taskListCmd struct { | ||
| *flags.GlobalFlags | ||
| } | ||
|
|
||
| func newTaskListCmd(globalFlags *flags.GlobalFlags) *cobra.Command { | ||
| cmd := &taskListCmd{GlobalFlags: globalFlags} | ||
| return &cobra.Command{ | ||
| Use: "list", | ||
| Aliases: []string{"ls"}, | ||
| Short: "List background tasks, most recently started first", | ||
| Args: cobra.NoArgs, | ||
| RunE: func(*cobra.Command, []string) error { | ||
| return cmd.run() | ||
| }, | ||
| } | ||
| } | ||
|
|
||
| func (cmd *taskListCmd) run() error { | ||
| emitJSON, err := resolveEmitJSON(cmd.GlobalFlags) | ||
| if err != nil { | ||
| return err | ||
| } | ||
|
|
||
| store, err := task.NewStore() | ||
| if err != nil { | ||
| return err | ||
| } | ||
| states, err := store.List() | ||
| if err != nil { | ||
| return err | ||
| } | ||
|
|
||
| if emitJSON { | ||
| return json.NewEncoder(os.Stdout).Encode(states) | ||
| } | ||
| for _, s := range states { | ||
| _, _ = fmt.Fprintf(os.Stdout, "%s\t%s\t%s\t%s\n", s.ID, s.Status, s.Command, s.WorkspaceID) | ||
| } | ||
| return nil | ||
| } | ||
|
|
||
| type taskGetCmd struct { | ||
| *flags.GlobalFlags | ||
| } | ||
|
|
||
| func newTaskGetCmd(globalFlags *flags.GlobalFlags) *cobra.Command { | ||
| cmd := &taskGetCmd{GlobalFlags: globalFlags} | ||
| return &cobra.Command{ | ||
| Use: "get <task-id>", | ||
| Aliases: []string{"describe", "show"}, | ||
| Short: "Show a task's current status", | ||
| Args: cobra.ExactArgs(1), | ||
| RunE: func(_ *cobra.Command, args []string) error { | ||
| return cmd.run(args[0]) | ||
| }, | ||
| } | ||
| } | ||
|
|
||
| func (cmd *taskGetCmd) run(id string) error { | ||
| emitJSON, err := resolveEmitJSON(cmd.GlobalFlags) | ||
| if err != nil { | ||
| return err | ||
| } | ||
| store, err := task.NewStore() | ||
| if err != nil { | ||
| return err | ||
| } | ||
| state, err := store.Get(id) | ||
| if err != nil { | ||
| return err | ||
| } | ||
| return reportTaskState(state, emitJSON) | ||
| } | ||
|
|
||
| type taskLogsCmd struct { | ||
| *flags.GlobalFlags | ||
|
|
||
| Follow bool | ||
| Interval string | ||
| } | ||
|
|
||
| func newTaskLogsCmd(globalFlags *flags.GlobalFlags) *cobra.Command { | ||
| cmd := &taskLogsCmd{GlobalFlags: globalFlags} | ||
| logsCmd := &cobra.Command{ | ||
| Use: "logs <task-id>", | ||
| Aliases: []string{"attach"}, | ||
| Short: "Show a task's status, or follow it until it finishes", | ||
| Args: cobra.ExactArgs(1), | ||
| RunE: func(_ *cobra.Command, args []string) error { | ||
| return cmd.run(args[0]) | ||
| }, | ||
| } | ||
| cliflags.Add(logsCmd, | ||
| cliflags.Bool(&cmd.Follow, names.Follow, false, | ||
| "Poll until the task reaches a terminal state instead of reporting once"). | ||
| Shorthand("f"), | ||
| cliflags.String(&cmd.Interval, names.Interval, "500ms", | ||
| "Poll interval when --follow is set"), | ||
| ) | ||
| return logsCmd | ||
| } | ||
|
|
||
| func (cmd *taskLogsCmd) run(id string) error { | ||
| emitJSON, err := resolveEmitJSON(cmd.GlobalFlags) | ||
| if err != nil { | ||
| return err | ||
| } | ||
| store, err := task.NewStore() | ||
| if err != nil { | ||
| return err | ||
| } | ||
|
|
||
| if !cmd.Follow { | ||
| state, err := store.Get(id) | ||
| if err != nil { | ||
| return err | ||
| } | ||
| return reportTaskState(state, emitJSON) | ||
| } | ||
|
|
||
| interval, err := time.ParseDuration(cmd.Interval) | ||
| if err != nil { | ||
| return fmt.Errorf("parse --interval: %w", err) | ||
| } | ||
| if interval <= 0 { | ||
| return fmt.Errorf("--interval must be positive, got %q", cmd.Interval) | ||
| } | ||
| return followTask(context.Background(), store, followTaskOptions{ | ||
| id: id, | ||
| interval: interval, | ||
| emitJSON: emitJSON, | ||
| }) | ||
| } | ||
|
|
||
| type followTaskOptions struct { | ||
| id string | ||
| interval time.Duration | ||
| emitJSON bool | ||
| } | ||
|
|
||
| func followTask(ctx context.Context, store *task.Store, opts followTaskOptions) error { | ||
| var last *task.State | ||
| ticker := time.NewTicker(opts.interval) | ||
| defer ticker.Stop() | ||
|
|
||
| for { | ||
| state, err := store.Get(opts.id) | ||
| if err != nil { | ||
| return err | ||
| } | ||
| emitTaskTransition(last, state, opts.emitJSON) | ||
| last = state | ||
|
|
||
| if state.Status.Terminal() { | ||
| return reportTaskState(state, opts.emitJSON) | ||
| } | ||
|
|
||
| select { | ||
| case <-ctx.Done(): | ||
| return ctx.Err() | ||
| case <-ticker.C: | ||
| } | ||
| } | ||
| } | ||
|
|
||
| func emitTaskTransition(last, current *task.State, emitJSON bool) { | ||
| if last != nil && last.Phase == current.Phase && last.Step == current.Step { | ||
| return | ||
| } | ||
| if current.Phase == "" { | ||
| return | ||
| } | ||
| event := status.Event{Phase: status.Phase(current.Phase), Step: current.Step, Started: true} | ||
| if emitJSON { | ||
| _ = config2.WriteStatusJSON(os.Stdout, event) | ||
| return | ||
| } | ||
| _, _ = fmt.Fprintf(os.Stdout, "task %s: %s\n", current.ID, current.Phase) | ||
| } | ||
|
|
||
| type taskCancelCmd struct { | ||
| *flags.GlobalFlags | ||
| } | ||
|
|
||
| func newTaskCancelCmd(globalFlags *flags.GlobalFlags) *cobra.Command { | ||
| cmd := &taskCancelCmd{GlobalFlags: globalFlags} | ||
| return &cobra.Command{ | ||
| Use: "cancel <task-id>", | ||
| Aliases: []string{"stop"}, | ||
| Short: "Stop a task's process and mark it failed", | ||
| Args: cobra.ExactArgs(1), | ||
| RunE: func(_ *cobra.Command, args []string) error { | ||
| return cmd.run(args[0]) | ||
| }, | ||
| } | ||
| } | ||
|
|
||
| func (cmd *taskCancelCmd) run(id string) error { | ||
| emitJSON, err := resolveEmitJSON(cmd.GlobalFlags) | ||
| if err != nil { | ||
| return err | ||
| } | ||
| store, err := task.NewStore() | ||
| if err != nil { | ||
| return err | ||
| } | ||
| if err := store.Open(id).Cancel(); err != nil { | ||
| return err | ||
| } | ||
| state, err := store.Get(id) | ||
| if err != nil { | ||
| return err | ||
| } | ||
|
|
||
| // Report the raw state. Canceling is not itself a failure. | ||
| if emitJSON { | ||
| return json.NewEncoder(os.Stdout).Encode(state) | ||
| } | ||
| _, _ = fmt.Fprintf(os.Stdout, "task %s: canceled\n", state.ID) | ||
| return nil | ||
| } | ||
|
|
||
| type taskRmCmd struct { | ||
| *flags.GlobalFlags | ||
|
|
||
| Force bool | ||
| } | ||
|
|
||
| func newTaskRmCmd(globalFlags *flags.GlobalFlags) *cobra.Command { | ||
| cmd := &taskRmCmd{GlobalFlags: globalFlags} | ||
| rmCmd := &cobra.Command{ | ||
| Use: "rm <task-id>", | ||
| Aliases: []string{"delete", "remove"}, | ||
| Short: "Delete a finished task's state", | ||
| Args: cobra.ExactArgs(1), | ||
| RunE: func(_ *cobra.Command, args []string) error { | ||
| return cmd.run(args[0]) | ||
| }, | ||
| } | ||
| cliflags.Add(rmCmd, | ||
| cliflags.Bool(&cmd.Force, names.Force, false, | ||
| "Stop the task first if it's still pending or running, then delete"), | ||
| ) | ||
| return rmCmd | ||
| } | ||
|
|
||
| func (cmd *taskRmCmd) run(id string) error { | ||
| store, err := task.NewStore() | ||
| if err != nil { | ||
| return err | ||
| } | ||
| if cmd.Force { | ||
| if err := store.Open(id).Cancel(); err != nil { | ||
| return err | ||
| } | ||
| } | ||
| return store.Delete(id, cmd.Force) | ||
| } | ||
|
|
||
| func resolveEmitJSON(g *flags.GlobalFlags) (bool, error) { | ||
| mode, err := output.ResolveMode(g.ResultFormat) | ||
| if err != nil { | ||
| return false, err | ||
| } | ||
| return mode == output.ModeJSON, nil | ||
| } | ||
|
|
||
| func reportTaskState(state *task.State, emitJSON bool) error { | ||
| if emitJSON { | ||
| return reportTaskStateJSON(state) | ||
| } | ||
|
|
||
| _, _ = fmt.Fprintf(os.Stdout, "task %s: %s\n", state.ID, state.Status) | ||
| if state.Status == task.StatusFailed { | ||
| return fmt.Errorf("%s", state.Error) | ||
| } | ||
| return nil | ||
| } | ||
|
|
||
| func reportTaskStateJSON(state *task.State) error { | ||
| switch state.Status { | ||
| case task.StatusFailed: | ||
| if err := config2.WriteErrorJSON(os.Stdout, state.Error); err != nil { | ||
| return err | ||
| } | ||
| return fmt.Errorf("%s", state.Error) | ||
| case task.StatusSucceeded: | ||
| return config2.WriteResultJSON(os.Stdout, resultEnvelopeFrom(state)) | ||
| default: | ||
| return config2.WriteStatusJSON(os.Stdout, status.Event{ | ||
| Phase: status.Phase(state.Phase), | ||
| Step: state.Step, | ||
| Started: true, | ||
| }) | ||
| } | ||
| } | ||
|
|
||
| func resultEnvelopeFrom(state *task.State) config2.ResultEnvelope { | ||
| if state.Result == nil { | ||
| return config2.ResultEnvelope{} | ||
| } | ||
| return config2.ResultEnvelope{ | ||
| ContainerID: config2.GetContainerID(state.Result), | ||
| RemoteUser: config2.GetRemoteUser(state.Result), | ||
| Warnings: state.Result.HostWarnings, | ||
| Recovery: state.Result.RecoveryContainer, | ||
| } | ||
| } | ||
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.