-
Notifications
You must be signed in to change notification settings - Fork 200
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Rename Sticky Activities sample (#316)
- Loading branch information
Showing
12 changed files
with
122 additions
and
117 deletions.
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
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
# Worker-Specific Task Queues | ||
|
||
*[Sessions](https://docs.temporal.io/dev-guide/go/features#worker-sessions) are an alternative to Worker-specific Tasks Queues.* | ||
|
||
Use a unique Task Queue for each Worker in order to have certain Activities run on a specific Worker. | ||
|
||
This is useful in scenarios where multiple Activities need to run in the same process or on the same host, for example to share memory or disk. This sample has a file processing Workflow, where one Activity downloads the file to disk and other Activities process it and clean it up. | ||
|
||
The strategy is: | ||
|
||
- Each Worker process creates two `worker` instances: | ||
- One instance listens on the `shared-task-queue` Task Queue. | ||
- Another instance listens on a uniquely generated Task Queue (in this case, `uuid` is used, but you can inject smart logic here to uniquely identify the Worker, [as Netflix did](https://community.temporal.io/t/using-dynamic-task-queues-for-traffic-routing/3045)). | ||
- The Workflow and the first Activity are run on `shared-task-queue`. | ||
- The first Activity returns one of the uniquely generated Task Queues (that only one Worker is listening on—i.e. the **Worker-specific Task Queue**). | ||
- The rest of the Activities do the file processing and are run on the Worker-specific Task Queue. | ||
|
||
Activities have been artificially slowed with `time.Sleep(3 * time.Second)` to simulate slow activities. | ||
|
||
### Running this sample | ||
|
||
```bash | ||
go run worker-specific-task-queues/worker/main.go | ||
``` | ||
|
||
Start the Workflow Execution: | ||
|
||
```bash | ||
go run worker-specific-task-queues/starter/main.go | ||
``` |
2 changes: 1 addition & 1 deletion
2
activities-sticky-queues/activities.go → worker-specific-task-queues/activities.go
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 |
---|---|---|
@@ -1,4 +1,4 @@ | ||
package activities_sticky_queues | ||
package worker_specific_task_queues | ||
|
||
import ( | ||
"bytes" | ||
|
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
package main | ||
|
||
import ( | ||
"log" | ||
"sync" | ||
|
||
worker_specific_task_queues "github.com/temporalio/samples-go/worker-specific-task-queues" | ||
|
||
"go.temporal.io/sdk/activity" | ||
"go.temporal.io/sdk/client" | ||
"go.temporal.io/sdk/worker" | ||
|
||
"github.com/google/uuid" | ||
) | ||
|
||
func main() { | ||
// The client and worker are heavyweight objects that should generally be created once per process. | ||
// In this case, we create a single client but two workers since we need to handle Activities on multiple task queues. | ||
c, err := client.Dial(client.Options{}) | ||
if err != nil { | ||
log.Fatalln("Unable to create client", err) | ||
} | ||
defer c.Close() | ||
uniqueTaskQueue := worker_specific_task_queues.WorkerSpecificTaskQueue{ | ||
TaskQueue: uuid.New().String(), | ||
} | ||
var wg sync.WaitGroup | ||
wg.Add(1) | ||
go func() { | ||
defer wg.Done() | ||
w := worker.New(c, "shared-task-queue", worker.Options{}) | ||
w.RegisterWorkflow(worker_specific_task_queues.FileProcessingWorkflow) | ||
|
||
w.RegisterActivityWithOptions(uniqueTaskQueue.GetWorkerSpecificTaskQueue, activity.RegisterOptions{ | ||
Name: "GetWorkerSpecificTaskQueue", | ||
}) | ||
err = w.Run(worker.InterruptCh()) | ||
if err != nil { | ||
log.Fatalln("Unable to start worker", err) | ||
} | ||
}() | ||
|
||
wg.Add(1) | ||
go func() { | ||
defer wg.Done() | ||
// Create a new worker listening on the unique queue | ||
uniqueTaskQueueWorker := worker.New(c, uniqueTaskQueue.TaskQueue, worker.Options{}) | ||
|
||
uniqueTaskQueueWorker.RegisterActivity(worker_specific_task_queues.DownloadFile) | ||
uniqueTaskQueueWorker.RegisterActivity(worker_specific_task_queues.ProcessFile) | ||
uniqueTaskQueueWorker.RegisterActivity(worker_specific_task_queues.DeleteFile) | ||
|
||
err = uniqueTaskQueueWorker.Run(worker.InterruptCh()) | ||
if err != nil { | ||
log.Fatalln("Unable to start worker", err) | ||
} | ||
}() | ||
// Wait for both workers to close | ||
wg.Wait() | ||
} |
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,14 @@ | ||
package worker_specific_task_queues | ||
|
||
import ( | ||
"context" | ||
) | ||
|
||
type WorkerSpecificTaskQueue struct { | ||
TaskQueue string | ||
} | ||
|
||
// GetWorkerSpecificTaskQueue is an activity to get a hosts unique task queue. | ||
func (q WorkerSpecificTaskQueue) GetWorkerSpecificTaskQueue(ctx context.Context) (string, error) { | ||
return q.TaskQueue, 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
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