mirror of
https://github.com/coder/coder.git
synced 2026-06-02 20:48:20 +00:00
ce627bf23f
closes: https://github.com/coder/coder/issues/10352 closes: https://github.com/coder/internal/issues/1094 closes: https://github.com/coder/internal/issues/1095 In this pull request, we enable a new set of experimental cli commands grouped under `coder exp sync`. These commands allow any process acting within a coder workspace to inform the coder agent of its requirements and execution progress. The coder agent will then relay this information to other processes that have subscribed. These commands are: ``` # Check if this feature is enabled in your environment coder exp sync ping # express that your unit depends on another coder exp sync want <unit> <dependency_unit> # express that your unit intends to start a portion of the script that requires # other units to have completed first. This command blocks until all dependencies have been met coder exp sync start <unit> # express that your unit has completes its work, allowing dependent units to begin their execution coder exp sync complete <unit> ``` Example: In order to automatically run claude code in a new workspace, it must first have a git repository cloned. The scripts responsible for cloning the repository and for running claude code would coordinate in the following way: ```bash # Script A: Claude code # Inform the agent that the claude script wants the git script. # That is, the git script must have completed before the claude script can begin its execution coder exp sync want claude git # Inform the agent that we would now like to begin execution of claude. # This command will block until the git script (and any other defined dependencies) # have completed coder exp sync start claude # Now we run claude code and any other commands we need claude ... # Once our script has completed, we inform the agent, so that any scripts that depend on this one # may begin their execution coder exp sync complete claude ``` ```bash # Script B: Git # Because the git script does not have any dependencies, we can simply inform the agent that we # intend to start coder exp sync start git git clone ssh://git@github.com/coder/coder # Once the repository have been cloned, we inform the agent that this script is complete, so that # scripts that depend on it may begin their execution. coder exp sync complete git ``` Notes: * Unit names (ie. `claude` and `git`) given as input to the sync commands are arbitrary strings. You do not have to conform to specific identifiers. We recommend naming your scripts descriptively, but succinctly. * Scripts unit names should be well documented. Other scripts will need to know the names you've chosen in order to depend on yours. Therefore, you --------- Co-authored-by: Mathias Fredriksson <mafredri@gmail.com>
102 lines
2.5 KiB
Go
102 lines
2.5 KiB
Go
package cli
|
|
|
|
import (
|
|
"context"
|
|
"time"
|
|
|
|
"golang.org/x/xerrors"
|
|
|
|
"github.com/coder/serpent"
|
|
|
|
"github.com/coder/coder/v2/agent/agentsocket"
|
|
"github.com/coder/coder/v2/agent/unit"
|
|
"github.com/coder/coder/v2/cli/cliui"
|
|
)
|
|
|
|
const (
|
|
syncPollInterval = 1 * time.Second
|
|
)
|
|
|
|
func (*RootCmd) syncStart(socketPath *string) *serpent.Command {
|
|
var timeout time.Duration
|
|
|
|
cmd := &serpent.Command{
|
|
Use: "start <unit>",
|
|
Short: "Wait until all unit dependencies are satisfied",
|
|
Long: "Wait until all dependencies are satisfied, consider the unit to have started, then allow it to proceed. This command polls until dependencies are ready, then marks the unit as started.",
|
|
Handler: func(i *serpent.Invocation) error {
|
|
ctx := i.Context()
|
|
|
|
if len(i.Args) != 1 {
|
|
return xerrors.New("exactly one unit name is required")
|
|
}
|
|
unitName := unit.ID(i.Args[0])
|
|
|
|
if timeout > 0 {
|
|
var cancel context.CancelFunc
|
|
ctx, cancel = context.WithTimeout(ctx, timeout)
|
|
defer cancel()
|
|
}
|
|
|
|
opts := []agentsocket.Option{}
|
|
if *socketPath != "" {
|
|
opts = append(opts, agentsocket.WithPath(*socketPath))
|
|
}
|
|
|
|
client, err := agentsocket.NewClient(ctx, opts...)
|
|
if err != nil {
|
|
return xerrors.Errorf("connect to agent socket: %w", err)
|
|
}
|
|
defer client.Close()
|
|
|
|
ready, err := client.SyncReady(ctx, unitName)
|
|
if err != nil {
|
|
return xerrors.Errorf("error checking dependencies: %w", err)
|
|
}
|
|
|
|
if !ready {
|
|
cliui.Infof(i.Stdout, "Waiting for dependencies of unit '%s' to be satisfied...", unitName)
|
|
|
|
ticker := time.NewTicker(syncPollInterval)
|
|
defer ticker.Stop()
|
|
|
|
pollLoop:
|
|
for {
|
|
select {
|
|
case <-ctx.Done():
|
|
if ctx.Err() == context.DeadlineExceeded {
|
|
return xerrors.Errorf("timeout waiting for dependencies of unit '%s'", unitName)
|
|
}
|
|
return ctx.Err()
|
|
case <-ticker.C:
|
|
ready, err := client.SyncReady(ctx, unitName)
|
|
if err != nil {
|
|
return xerrors.Errorf("error checking dependencies: %w", err)
|
|
}
|
|
if ready {
|
|
break pollLoop
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
if err := client.SyncStart(ctx, unitName); err != nil {
|
|
return xerrors.Errorf("start unit failed: %w", err)
|
|
}
|
|
|
|
cliui.Info(i.Stdout, "Success")
|
|
|
|
return nil
|
|
},
|
|
}
|
|
|
|
cmd.Options = append(cmd.Options, serpent.Option{
|
|
Flag: "timeout",
|
|
Description: "Maximum time to wait for dependencies (e.g., 30s, 5m). 5m by default.",
|
|
Value: serpent.DurationOf(&timeout),
|
|
Default: "5m",
|
|
})
|
|
|
|
return cmd
|
|
}
|