-
Notifications
You must be signed in to change notification settings - Fork 36
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
Implement activity commands #445
Merged
dandavison
merged 3 commits into
temporalio:cli-rewrite
from
dandavison:activity-commands
Feb 12, 2024
Merged
Changes from 1 commit
Commits
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 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,73 @@ | ||||||||||||||||||||
package temporalcli | ||||||||||||||||||||
|
||||||||||||||||||||
import ( | ||||||||||||||||||||
"fmt" | ||||||||||||||||||||
|
||||||||||||||||||||
"go.temporal.io/api/common/v1" | ||||||||||||||||||||
"go.temporal.io/api/failure/v1" | ||||||||||||||||||||
"go.temporal.io/api/workflowservice/v1" | ||||||||||||||||||||
) | ||||||||||||||||||||
|
||||||||||||||||||||
func (c *TemporalActivityCompleteCommand) run(cctx *CommandContext, args []string) error { | ||||||||||||||||||||
cl, err := c.Parent.ClientOptions.dialClient(cctx) | ||||||||||||||||||||
if err != nil { | ||||||||||||||||||||
return err | ||||||||||||||||||||
} | ||||||||||||||||||||
defer cl.Close() | ||||||||||||||||||||
|
||||||||||||||||||||
metadata := map[string][]byte{"encoding": []byte("json/plain")} | ||||||||||||||||||||
resultPayloads, err := CreatePayloads([][]byte{[]byte(c.Result)}, metadata, false) | ||||||||||||||||||||
if err != nil { | ||||||||||||||||||||
return err | ||||||||||||||||||||
} | ||||||||||||||||||||
|
||||||||||||||||||||
_, err = cl.WorkflowService().RespondActivityTaskCompletedById(cctx, &workflowservice.RespondActivityTaskCompletedByIdRequest{ | ||||||||||||||||||||
Namespace: c.Parent.Namespace, | ||||||||||||||||||||
WorkflowId: c.WorkflowId, | ||||||||||||||||||||
RunId: c.RunId, | ||||||||||||||||||||
ActivityId: c.ActivityId, | ||||||||||||||||||||
Result: resultPayloads, | ||||||||||||||||||||
Identity: c.Identity, | ||||||||||||||||||||
}) | ||||||||||||||||||||
if err != nil { | ||||||||||||||||||||
return fmt.Errorf("unable to complete Activity: %w", err) | ||||||||||||||||||||
} | ||||||||||||||||||||
return nil | ||||||||||||||||||||
} | ||||||||||||||||||||
|
||||||||||||||||||||
func (c *TemporalActivityFailCommand) run(cctx *CommandContext, args []string) error { | ||||||||||||||||||||
cl, err := c.Parent.ClientOptions.dialClient(cctx) | ||||||||||||||||||||
if err != nil { | ||||||||||||||||||||
return err | ||||||||||||||||||||
} | ||||||||||||||||||||
defer cl.Close() | ||||||||||||||||||||
|
||||||||||||||||||||
metadata := map[string][]byte{"encoding": []byte("json/plain")} | ||||||||||||||||||||
var detailPayloads *common.Payloads | ||||||||||||||||||||
detailPayloads = nil | ||||||||||||||||||||
if len(c.Detail) > 0 { | ||||||||||||||||||||
detailPayloads, err = CreatePayloads([][]byte{[]byte(c.Detail)}, metadata, false) | ||||||||||||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
Little cleanup There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Thanks! |
||||||||||||||||||||
if err != nil { | ||||||||||||||||||||
return err | ||||||||||||||||||||
} | ||||||||||||||||||||
} | ||||||||||||||||||||
_, err = cl.WorkflowService().RespondActivityTaskFailedById(cctx, &workflowservice.RespondActivityTaskFailedByIdRequest{ | ||||||||||||||||||||
Namespace: c.Parent.Namespace, | ||||||||||||||||||||
WorkflowId: c.WorkflowId, | ||||||||||||||||||||
RunId: c.RunId, | ||||||||||||||||||||
ActivityId: c.ActivityId, | ||||||||||||||||||||
Failure: &failure.Failure{ | ||||||||||||||||||||
Message: c.Reason, | ||||||||||||||||||||
Source: "CLI", | ||||||||||||||||||||
FailureInfo: &failure.Failure_ApplicationFailureInfo{ApplicationFailureInfo: &failure.ApplicationFailureInfo{ | ||||||||||||||||||||
NonRetryable: true, | ||||||||||||||||||||
Details: detailPayloads, | ||||||||||||||||||||
}}, | ||||||||||||||||||||
}, | ||||||||||||||||||||
Identity: c.Identity, | ||||||||||||||||||||
}) | ||||||||||||||||||||
if err != nil { | ||||||||||||||||||||
return fmt.Errorf("unable to fail Activity: %w", err) | ||||||||||||||||||||
} | ||||||||||||||||||||
return nil | ||||||||||||||||||||
} |
This file contains 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,150 @@ | ||
package temporalcli_test | ||
|
||
import ( | ||
"context" | ||
"time" | ||
|
||
"go.temporal.io/api/enums/v1" | ||
"go.temporal.io/api/history/v1" | ||
"go.temporal.io/sdk/client" | ||
) | ||
|
||
func (s *SharedServerSuite) TestActivity_Complete() { | ||
run := s.waitActivityStarted() | ||
wid := run.GetID() | ||
aid := "dev-activity-id" | ||
identity := "MyIdentity" | ||
res := s.Execute( | ||
"activity", "complete", | ||
"--activity-id", aid, | ||
"--workflow-id", wid, | ||
"--result", "\"complete-activity-result\"", | ||
"--identity", identity, | ||
"--address", s.Address(), | ||
) | ||
s.NoError(res.Err) | ||
var actual string | ||
s.NoError(run.Get(s.Context, &actual)) | ||
s.Equal("complete-activity-result", actual) | ||
|
||
started, completed, failed := s.getActivityEvents(wid, aid) | ||
s.NotNil(started) | ||
s.Nil(failed) | ||
s.NotNil(completed) | ||
s.Equal("\"complete-activity-result\"", string(completed.Result.Payloads[0].GetData())) | ||
s.Equal(identity, completed.GetIdentity()) | ||
} | ||
|
||
func (s *SharedServerSuite) TestActivity_Fail() { | ||
run := s.waitActivityStarted() | ||
wid := run.GetID() | ||
aid := "dev-activity-id" | ||
detail := "{\"myKey\": \"myValue\"}" | ||
reason := "MyReason" | ||
identity := "MyIdentity" | ||
res := s.Execute( | ||
"activity", "fail", | ||
"--activity-id", aid, | ||
"--workflow-id", wid, | ||
"--run-id", run.GetRunID(), | ||
"--detail", detail, | ||
"--reason", reason, | ||
"--identity", identity, | ||
"--address", s.Address(), | ||
) | ||
s.NoError(res.Err) | ||
err := run.Get(s.Context, nil) | ||
s.NotNil(err) | ||
|
||
started, completed, failed := s.getActivityEvents(wid, aid) | ||
s.NotNil(started) | ||
s.Nil(completed) | ||
s.NotNil(failed) | ||
s.Equal( | ||
detail, | ||
string(failed.GetFailure().GetApplicationFailureInfo().GetDetails().Payloads[0].GetData()), | ||
) | ||
s.Equal(reason, failed.GetFailure().Message) | ||
s.Equal(identity, failed.GetIdentity()) | ||
} | ||
|
||
func (s *SharedServerSuite) TestActivity_Complete_InvalidResult() { | ||
run := s.waitActivityStarted() | ||
wid := run.GetID() | ||
aid := "dev-activity-id" | ||
res := s.Execute( | ||
"activity", "complete", | ||
"--activity-id", aid, | ||
"--workflow-id", wid, | ||
"--result", "{not json}", | ||
"--address", s.Address(), | ||
) | ||
s.ErrorContains(res.Err, "is not valid JSON") | ||
|
||
started, completed, failed := s.getActivityEvents(wid, aid) | ||
s.Nil(started) | ||
s.Nil(completed) | ||
s.Nil(failed) | ||
} | ||
|
||
func (s *SharedServerSuite) TestActivity_Fail_InvalidDetail() { | ||
run := s.waitActivityStarted() | ||
wid := run.GetID() | ||
aid := "dev-activity-id" | ||
res := s.Execute( | ||
"activity", "fail", | ||
"--activity-id", aid, | ||
"--workflow-id", wid, | ||
"--detail", "{not json}", | ||
"--address", s.Address(), | ||
) | ||
s.ErrorContains(res.Err, "is not valid JSON") | ||
|
||
started, completed, failed := s.getActivityEvents(wid, aid) | ||
s.Nil(started) | ||
s.Nil(completed) | ||
s.Nil(failed) | ||
} | ||
|
||
// Test helpers | ||
|
||
func (s *SharedServerSuite) waitActivityStarted() client.WorkflowRun { | ||
s.Worker.OnDevActivity(func(ctx context.Context, a any) (any, error) { | ||
time.Sleep(0xFFFF * time.Hour) | ||
return nil, nil | ||
}) | ||
run, err := s.Client.ExecuteWorkflow( | ||
s.Context, | ||
client.StartWorkflowOptions{TaskQueue: s.Worker.Options.TaskQueue}, | ||
DevWorkflow, | ||
"ignored", | ||
) | ||
s.NoError(err) | ||
s.Eventually(func() bool { | ||
resp, err := s.Client.DescribeWorkflowExecution(s.Context, run.GetID(), run.GetRunID()) | ||
s.NoError(err) | ||
return len(resp.PendingActivities) > 0 | ||
}, 5*time.Second, 100*time.Millisecond) | ||
return run | ||
} | ||
|
||
func (s *SharedServerSuite) getActivityEvents(workflowID, activityID string) ( | ||
started *history.ActivityTaskStartedEventAttributes, | ||
completed *history.ActivityTaskCompletedEventAttributes, | ||
failed *history.ActivityTaskFailedEventAttributes, | ||
) { | ||
iter := s.Client.GetWorkflowHistory(s.Context, workflowID, "", false, enums.HISTORY_EVENT_FILTER_TYPE_ALL_EVENT) | ||
for iter.HasNext() { | ||
event, err := iter.Next() | ||
s.NoError(err) | ||
if attrs := event.GetActivityTaskStartedEventAttributes(); attrs != nil { | ||
started = attrs | ||
} else if attrs := event.GetActivityTaskCompletedEventAttributes(); attrs != nil { | ||
completed = attrs | ||
s.Equal("json/plain", string(completed.Result.Payloads[0].Metadata["encoding"])) | ||
} else if attrs := event.GetActivityTaskFailedEventAttributes(); attrs != nil { | ||
failed = attrs | ||
} | ||
} | ||
return started, completed, failed | ||
} |
This file contains 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 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 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
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
While this is the approach of the old CLI,
cl.CompleteActivityByID
would have worked here too (you'd wrap the payload inrawValue
). Arguably it'd be cleaner/clearer to use the high-level SDK client, but don't need to change now, but we might consider using the high-level API in the future.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
OK I'm happy to make that change and it would be easy, however, it looks like it doesn't support
identity
so I'm going to leave it for now.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Usually smart clients set identity at the client level not the request level. That's what makes the
--identity
here so strange.