From 254fcbc30194703c8753c0375d81e4f257d8221a Mon Sep 17 00:00:00 2001 From: Youssef Omar Bouden Date: Thu, 12 Nov 2026 12:15:52 +0100 Subject: [PATCH] Honor --poll-interval in the shared async operation poller AsyncOperationPoller polled every second regardless of the flag, so namespace create and the other commands still on getPoller ignored --poll-interval. Pass the parsed interval through, reject non-positive values, and declare the flag on namespace edit, apply, delete and lifecycle set, which polled with the same poller without exposing it. Closes #121 --- temporalcloudcli/commands.gen.go | 12 ++++ temporalcloudcli/commands.namespace.go | 6 +- .../commands.namespace.lifecycle.go | 2 +- temporalcloudcli/commands.yml | 28 ++++++++++ temporalcloudcli/commands_test.go | 6 +- temporalcloudcli/common.go | 20 ++++--- temporalcloudcli/common_test.go | 55 +++++++++++++++++++ 7 files changed, 117 insertions(+), 12 deletions(-) diff --git a/temporalcloudcli/commands.gen.go b/temporalcloudcli/commands.gen.go index f34129c..d8cea7e 100644 --- a/temporalcloudcli/commands.gen.go +++ b/temporalcloudcli/commands.gen.go @@ -2044,6 +2044,7 @@ type CloudNamespaceApplyCommand struct { AsyncOperationId string Idempotent bool Async bool + PollInterval cliext.FlagDuration ResourceVersion string } @@ -2065,6 +2066,8 @@ func NewCloudNamespaceApplyCommand(cctx *CommandContext, parent *CloudNamespaceC s.Command.Flags().StringVar(&s.AsyncOperationId, "async-operation-id", "", "Custom identifier for tracking this async operation. If not provided, a unique ID is generated automatically.") s.Command.Flags().BoolVar(&s.Idempotent, "idempotent", false, "Succeed silently if the namespace already matches the specification. Without this flag, the command errors when no changes are needed.") s.Command.Flags().BoolVar(&s.Async, "async", false, "Return immediately after initiating the operation instead of waiting for completion. Use the returned operation ID to check status later.") + s.PollInterval = cliext.MustParseFlagDuration("1s") + s.Command.Flags().Var(&s.PollInterval, "poll-interval", "Time to wait between status checks when waiting for operation completion. Cannot be greater than 10 minutes. Supports minutes (m) and seconds (s).") s.Command.Flags().StringVarP(&s.ResourceVersion, "resource-version", "v", "", "Resource version for optimistic concurrency control. If not provided, the current version is fetched automatically.") s.ClientOptions.BuildFlags(s.Command.Flags()) s.DiffOptions.BuildFlags(s.Command.Flags()) @@ -2680,6 +2683,7 @@ type CloudNamespaceDeleteCommand struct { Namespace string AsyncOperationId string Async bool + PollInterval cliext.FlagDuration Idempotent bool ResourceVersion string } @@ -2700,6 +2704,8 @@ func NewCloudNamespaceDeleteCommand(cctx *CommandContext, parent *CloudNamespace _ = cobra.MarkFlagRequired(s.Command.Flags(), "namespace") s.Command.Flags().StringVar(&s.AsyncOperationId, "async-operation-id", "", "Custom identifier for tracking this async operation. If not provided, a unique ID is generated automatically.") s.Command.Flags().BoolVar(&s.Async, "async", false, "Return immediately after initiating the operation instead of waiting for completion. Use the returned operation ID to check status later.") + s.PollInterval = cliext.MustParseFlagDuration("1s") + s.Command.Flags().Var(&s.PollInterval, "poll-interval", "Time to wait between status checks when waiting for operation completion. Cannot be greater than 10 minutes. Supports minutes (m) and seconds (s).") s.Command.Flags().BoolVar(&s.Idempotent, "idempotent", false, "Succeed silently if the namespace does not exist. Without this flag, the command errors if the namespace is not found.") s.Command.Flags().StringVarP(&s.ResourceVersion, "resource-version", "v", "", "Resource version for optimistic concurrency control. If not provided, the current version is fetched automatically.") s.ClientOptions.BuildFlags(s.Command.Flags()) @@ -2802,6 +2808,7 @@ type CloudNamespaceEditCommand struct { AsyncOperationId string Idempotent bool Async bool + PollInterval cliext.FlagDuration ResourceVersion string } @@ -2822,6 +2829,8 @@ func NewCloudNamespaceEditCommand(cctx *CommandContext, parent *CloudNamespaceCo s.Command.Flags().StringVar(&s.AsyncOperationId, "async-operation-id", "", "Custom identifier for tracking this async operation. If not provided, a unique ID is generated automatically.") s.Command.Flags().BoolVar(&s.Idempotent, "idempotent", false, "Succeed silently if no changes were made in the editor. Without this flag, the command errors when the configuration is unchanged.") s.Command.Flags().BoolVar(&s.Async, "async", false, "Return immediately after initiating the operation instead of waiting for completion. Use the returned operation ID to check status later.") + s.PollInterval = cliext.MustParseFlagDuration("1s") + s.Command.Flags().Var(&s.PollInterval, "poll-interval", "Time to wait between status checks when waiting for operation completion. Cannot be greater than 10 minutes. Supports minutes (m) and seconds (s).") s.Command.Flags().StringVarP(&s.ResourceVersion, "resource-version", "v", "", "Resource version for optimistic concurrency control. If not provided, the current version is fetched automatically.") s.ClientOptions.BuildFlags(s.Command.Flags()) s.DiffOptions.BuildFlags(s.Command.Flags()) @@ -3949,6 +3958,7 @@ type CloudNamespaceLifecycleSetCommand struct { EnableDeleteProtection bool AsyncOperationId string Async bool + PollInterval cliext.FlagDuration Idempotent bool ResourceVersion string } @@ -3971,6 +3981,8 @@ func NewCloudNamespaceLifecycleSetCommand(cctx *CommandContext, parent *CloudNam _ = cobra.MarkFlagRequired(s.Command.Flags(), "enable-delete-protection") s.Command.Flags().StringVar(&s.AsyncOperationId, "async-operation-id", "", "Custom identifier for tracking this async operation. If not provided, a unique ID is generated automatically.") s.Command.Flags().BoolVar(&s.Async, "async", false, "Return immediately after initiating the operation instead of waiting for completion. Use the returned operation ID to check status later.") + s.PollInterval = cliext.MustParseFlagDuration("1s") + s.Command.Flags().Var(&s.PollInterval, "poll-interval", "Time to wait between status checks when waiting for operation completion. Cannot be greater than 10 minutes. Supports minutes (m) and seconds (s).") s.Command.Flags().BoolVar(&s.Idempotent, "idempotent", false, "Succeed silently if the lifecycle configuration is already set to the specified value. Without this flag, the command errors when no change is needed.") s.Command.Flags().StringVar(&s.ResourceVersion, "resource-version", "", "Resource version for optimistic concurrency control. If not provided, the current version is fetched automatically.") s.ClientOptions.BuildFlags(s.Command.Flags()) diff --git a/temporalcloudcli/commands.namespace.go b/temporalcloudcli/commands.namespace.go index e573832..8277915 100644 --- a/temporalcloudcli/commands.namespace.go +++ b/temporalcloudcli/commands.namespace.go @@ -97,7 +97,7 @@ func (c *CloudNamespaceEditCommand) run(cctx *CommandContext, _ []string) error } // Poll for completion - poller, err := getPoller(cctx, c.ClientOptions) + poller, err := getPoller(cctx, c.ClientOptions, c.PollInterval.Duration()) if err != nil { return err } @@ -211,7 +211,7 @@ func (c *CloudNamespaceApplyCommand) run(cctx *CommandContext, _ []string) error } // Step 7: Poll for completion - poller, err := getPoller(cctx, c.ClientOptions) + poller, err := getPoller(cctx, c.ClientOptions, c.PollInterval.Duration()) if err != nil { return err } @@ -268,7 +268,7 @@ func (c *CloudNamespaceDeleteCommand) run(cctx *CommandContext, _ []string) erro } // Poll for completion - poller, err := getPoller(cctx, c.ClientOptions) + poller, err := getPoller(cctx, c.ClientOptions, c.PollInterval.Duration()) if err != nil { return err } diff --git a/temporalcloudcli/commands.namespace.lifecycle.go b/temporalcloudcli/commands.namespace.lifecycle.go index 9861f58..853389b 100644 --- a/temporalcloudcli/commands.namespace.lifecycle.go +++ b/temporalcloudcli/commands.namespace.lifecycle.go @@ -105,7 +105,7 @@ func (c *CloudNamespaceLifecycleSetCommand) run(cctx *CommandContext, _ []string } // Poll for completion - poller, err := getPoller(cctx, c.ClientOptions) + poller, err := getPoller(cctx, c.ClientOptions, c.PollInterval.Duration()) if err != nil { return err } diff --git a/temporalcloudcli/commands.yml b/temporalcloudcli/commands.yml index 986fabb..be8e010 100644 --- a/temporalcloudcli/commands.yml +++ b/temporalcloudcli/commands.yml @@ -241,6 +241,13 @@ commands: description: | Return immediately after initiating the operation instead of waiting for completion. Use the returned operation ID to check status later. + - name: poll-interval + type: duration + default: 1s + description: | + Time to wait between status checks when waiting for operation completion. + Cannot be greater than 10 minutes. + Supports minutes (m) and seconds (s). - name: resource-version type: string short: v @@ -289,6 +296,13 @@ commands: description: | Return immediately after initiating the operation instead of waiting for completion. Use the returned operation ID to check status later. + - name: poll-interval + type: duration + default: 1s + description: | + Time to wait between status checks when waiting for operation completion. + Cannot be greater than 10 minutes. + Supports minutes (m) and seconds (s). - name: resource-version type: string short: v @@ -328,6 +342,13 @@ commands: description: | Return immediately after initiating the operation instead of waiting for completion. Use the returned operation ID to check status later. + - name: poll-interval + type: duration + default: 1s + description: | + Time to wait between status checks when waiting for operation completion. + Cannot be greater than 10 minutes. + Supports minutes (m) and seconds (s). - name: idempotent type: bool description: | @@ -709,6 +730,13 @@ commands: description: | Return immediately after initiating the operation instead of waiting for completion. Use the returned operation ID to check status later. + - name: poll-interval + type: duration + default: 1s + description: | + Time to wait between status checks when waiting for operation completion. + Cannot be greater than 10 minutes. + Supports minutes (m) and seconds (s). - name: idempotent type: bool description: | diff --git a/temporalcloudcli/commands_test.go b/temporalcloudcli/commands_test.go index 71e9abc..51842b8 100644 --- a/temporalcloudcli/commands_test.go +++ b/temporalcloudcli/commands_test.go @@ -10,6 +10,7 @@ import ( "math/rand" "os" "testing" + "time" "github.com/stretchr/testify/assert" "github.com/stretchr/testify/require" @@ -112,7 +113,10 @@ func (s *SharedServerSuite) pollAsyncOperation( }, } - poller := temporalcloudcli.AsyncOperationPoller{CloudClient: cloudClient.CloudService()} + poller := temporalcloudcli.AsyncOperationPoller{ + CloudClient: cloudClient.CloudService(), + PollInterval: time.Second, + } return poller.PollAsyncOperation(cctx, operationID, "") } diff --git a/temporalcloudcli/common.go b/temporalcloudcli/common.go index d98109d..a3bb4cc 100644 --- a/temporalcloudcli/common.go +++ b/temporalcloudcli/common.go @@ -2,6 +2,7 @@ package temporalcloudcli import ( "context" + "errors" "fmt" "os" "strings" @@ -208,7 +209,7 @@ func (r *operationHandler) HandleOperation(op *operation.AsyncOperation, resourc if r.asyncOpts.Async { return r.cctx.Printer.PrintStructured(MutationResult{AsyncOp: op, ID: resourceID}, printer.StructuredOptions{}) } - poller, pollerErr := getPoller(r.cctx, r.clientOpts) + poller, pollerErr := getPoller(r.cctx, r.clientOpts, r.asyncOpts.PollInterval.Duration()) if pollerErr != nil { return pollerErr } @@ -306,11 +307,12 @@ func wrapDeleteOperation[Req any, Res AsyncOperationResponse]( } type AsyncOperationPoller struct { - CloudClient cloudservice.CloudServiceClient + CloudClient cloudservice.CloudServiceClient + PollInterval time.Duration } // pollAsyncOperation polls an async operation until it reaches a terminal state. -// It prints status updates every second and returns the final AsyncOperation. +// It prints status updates every PollInterval and returns the final AsyncOperation. // // The cloudClient should be pre-built using cctx.BuildCloudClient(). // @@ -321,7 +323,10 @@ func (p *AsyncOperationPoller) PollAsyncOperation( operationID string, id string, ) error { - ticker := time.NewTicker(1 * time.Second) + if p.PollInterval <= 0 { + return errors.New("--poll-interval must be greater than 0") + } + ticker := time.NewTicker(p.PollInterval) defer ticker.Stop() for { @@ -410,7 +415,7 @@ func newUnchangedResult() Result { } } -func getPoller(cctx *CommandContext, opts ClientOptions) (Poller, error) { +func getPoller(cctx *CommandContext, opts ClientOptions, pollInterval time.Duration) (Poller, error) { if cctx.Poller != nil { return cctx.Poller, nil } @@ -419,7 +424,8 @@ func getPoller(cctx *CommandContext, opts ClientOptions) (Poller, error) { return nil, err } return &AsyncOperationPoller{ - CloudClient: cloudClient.CloudService(), + CloudClient: cloudClient.CloudService(), + PollInterval: pollInterval, }, nil } @@ -453,7 +459,7 @@ func wrapAsyncOperation[P any]( }, printer.StructuredOptions{}) } - poller, err := getPoller(cctx, clientOpts) + poller, err := getPoller(cctx, clientOpts, asyncOpts.PollInterval.Duration()) if err != nil { return err } diff --git a/temporalcloudcli/common_test.go b/temporalcloudcli/common_test.go index c6e70f7..27c54e5 100644 --- a/temporalcloudcli/common_test.go +++ b/temporalcloudcli/common_test.go @@ -13,11 +13,14 @@ import ( "github.com/google/go-cmp/cmp" "github.com/stretchr/testify/assert" + "github.com/stretchr/testify/mock" "github.com/stretchr/testify/require" + cloudmock "github.com/temporalio/cloud-cli/internal/cloudservice/mock" "github.com/temporalio/cloud-cli/temporalcloudcli" "github.com/temporalio/cloud-cli/temporalcloudcli/internal/printer" cmdmock "github.com/temporalio/cloud-cli/temporalcloudcli/mock" "go.temporal.io/api/temporalproto" + cloudservice "go.temporal.io/cloud-sdk/api/cloudservice/v1" operation "go.temporal.io/cloud-sdk/api/operation/v1" "google.golang.org/grpc/codes" "google.golang.org/grpc/status" @@ -219,6 +222,58 @@ func TestAsyncOperationHandler_PollingError(t *testing.T) { require.ErrorIs(t, err, pollingErr) } +func TestAsyncOperationPoller_PollInterval(t *testing.T) { + const interval = 20 * time.Millisecond + states := []operation.AsyncOperation_State{ + operation.AsyncOperation_STATE_PENDING, + operation.AsyncOperation_STATE_IN_PROGRESS, + operation.AsyncOperation_STATE_FULFILLED, + } + mockCloud := cloudmock.NewMockCloudServiceClient(t) + for _, state := range states { + mockCloud.EXPECT(). + GetAsyncOperation(mock.Anything, &cloudservice.GetAsyncOperationRequest{AsyncOperationId: "op-1"}, mock.Anything). + Return(&cloudservice.GetAsyncOperationResponse{ + AsyncOperation: &operation.AsyncOperation{Id: "op-1", State: state}, + }, nil). + Once() + } + var buf bytes.Buffer + cctx := newTestCommandContext(t, &buf) + poller := temporalcloudcli.AsyncOperationPoller{CloudClient: mockCloud, PollInterval: interval} + + start := time.Now() + require.NoError(t, poller.PollAsyncOperation(cctx, "op-1", "my-namespace")) + elapsed := time.Since(start) + + assert.GreaterOrEqual(t, elapsed, time.Duration(len(states))*interval) + assert.Less(t, elapsed, 2*time.Second, "status checks did not follow the configured interval") +} + +func TestAsyncOperationPoller_InvalidPollInterval(t *testing.T) { + tests := []struct { + name string + interval time.Duration + }{ + {name: "Zero", interval: 0}, + {name: "Negative", interval: -time.Second}, + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + var buf bytes.Buffer + cctx := newTestCommandContext(t, &buf) + poller := temporalcloudcli.AsyncOperationPoller{ + CloudClient: cloudmock.NewMockCloudServiceClient(t), + PollInterval: tt.interval, + } + + err := poller.PollAsyncOperation(cctx, "op-1", "my-namespace") + require.EqualError(t, err, "--poll-interval must be greater than 0") + assert.Empty(t, buf.String()) + }) + } +} + func TestAsyncOperationHandler_HandleUpdateErr_NothingToChange_Idempotent(t *testing.T) { var buf bytes.Buffer cctx := newTestCommandContext(t, &buf)