8000 feat: go worker assignment by grutt · Pull Request #741 · hatchet-dev/hatchet · GitHub
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content

feat: go worker assignment #741

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

Merged
merged 13 commits into from
Jul 26, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 38 additions & 0 deletions examples/assignment-affinity/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
package main

import (
"fmt"

"github.com/joho/godotenv"

"github.com/hatchet-dev/hatchet/pkg/cmdutils"
)

type userCreateEvent struct {
Username string `json:"username"`
UserID string `json:"user_id"`
Data map[string]string `json:"data"`
}

type stepOneOutput struct {
Message string `json:"message"`
}

func main() {
err := godotenv.Load()
if err != nil {
panic(err)
}

ch := cmdutils.InterruptChan()
cleanup, err := run()
if err != nil {
panic(err)
}

<-ch

if err := cleanup(); err != nil {
panic(fmt.Errorf("cleanup() error = %v", err))
}
}
106 changes: 106 additions & 0 deletions examples/assignment-affinity/run.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,106 @@
package main

import (
"context"
"fmt"
"log"
"time"

"github.com/hatchet-dev/hatchet/pkg/client"
"github.com/hatchet-dev/hatchet/pkg/client/types"
"github.com/hatchet-dev/hatchet/pkg/worker"
)

func run() (func() error, error) {
c, err := client.New()
if err != nil {
return nil, fmt.Errorf("error creating client: %w", err)
}

w, err := worker.NewWorker(
worker.WithClient(
c,
),
worker.WithLabels(map[string]interface{}{
"model": "fancy-ai-model-v2",
"memory": 512,
}),
)
if err != nil {
return nil, fmt.Errorf("error creating worker: %w", err)
}

err = w.RegisterWorkflow(
&worker.WorkflowJob{
On: worker.Events("user:create:affinity"),
Name: "affinity",
Description: "affinity",
Steps: []*worker.WorkflowStep{
worker.Fn(func(ctx worker.HatchetContext) (result *stepOneOutput, err error) {

model := ctx.Worker().GetLabels()["model"]

if model != "fancy-ai-model-v3" {
ctx.Worker().UpsertLabels(map[string]interface{}{
"model": nil,
})
// Do something to load the model
ctx.Worker().UpsertLabels(map[string]interface{}{
"model": "fancy-ai-model-v3",
})
}

return &stepOneOutput{
Message: ctx.Worker().ID(),
}, nil
}).
SetName("step-one").
SetDesiredLabels(map[string]*types.DesiredWorkerLabel{
"model": {
Value: "fancy-ai-model-v3",
Weight: 10,
},
"memory": {
Value: 512,
Required: true,
Comparator: types.ComparatorPtr(types.WorkerLabelComparator_GREATER_THAN),
},
}),
},
},
)
if err != nil {
return nil, fmt.Errorf("error registering workflow: %w", err)
}

go func() {
log.Printf("pushing event")

testEvent := userCreateEvent{
Username: "echo-test",
UserID: "1234",
Data: map[string]string{
"test": "test",
},
}

// push an event
err := c.Event().Push(
context.Background(),
"user:create:affinity",
testEvent,
)
if err != nil {
panic(fmt.Errorf("error pushing event: %w", err))
}

time.Sleep(10 * time.Second)
}()

cleanup, err := w.Start()
if err != nil {
return nil, fmt.Errorf("error starting worker: %w", err)
}

return cleanup, nil
}
38 changes: 38 additions & 0 deletions examples/assignment-sticky/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
package main

import (
"fmt"

"github.com/joho/godotenv"

"github.com/hatchet-dev/hatchet/pkg/cmdutils"
)

type userCreateEvent struct {
Username string `json:"username"`
UserID string `json:"user_id"`
Data map[string]string `json:"data"`
}

type stepOneOutput struct {
Message string `json:"message"`
}

func main() {
err := godotenv.Load()
if err != nil {
panic(err)
}

ch := cmdutils.InterruptChan()
cleanup, err := run()
if err != nil {
panic(err)
}

<-ch

if err := cleanup(); err != nil {
panic(fmt.Errorf("cleanup() error = %v", err))
}
}
99 changes: 99 additions & 0 deletions examples/assignment-sticky/run.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
package main

import (
"context"
"fmt"
"log"
"time"

"github.com/hatchet-dev/hatchet/pkg/client"
"github.com/hatchet-dev/hatchet/pkg/client/types"
"github.com/hatchet-dev/hatchet/pkg/worker"
)

func run() (func() error, error) {
c, err := client.New()
if err != nil {
return nil, fmt.Errorf("error creating client: %w", err)
}

w, err := worker.NewWorker(
worker.WithClient(
c,
),
)
if err != nil {
return nil, fmt.Errorf("error creating worker: %w", err)
}

err = w.RegisterWorkflow(
&worker.WorkflowJob{
On: worker.Events("user:create:sticky"),
Name: "sticky",
Description: "sticky",
StickyStrategy: types.StickyStrategyPtr(types.StickyStrategy_SOFT),
Steps: []*worker.WorkflowStep{
worker.Fn(func(ctx worker.HatchetContext) (result *stepOneOutput, err error) {

sticky := true

_, err = ctx.SpawnWorkflow("step-one", nil, &worker.SpawnWorkflowOpts{
Sticky: &sticky,
})

if err != nil {
return nil, fmt.Errorf("error spawning workflow: %w", err)
}

return &stepOneOutput{
Message: ctx.Worker().ID(),
}, nil
}).SetName("step-one"),
worker.Fn(func(ctx worker.HatchetContext) (result *stepOneOutput, err error) {
return &stepOneOutput{
Message: ctx.Worker().ID(),
}, nil
}).SetName("step-two"),
worker.Fn(func(ctx worker.HatchetContext) (result *stepOneOutput, err error) {
return &stepOneOutput{
Message: ctx.Worker().ID(),
}, nil
}).SetName("step-three").AddParents("step-one", "step-two"),
},
},
)
if err != nil {
return nil, fmt.Errorf("error registering workflow: %w", err)
}

go func() {
log.Printf("pushing event")

testEvent := userCreateEvent{
Username: "echo-test",
UserID: "1234",
Data: map[string]string{
"test": "test",
},
}

// push an event
err := c.Event().Push(
context.Background(),
"user:create:sticky",
testEvent,
)
if err != nil {
panic(fmt.Errorf("error pushing event: %w", err))
}

time.Sleep(10 * time.Second)
}()

cleanup, err := w.Start()
if err != nil {
return nil, fmt.Errorf("error starting worker: %w", err)
}

return cleanup, nil
}
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,31 @@ const myWorkflow: Workflow = {
</Tabs.Tab>
<Tabs.Tab>
```go
// TODO add go example
err = w.RegisterWorkflow(
&worker.WorkflowJob{
On: worker.Events("user:create:sticky"),
Name: "sticky",
Description: "sticky",
StickyStrategy: types.StickyStrategyPtr(types.StickyStrategy_SOFT),
Steps: []*worker.WorkflowStep{
worker.Fn(func(ctx worker.HatchetContext) (result *stepOneOutput, err error) {
return &stepOneOutput{
Message: ctx.Worker().ID(),
}, nil
}).SetName("step-one"),
worker.Fn(func(ctx worker.HatchetContext) (result *stepOneOutput, err error) {
return &stepOneOutput{
Message: ctx.Worker().ID(),
}, nil
}).SetName("step-two"),
worker.Fn(func(ctx worker.HatchetContext) (result *stepOneOutput, err error) {
return &stepOneOutput{
Message: ctx.Worker().ID(),
}, nil
}).SetName("step-three").AddParents("step-one", "step-two"),
},
},
)
```
</Tabs.Tab>
</Tabs>
Expand Down Expand Up @@ -143,7 +167,33 @@ const parentWorkflow: Workflow = {
</Tabs.Tab>
<Tabs.Tab>
```go
// TODO go example
err = w.RegisterWorkflow(
&worker.WorkflowJob{
On: worker.Events("user:create:sticky"),
Name: "sticky",
Description: "sticky",
Steps: []*worker.WorkflowStep{
worker.Fn(func(ctx worker.HatchetContext) (result *stepOneOutput, err error) {

sticky := true

_, err = ctx.SpawnWorkflow("sticky-child", nil, &worker.SpawnWorkflowOpts{
Sticky: &sticky,
})

if err != nil {
return nil, fmt.Errorf("error spawning workflow: %w", err)
}

return &stepOneOutput{
Message: ctx.Worker().ID(),
}, nil
}).SetName("step-one"),
},
},
)

```
</Tabs.Tab>
</Tabs>
```
Loading
Loading
0