Skip to content

Commit

Permalink
address comments. add showid, stack
Browse files Browse the repository at this point in the history
  • Loading branch information
vancexu committed Mar 1, 2018
1 parent 81799c2 commit 14b372a
Show file tree
Hide file tree
Showing 4 changed files with 91 additions and 28 deletions.
16 changes: 11 additions & 5 deletions tools/cli/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -60,23 +60,27 @@ and it takes a string as input, so there is the `-i '"cadence"'`. Single quote `

- Start workflow:
```
./cadence workflow start --tl helloWorldGroup --wt main.Workflow --et 60 --dt 10 -i '"cadence"'
./cadence workflow start --tl helloWorldGroup --wt main.Workflow --et 60 -i '"cadence"'
# view help messages for workflow start
./cadence workflow start -h
# for workflow with multiple input, seperate each json with space/newline like
./cadence workflow start --tl helloWorldGroup --wt main.WorkflowWith3Args --et 60 --dt 10 -i '"your_input_string" 123 {"Name":"my-string", "Age":12345}'
./cadence workflow start --tl helloWorldGroup --wt main.WorkflowWith3Args --et 60 -i '"your_input_string" 123 {"Name":"my-string", "Age":12345}'
```
Workflow `start` command is similar to `run` command and takes same flag options. But it just start the workflow and immediately return workflow_id and run_id.
User need to run `show` to view workflow history/progress.

- Show workflow history
```
./cadence workflow show -w 3ea6b242-b23c-4279-bb13-f215661b4717 -r 866ae14c-88cf-4f1e-980f-571e031d71b0
# a shortcut of this is (without -w -r flag)
./cadence workflow showid 3ea6b242-b23c-4279-bb13-f215661b4717 866ae14c-88cf-4f1e-980f-571e031d71b0
# if run_id is not provided, it will show the latest run history for that workflow_id
./cadence workflow show -w 3ea6b242-b23c-4279-bb13-f215661b4717
# a shortcut of this is
./cadence workflow showid 3ea6b242-b23c-4279-bb13-f215661b4717
```

- List open or closed workflow executions
Expand All @@ -86,11 +90,13 @@ User need to run `show` to view workflow history/progress.

- Query workflow execution
```
# use build-in query type "__stack_trace" which is supported by cadence client library
./cadence workflow query -w <wid> -r <rid>
# use custom query type
./cadence workflow query -w <wid> -r <rid> --qt <query-type>
# use build-in query type "__stack_trace" which is supported by cadence client library
./cadence workflow query -w <wid> -r <rid> --qt __stack_trace
# a shortcut to query using __stack_trace is (without --qt flag)
./cadence workflow stack -w <wid> -r <rid>
```

- Signal, cancel, terminate workflow
Expand Down
6 changes: 6 additions & 0 deletions tools/cli/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,5 +61,11 @@ func NewCliApp() *cli.App {
Subcommands: newWorkflowCommands(),
},
}

// set builder if not customized
if cBuilder == nil {
SetBuilder(NewBuilder())
}

return app
}
60 changes: 38 additions & 22 deletions tools/cli/commands.go
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,8 @@ var (
colorGreen = color.New(color.FgGreen).SprintFunc()
)

// cBuilder is used to create cadence clients if user want to provide customized builder
// cBuilder is used to create cadence clients
// To provide customized builder, call SetBuilder() before call NewCliApp()
var cBuilder WorkflowClientBuilderInterface

// SetBuilder can be used to inject customized builder of cadence clients
Expand Down Expand Up @@ -272,10 +273,26 @@ func DescribeDomain(c *cli.Context) {

// ShowHistory shows the history of given workflow execution based on workflowID and runID.
func ShowHistory(c *cli.Context) {
wfClient := getWorkflowClient(c)

wid := getRequiredOption(c, FlagWorkflowID)
rid := c.String(FlagRunID)
showHistoryHelper(c, wid, rid)
}

// ShowHistoryWithWID shows the history of given workflow with workflow_id
func ShowHistoryWithWID(c *cli.Context) {
if !c.Args().Present() {
ExitIfError(errors.New("workflow_id is required"))
}
wid := c.Args().First()
rid := ""
if c.NArg() >= 2 {
rid = c.Args().Get(1)
}
showHistoryHelper(c, wid, rid)
}

func showHistoryHelper(c *cli.Context, wid, rid string) {
wfClient := getWorkflowClient(c)

printRawTime := c.Bool(FlagPrintRawTime)
outputFileName := c.String(FlagOutputFilename)
Expand Down Expand Up @@ -319,10 +336,7 @@ func StartWorkflow(c *cli.Context) {
if et == 0 {
ExitIfError(errors.New(FlagExecutionTimeout + " is required"))
}
dt := defaultDecisionTimeoutInSeconds
if c.IsSet(FlagDecisionTimeout) {
dt = c.Int(FlagDecisionTimeout)
}
dt := c.Int(FlagDecisionTimeout)
wid := c.String(FlagWorkflowID)
if len(wid) == 0 {
wid = uuid.New()
Expand Down Expand Up @@ -368,10 +382,7 @@ func RunWorkflow(c *cli.Context) {
if et == 0 {
ExitIfError(errors.New(FlagExecutionTimeout + " is required"))
}
dt := defaultDecisionTimeoutInSeconds
if c.IsSet(FlagDecisionTimeout) {
dt = c.Int(FlagDecisionTimeout)
}
dt := c.Int(FlagDecisionTimeout)
wid := c.String(FlagWorkflowID)
if len(wid) == 0 {
wid = uuid.New()
Expand Down Expand Up @@ -536,13 +547,25 @@ func SignalWorkflow(c *cli.Context) {

// QueryWorkflow query workflow execution
func QueryWorkflow(c *cli.Context) {
getRequiredGlobalOption(c, FlagDomain) // for pre-check and alert if not provided
getRequiredOption(c, FlagWorkflowID)
queryType := getRequiredOption(c, FlagQueryType)

queryWorkflowHelper(c, queryType)
}

// QueryWorkflowUsingStackTrace query workflow execution using __stack_trace as query type
func QueryWorkflowUsingStackTrace(c *cli.Context) {
queryWorkflowHelper(c, "__stack_trace")
}

func queryWorkflowHelper(c *cli.Context, queryType string) {
// using service client instead of cadence.Client because we need to directly pass the json blob as input.
serviceClient := getWorkflowServiceClient(c)

domain := getRequiredGlobalOption(c, FlagDomain)
wid := getRequiredOption(c, FlagWorkflowID)
rid := c.String(FlagRunID)
queryType := c.String(FlagQueryType)
input := processJSONInput(c)

tcCtx, cancel := newContext()
Expand Down Expand Up @@ -671,7 +694,7 @@ func listClosedWorkflow(client client.Client, pageSize int, earliestTime, latest
}

func getDomainClient(c *cli.Context) client.DomainClient {
service, err := getBuilder().BuildServiceClient(c)
service, err := cBuilder.BuildServiceClient(c)
if err != nil {
ExitIfError(err)
}
Expand All @@ -686,7 +709,7 @@ func getDomainClient(c *cli.Context) client.DomainClient {
func getWorkflowClient(c *cli.Context) client.Client {
domain := getRequiredGlobalOption(c, FlagDomain)

service, err := getBuilder().BuildServiceClient(c)
service, err := cBuilder.BuildServiceClient(c)
if err != nil {
ExitIfError(err)
}
Expand All @@ -700,7 +723,7 @@ func getWorkflowClient(c *cli.Context) client.Client {
}

func getWorkflowServiceClient(c *cli.Context) workflowserviceclient.Interface {
client, err := getBuilder().BuildServiceClient(c)
client, err := cBuilder.BuildServiceClient(c)
if err != nil {
ExitIfError(err)
}
Expand All @@ -724,13 +747,6 @@ func getRequiredGlobalOption(c *cli.Context, optionName string) string {
return value
}

func getBuilder() WorkflowClientBuilderInterface {
if cBuilder == nil {
cBuilder = NewBuilder()
}
return cBuilder
}

func convertTime(unixNano int64) string {
t2 := time.Unix(0, unixNano)
return t2.Format(defaultTimeFormat)
Expand Down
37 changes: 36 additions & 1 deletion tools/cli/workflow.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,14 @@ func newWorkflowCommands() []cli.Command {
ShowHistory(c)
},
},
{
Name: "showid",
Usage: "show workflow history with given workflow_id and optional run_id (a shortcut of `show -w <wid> -r <rid>`)",
Description: "cadence workflow showid <workflow_id> <run_id>. workflow_id is required; run_id is optional",
Action: func(c *cli.Context) {
ShowHistoryWithWID(c)
},
},
{
Name: "start",
Usage: "start a new workflow execution",
Expand All @@ -71,6 +79,7 @@ func newWorkflowCommands() []cli.Command {
},
cli.IntFlag{
Name: FlagDecisionTimeoutWithAlias,
Value: defaultDecisionTimeoutInSeconds,
Usage: "Decision task start to close timeout in seconds",
},
cli.StringFlag{
Expand Down Expand Up @@ -109,6 +118,7 @@ func newWorkflowCommands() []cli.Command {
},
cli.IntFlag{
Name: FlagDecisionTimeoutWithAlias,
Value: defaultDecisionTimeoutInSeconds,
Usage: "Decision task start to close timeout in seconds",
},
cli.IntFlag{
Expand Down Expand Up @@ -251,7 +261,6 @@ func newWorkflowCommands() []cli.Command {
},
cli.StringFlag{
Name: FlagQueryTypeWithAlias,
Value: "__stack_trace",
Usage: "The query type you want to run",
},
cli.StringFlag{
Expand All @@ -268,5 +277,31 @@ func newWorkflowCommands() []cli.Command {
QueryWorkflow(c)
},
},
{
Name: "stack",
Usage: "query workflow execution with __stack_trace as query type",
Flags: []cli.Flag{
cli.StringFlag{
Name: FlagWorkflowIDWithAlias,
Usage: "WorkflowID",
},
cli.StringFlag{
Name: FlagRunIDWithAlias,
Usage: "RunID",
},
cli.StringFlag{
Name: FlagInputWithAlias,
Usage: "Optional input for the query, in JSON format. If there are multiple parameters, concatenate them and separate by space.",
},
cli.StringFlag{
Name: FlagInputFileWithAlias,
Usage: "Optional input for the query from JSON file. If there are multiple JSON, concatenate them and separate by space or newline. " +
"Input from file will be overwrite by input from command line",
},
},
Action: func(c *cli.Context) {
QueryWorkflowUsingStackTrace(c)
},
},
}
}

0 comments on commit 14b372a

Please sign in to comment.