Skip to content

Commit

Permalink
Add start delay sample
Browse files Browse the repository at this point in the history
  • Loading branch information
Quinn-With-Two-Ns committed Oct 14, 2023
1 parent e735bec commit b42bf46
Show file tree
Hide file tree
Showing 3 changed files with 79 additions and 0 deletions.
12 changes: 12 additions & 0 deletions start-delay/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
This sample demonstrates how to setup a workflow to start after a delay.

### Steps to run this sample:
1) Run a [Temporal service](https://github.com/temporalio/samples-go/tree/main/#how-to-use).
2) Run the following command to start the worker
```
go run start-delay/worker/main.go
```
3) Run the following command to start the example
```
go run start-delay/starter/main.go
```
36 changes: 36 additions & 0 deletions start-delay/starter/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
package main

import (
"context"
"log"
"time"

"github.com/pborman/uuid"
"github.com/temporalio/samples-go/helloworld"
"go.temporal.io/sdk/client"
)

func main() {
// The client is a heavyweight object that should be created once per process.
c, err := client.Dial(client.Options{
HostPort: client.DefaultHostPort,
})
if err != nil {
log.Fatalln("Unable to create client", err)
}
defer c.Close()

workflowOptions := client.StartWorkflowOptions{
ID: "startdelay_" + uuid.New(),
TaskQueue: "startdelay",
// The first workflow task will be dispatched in 5 minutes
StartDelay: 5 * time.Minute,
}

we, err := c.ExecuteWorkflow(context.Background(), workflowOptions, helloworld.Workflow, "from a delayed workflow")
if err != nil {
log.Fatalln("Unable to execute workflow", err)
}
// ExecuteWorkflow will return immediately, but the workflow won't start executing till the StartDelay expires.
log.Println("Scheduled workflow", "WorkflowID", we.GetID(), "RunID", we.GetRunID())
}
31 changes: 31 additions & 0 deletions start-delay/worker/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package main

import (
"log"

"go.temporal.io/sdk/client"
"go.temporal.io/sdk/worker"

"github.com/temporalio/samples-go/helloworld"
)

func main() {
// The client and worker are heavyweight objects that should be created once per process.
c, err := client.Dial(client.Options{
HostPort: client.DefaultHostPort,
})
if err != nil {
log.Fatalln("Unable to create client", err)
}
defer c.Close()

w := worker.New(c, "startdelay", worker.Options{})

w.RegisterWorkflow(helloworld.Workflow)
w.RegisterActivity(helloworld.Activity)

err = w.Run(worker.InterruptCh())
if err != nil {
log.Fatalln("Unable to start worker", err)
}
}

0 comments on commit b42bf46

Please sign in to comment.