Skip to content

Instantly share code, notes, and snippets.

@longquanzheng
Last active February 24, 2023 02:19
Show Gist options
  • Save longquanzheng/30d3b53a201a9caaaca4f08d1fa0dabe to your computer and use it in GitHub Desktop.
Save longquanzheng/30d3b53a201a9caaaca4f08d1fa0dabe to your computer and use it in GitHub Desktop.
package main
import (
"context"
"go.temporal.io/sdk/client"
"go.temporal.io/sdk/temporal"
"go.temporal.io/sdk/workflow"
"time"
)
// Define the sendEmail activity interface
func sendEmail(ctx context.Context, email string) error {
// TODO: Replace this with actual email sending logic
workflow.GetLogger(ctx).Info("Sending email to:", email)
// Return nil to indicate successful completion of the activity
return nil
}
// Define the workflow interface
func helloWorkflow(ctx workflow.Context, email1 string, email2 string) error {
// Print a message to the console
workflow.GetLogger(ctx).Info("Hello, world!")
// Send the first email
err := workflow.ExecuteActivity(ctx, sendEmail, email1).Get(ctx, nil)
if err != nil {
workflow.GetLogger(ctx).Error("Failed to send email:", err)
}
// Create a timer that waits for one day
timer := workflow.NewTimer(ctx, time.Hour*24)
selector := workflow.NewSelector(ctx)
var timerFired bool
selector.AddFuture(timer, func(f workflow.Future) {
timerFired = true
})
// Wait for either the timer to fire or the workflow to be cancelled
selector.Select(ctx)
if timerFired {
// Send the second email after the timer fires
err := workflow.ExecuteActivity(ctx, sendEmail, email2).Get(ctx, nil)
if err != nil {
workflow.GetLogger(ctx).Error("Failed to send email:", err)
}
}
// Return nil to indicate successful completion of the workflow
return nil
}
func main() {
// Set up the Temporal client
c, err := client.NewClient(client.Options{})
if err != nil {
panic(err)
}
// Register the workflow with the versioning API
// This should be done once per version of the workflow
workflow.RegisterWithOptions(helloWorkflow, workflow.RegisterOptions{Name: "hello-workflow", EnableSessionWorker: true})
// Get the workflow client and start a new workflow execution
workflowClient := c.NewWorkflowClient()
options := client.StartWorkflowOptions{
TaskQueue: "default",
}
we, err := workflowClient.ExecuteWorkflow(context.Background(), options, "hello-workflow", "example1@example.com", "example2@example.com")
if err != nil {
panic(err)
}
// Wait for the workflow to complete
var result string
err = we.Get(context.Background(), &result)
if err != nil {
panic(err)
}
// Print the workflow result to the console
workflow.GetLogger(context.Background()).Info("Workflow result:", result)
// Signal the workflow to run the new version
// This can be done at any time after the initial workflow execution has started
err = we.SignalWorkflow(context.Background(), "upgrade", "example1@example.com", "example2@example.com")
if err != nil {
panic(err)
}
// Wait for the upgraded workflow to complete
err = we.Get(context.Background(), &result)
if err != nil {
panic(err)
}
// Print the upgraded workflow result to the console
workflow.GetLogger(context.Background()).Info("Upgraded workflow result:", result)
}
// Define the upgrade function
func upgradeWorkflow(ctx workflow.Context, email string) error {
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment