README
ΒΆ
Temporal Workflow Examples
This directory contains examples demonstrating how to use Temporal workflows in Go. These examples show practical usage patterns and best practices for implementing Temporal workflows, activities, workers, and schedulers.
π Example Code Locations
Workflow examples:
Other examples:
π Quick Reference for LLMs/Coding Agents
// Basic usage pattern
import "github.com/jasoet/pkg/temporal"
// 1. Define workflow
func MyWorkflow(ctx workflow.Context, input string) (string, error) {
// Workflow logic here
ao := workflow.ActivityOptions{
StartToCloseTimeout: 10 * time.Second,
}
ctx = workflow.WithActivityOptions(ctx, ao)
var result string
err := workflow.ExecuteActivity(ctx, MyActivity, input).Get(ctx, &result)
return result, err
}
// 2. Define activities
func MyActivity(ctx context.Context, input string) (string, error) {
// Activity logic here
return "processed: " + input, nil
}
// 3. Create worker
client, _ := temporal.NewClient(temporal.ClientConfig{
HostPort: "localhost:7233",
})
worker := temporal.NewWorker(client, "my-task-queue")
worker.RegisterWorkflow(MyWorkflow)
worker.RegisterActivity(MyActivity)
// 4. Start worker (in a goroutine)
go func() {
err := worker.Run(context.Background())
if err != nil {
log.Fatal().Err(err).Msg("Worker failed")
}()
// 5. Trigger workflow manually
workflowOptions := client.StartWorkflowOptions{
ID: "my-workflow-id",
TaskQueue: "my-task-queue",
}
we, err := client.ExecuteWorkflow(context.Background(), workflowOptions, MyWorkflow, "input-data")
if err != nil {
log.Error().Err(err).Msg("Failed to start workflow")
}
log.Info().Str("workflow_id", we.GetID()).Str("run_id", we.GetRunID()).Msg("Workflow started")
// Get workflow result
var result string
err = we.Get(context.Background(), &result)
// 6. Or create a scheduler for periodic execution
scheduleManager := temporal.NewScheduleManager(client)
defer scheduleManager.Close()
scheduleID := "my-schedule-id"
scheduleOptions := temporal.WorkflowScheduleOptions{
WorkflowID: "scheduled-workflow-id",
Workflow: MyWorkflow,
TaskQueue: "my-task-queue",
Interval: 1 * time.Hour, // Run every hour
Args: []any{"scheduled-input"},
}
scheduleHandle, err := scheduleManager.CreateWorkflowSchedule(
context.Background(),
scheduleID,
scheduleOptions,
)
if err != nil {
log.Error().Err(err).Msg("Failed to create schedule")
}
log.Info().Str("schedule_id", scheduleID).Msg("Schedule created")
// Delete schedule when done
err = scheduleHandle.Delete(context.Background())
Key features:
- Durable execution with automatic retries
- Built-in error handling and compensation
- Timer and scheduling support
- Integration with logging package
- Manual workflow invocation with ExecuteWorkflow
- Scheduled workflows with cron-like intervals
- Async execution with result retrieval
Directory Structure
pkg/temporal/examples/
βββ workflows/ # Example workflow implementations
βββ activities/ # Example activity implementations
βββ worker/ # Example worker setup
βββ scheduler/ # Example scheduler setup
βββ README.md # This file
Prerequisites
- Go 1.22.2 or later
- Temporal server running (default: localhost:7233)
- Understanding of basic Temporal concepts
Examples Overview
Workflows
The workflows/ directory contains examples of different workflow patterns:
- Simple Sequential Workflow: A basic workflow that executes steps in sequence
- Workflow with Activities: A workflow that calls activities
- Workflow with Error Handling: A workflow demonstrating error handling and retry patterns
- Workflow with Timers: A workflow using timers and delays
Activities
The activities/ directory contains examples of different activity patterns:
- Basic Activities: Simple activity implementations
- Activities with Error Handling: Activities demonstrating error handling and retries
- External Service Activities: Activities that interact with external services
Worker
The worker/ directory contains examples of worker setup:
- Basic Worker: A complete worker setup example
- Worker with Multiple Task Queues: A worker handling multiple task queues
- Worker with Graceful Shutdown: A worker with proper shutdown handling
Scheduler
The scheduler/ directory contains examples of scheduler setup:
- Interval Scheduler: A scheduler using interval-based scheduling
- Cron Scheduler: A scheduler using cron-based scheduling
- One-time Scheduler: A scheduler for one-time execution
How to Run the Examples
Each example directory contains a README.md with specific instructions for running that example. In general, you'll need to:
- Start the Temporal server
- Run the worker in one terminal
- Run the workflow starter in another terminal
Best Practices
- Use the
NewClient()orNewClientWithMetrics()functions from the main temporal package - Always handle errors properly in workflows and activities
- Use proper logging with zerolog
- Implement proper shutdown handling for workers
- Use meaningful task queue names
- Structure your code to separate workflows, activities, and worker setup
Integration with Main Package
These examples use the main temporal package from this project. Key integration points:
- Client creation using
temporal.NewClient()ortemporal.NewClientWithMetrics() - Worker management using
temporal.WorkerManager - Schedule management using
temporal.ScheduleManager - Logging using
temporal.ZerologAdapter
For more details on the main temporal package, see the source code in the parent directory.