Skip to content

Commit

Permalink
docs(sdk-go): Update examples in Go
Browse files Browse the repository at this point in the history
  • Loading branch information
eduwercamacaro authored Aug 28, 2024
1 parent 84c44fb commit 1481f20
Show file tree
Hide file tree
Showing 47 changed files with 172 additions and 140 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -218,10 +218,10 @@ wf.handleError(
```go
threadsResult := wf.WaitForThreads(...)

errorName := "TIMEOUT"
exnToHandle := littlehorse.Timeout
wf.HandleError(
&threadsResult,
&errorName, // handle only TIMEOUT error. Leave nil to catch all ERROR.
&exnToHandle, // handle only TIMEOUT error. Leave nil to catch all ERROR.
func(handler *littlehorse.WorkflowThread) {
handler.Execute("some-task-in-my-exn-handler")
},
Expand Down
38 changes: 0 additions & 38 deletions lhctl/go.work.sum

This file was deleted.

8 changes: 4 additions & 4 deletions sdk-go/examples/basic/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ The file [workflow.go](./workflow.go) has two functions defined:
Before we can create the `WfSpec`, we need to register the `TaskDef`. The easiest way to do that is to run the task worker:

```
go run ./basic/worker
go run ./examples/basic/worker
```

Leave that process running.
Expand All @@ -24,21 +24,21 @@ Leave that process running.
Next, in another terminal, run:

```
go run ./basic/deploy
go run ./examples/basic/deploy
```

That will create the `WfSpec`. You can verify that via:

```
lhctl get wfSpec my-workflow
lhctl get wfSpec basic-workflow
```

## Run a `WfRun`

To run a `WfRun`, you can use `lhctl`.

```
lhctl run my-workflow name obi-wan
lhctl run basic-workflow name obi-wan
```

It will print out an ID. You can view the status of that `WfRun` via:
Expand Down
6 changes: 3 additions & 3 deletions sdk-go/examples/basic/deploy/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ package main

import (
"context"
lh "github.com/littlehorse-enterprises/littlehorse/sdk-go/littlehorse"
"github.com/littlehorse-enterprises/littlehorse/sdk-go/littlehorse"
"log"

"github.com/littlehorse-enterprises/littlehorse/sdk-go/examples"
Expand All @@ -11,7 +11,7 @@ import (

func main() {
_, client := examples.LoadConfigAndClient()
wf := lh.NewWorkflow(basic.MyWorkflow, "my-workflow")
wf := littlehorse.NewWorkflow(basic.MyWorkflow, basic.WorkflowName)
putWf, err := wf.Compile()
if err != nil {
log.Fatal(err)
Expand All @@ -21,5 +21,5 @@ func main() {
if err != nil {
log.Fatal(err)
}
lh.PrintProto(resp)
littlehorse.PrintProto(resp)
}
3 changes: 2 additions & 1 deletion sdk-go/examples/basic/runworkflow/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package main

import (
"context"
"github.com/littlehorse-enterprises/littlehorse/sdk-go/examples/basic"
"github.com/littlehorse-enterprises/littlehorse/sdk-go/lhproto"
"github.com/littlehorse-enterprises/littlehorse/sdk-go/littlehorse"
"log"
Expand All @@ -23,7 +24,7 @@ func main() {
wfId, err := (*client).RunWf(
context.Background(),
&lhproto.RunWfRequest{
WfSpecName: "my-workflow",
WfSpecName: basic.WorkflowName,
Variables: map[string]*lhproto.VariableValue{
"name": {
Value: &lhproto.VariableValue_Str{Str: name},
Expand Down
2 changes: 1 addition & 1 deletion sdk-go/examples/basic/worker/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import (
func main() {
config, _ := examples.LoadConfigAndClient()

tw, err := littlehorse.NewTaskWorker(config, basic.Greet, "greet")
tw, err := littlehorse.NewTaskWorker(config, basic.Greet, basic.TaskDefName)
if err != nil {
log.Fatal(err)
}
Expand Down
5 changes: 4 additions & 1 deletion sdk-go/examples/basic/workflow.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@ import (
"github.com/littlehorse-enterprises/littlehorse/sdk-go/littlehorse"
)

const WorkflowName string = "basic-workflow"
const TaskDefName string = "greet"

func Greet(name string) string {
if name == "obi-wan" {
return "hello there"
Expand All @@ -19,5 +22,5 @@ func MyWorkflow(wf *littlehorse.WorkflowThread) {
// Make it searchable
nameVar.Searchable()

wf.Execute("greet", nameVar)
wf.Execute(TaskDefName, nameVar)
}
10 changes: 5 additions & 5 deletions sdk-go/examples/bytes/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,15 @@ This is a "hello-world" with bytes example of LittleHorse using GoLang.

The file [workflow.go](./workflow.go) has two functions defined:

- `Greet`, which is a Task Function.
- `ToBytesLength`, which is a Task Function.
- `MyWorkflow`, which is the Workflow Function.

## Deploy the Task Worker

Before we can create the `WfSpec`, we need to register the `TaskDef`. The easiest way to do that is to run the task worker:

```
go run ./bytes/worker
go run ./examples/bytes/worker
```

Leave that process running.
Expand All @@ -24,21 +24,21 @@ Leave that process running.
Next, in another terminal, run:

```
go run ./bytes/deploy
go run ./examples/bytes/deploy
```

That will create the `WfSpec`. You can verify that via:

```
lhctl get wfSpec my-workflow
lhctl get wfSpec bytes-workflow
```

## Run a `WfRun`

To run a `WfRun`, you can use `lhctl`.

```
lhctl run my-workflow
lhctl run bytes-workflow
```

It will print out an ID. You can view the status of that `WfRun` via:
Expand Down
2 changes: 1 addition & 1 deletion sdk-go/examples/bytes/deploy/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import (

func main() {
_, client := examples.LoadConfigAndClient()
wf := littlehorse.NewWorkflow(bytes.MyWorkflow, "my-workflow")
wf := littlehorse.NewWorkflow(bytes.MyWorkflow, bytes.WorkflowName)
putWf, err := wf.Compile()
if err != nil {
log.Fatal(err)
Expand Down
2 changes: 1 addition & 1 deletion sdk-go/examples/bytes/worker/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import (
func main() {
config, _ := examples.LoadConfigAndClient()

tw, err := littlehorse.NewTaskWorker(config, bytes.Greet, "greet")
tw, err := littlehorse.NewTaskWorker(config, bytes.ToBytesLength, bytes.TaskDefName)

if err != nil {
log.Fatal(err)
Expand Down
7 changes: 5 additions & 2 deletions sdk-go/examples/bytes/workflow.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,13 @@ import (
"github.com/littlehorse-enterprises/littlehorse/sdk-go/littlehorse"
)

func Greet(byteInput []byte) int {
const WorkflowName string = "bytes-workflow"
const TaskDefName string = "to-bytes"

func ToBytesLength(byteInput []byte) int {
return len(byteInput)
}

func MyWorkflow(wf *littlehorse.WorkflowThread) {
wf.Execute("greet", []byte("hello little horse"))
wf.Execute(TaskDefName, []byte("hello little horse"))
}
6 changes: 3 additions & 3 deletions sdk-go/examples/childthread/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,23 +7,23 @@ This is a simple example of a workflow that launches a Child Thread and waits fo
We have two `TaskDef`'s and thus two Task Functions. Note that `worker/main.go` kicks off two threads, one for each Task Worker.

```
go run ./interrupt/worker
go run ./examples/childthread/worker
```

## Register the `WfSpec`

In another terminal, run:

```
go run ./interrupt/deploy
go run ./examples/childthread/deploy
```

## Run a `WfRun`

Let's run the `WfRun`:

```
lhctl run child-thread input "this is the input!"
lhctl run child-thread-workflow input "this is the input!"
```

You can see both `ThreadRun`s when you inspect the `WfRun`:
Expand Down
2 changes: 1 addition & 1 deletion sdk-go/examples/childthread/deploy/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import (
func main() {
_, client := examples.LoadConfigAndClient()

wf := littlehorse.NewWorkflow(childthread.ChildThreadWorkflow, "child-thread")
wf := littlehorse.NewWorkflow(childthread.ChildThreadWorkflow, childthread.WorkflowName)
putWf, err := wf.Compile()
if err != nil {
log.Fatal(err)
Expand Down
4 changes: 2 additions & 2 deletions sdk-go/examples/childthread/worker/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,12 @@ func main() {
var parentWorker, childWorker *littlehorse.LHTaskWorker
var err error

parentWorker, err = littlehorse.NewTaskWorker(config, childthread.ParentThreadTask, "parent-thread-task")
parentWorker, err = littlehorse.NewTaskWorker(config, childthread.ParentThreadTask, childthread.ParentTaskName)
if err != nil {
log.Fatal(err)
}

childWorker, err = littlehorse.NewTaskWorker(config, childthread.ChildThreadTask, "child-thread-task")
childWorker, err = littlehorse.NewTaskWorker(config, childthread.ChildThreadTask, childthread.ChildTaskName)
if err != nil {
log.Fatal(err)
}
Expand Down
11 changes: 8 additions & 3 deletions sdk-go/examples/childthread/workflow.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,12 @@ import (
"github.com/littlehorse-enterprises/littlehorse/sdk-go/littlehorse"
)

const (
ChildTaskName string = "child-thread-task"
ParentTaskName string = "parent-thread-task"
WorkflowName string = "child-thread-workflow"
)

func ParentThreadTask() string {
return "hello there"
}
Expand All @@ -15,15 +21,14 @@ func ChildThreadTask(input string) string {

func ChildThreadWorkflow(wf *littlehorse.WorkflowThread) {
inputVar := wf.AddVariable("input", lhproto.VariableType_STR)

childThread := wf.SpawnThread(
func(child *littlehorse.WorkflowThread) {
child.Execute("child-thread-task", inputVar)
child.Execute(ChildTaskName, inputVar)
},
"my-child-thread",
nil,
)

wf.Execute("parent-thread-task")
wf.Execute(ParentTaskName)
wf.WaitForThreads(childThread)
}
22 changes: 16 additions & 6 deletions sdk-go/examples/conditionals/ifelse/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,15 +16,15 @@ else:
We have two `TaskDef`'s and thus two Task Functions. Note that `worker/main.go` kicks off two threads, one for each Task Worker.

```
go run ./conditionals/ifelse/worker
go run ./examples/conditionals/ifelse/worker
```

## Register the `WfSpec`

In another terminal, run:

```
go run ./conditionals/ifelse/deploy
go run ./examples/conditionals/ifelse/deploy
```

## Run a `WfRun`
Expand All @@ -35,10 +35,15 @@ Let's run the `WfRun` with a small amount of donuts:
lhctl run donut-workflow number-of-donuts 3
```

Let's view the output of the Task
Let's find and copy the TaskGuid
```
lhctl get nodeRun <wfRunId from before> 0 2
```

Let's view the output of the Task:

```
lhctl get nodeRun <wfRunId from before> 0 2 | jq .result.task.output.str
lhctl get taskRun <wfRunId from before> <taskGuid from before> | jq '.attempts[0].output.str'
```

That should print:
Expand All @@ -53,10 +58,15 @@ Let's run it after we've had too many donuts:
lhctl run donut-workflow number-of-donuts 15
```

Let's view the output of the Task
Let's find and copy the TaskGuid
```
lhctl get nodeRun <wfRunId from before> 0 2
```

Let's view the output of the Task:

```
lhctl get nodeRun <wfRunId from before> 0 2 | jq .result.task.output.str
lhctl get taskRun <wfRunId from before> <taskGuid from before> | jq '.attempts[0].output.str'
```

That should print:
Expand Down
2 changes: 1 addition & 1 deletion sdk-go/examples/conditionals/ifelse/deploy/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import (

func main() {
_, client := examples.LoadConfigAndClient()
wf := littlehorse.NewWorkflow(ifelse.DonutWorkflow, "donut-workflow")
wf := littlehorse.NewWorkflow(ifelse.DonutWorkflow, ifelse.WorkflowName)
putWf, err := wf.Compile()
if err != nil {
log.Fatal(err)
Expand Down
4 changes: 2 additions & 2 deletions sdk-go/examples/conditionals/ifelse/worker/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,12 @@ func main() {
var saladWorker, donutWorker *littlehorse.LHTaskWorker
var err error

saladWorker, err = littlehorse.NewTaskWorker(config, ifelse.Salad, "eat-salad")
saladWorker, err = littlehorse.NewTaskWorker(config, ifelse.Salad, ifelse.EatSaladTaskName)
if err != nil {
log.Fatal(err)
}

donutWorker, err = littlehorse.NewTaskWorker(config, ifelse.Donut, "eat-another-donut")
donutWorker, err = littlehorse.NewTaskWorker(config, ifelse.Donut, ifelse.EatAnotherDonutTaskName)
if err != nil {
log.Fatal(err)
}
Expand Down
Loading

0 comments on commit 1481f20

Please sign in to comment.