Skip to content

Commit

Permalink
chore(dispatch): activity context abstraction
Browse files Browse the repository at this point in the history
  • Loading branch information
debuggerpk committed Sep 12, 2024
1 parent 1dfa5d9 commit bd57b08
Show file tree
Hide file tree
Showing 2 changed files with 73 additions and 0 deletions.
67 changes: 67 additions & 0 deletions dispatch/defaults.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
package dispatch

import (
"time"

"go.temporal.io/sdk/temporal"
"go.temporal.io/sdk/workflow"

"go.breu.io/durex/queues"
)

// WithDefaultActivityContext returns a workflow.Context with the default activity options applied.
// The default options include a StartToCloseTimeout of 60 seconds.
//
// Example:
//
// ctx = shared.WithDefaultActivityContext(ctx)
func WithDefaultActivityContext(ctx workflow.Context) workflow.Context {
return workflow.WithActivityOptions(ctx, workflow.ActivityOptions{
StartToCloseTimeout: 60 * time.Second,
})
}

// WithIgnoredErrorsContext returns a workflow.Context with activity options configured with a
// StartToCloseTimeout of 60 seconds and a RetryPolicy that allows a single attempt and ignores
// specified error types.
//
// Example:
//
// ignored := []string{"CustomErrorType"}
// ctx = shared.WithIgnoredErrorsContext(ctx, ignored...)
func WithIgnoredErrorsContext(ctx workflow.Context, args ...string) workflow.Context {
return workflow.WithActivityOptions(ctx, workflow.ActivityOptions{
StartToCloseTimeout: 60 * time.Second,
RetryPolicy: &temporal.RetryPolicy{
MaximumAttempts: 1,
NonRetryableErrorTypes: args,
},
})
}

// WithMarathonContext returns a workflow.Context with activity options configured for long-running activities.
// It sets the StartToCloseTimeout to 60 minutes and the HeartbeatTimeout to 30 seconds.
//
// Example:
//
// ctx = shared.WithMarathonContext(ctx)
func WithMarathonContext(ctx workflow.Context) workflow.Context {
return workflow.WithActivityOptions(ctx, workflow.ActivityOptions{
StartToCloseTimeout: 60 * time.Minute,
HeartbeatTimeout: 30 * time.Second,
})
}

// WithCustomQueueContext returns a workflow.Context with activity options configured with a
// StartToCloseTimeout of 60 seconds and a dedicated task queue. This allows scheduling activities
// on a different queue than the one the workflow is running on.
//
// Example:
//
// ctx = shared.WithCustomQueueContext(ctx, queues.MyTaskQueue)
func WithCustomQueueContext(ctx workflow.Context, q queues.Queue) workflow.Context {
return workflow.WithActivityOptions(ctx, workflow.ActivityOptions{
StartToCloseTimeout: 60 * time.Second,
TaskQueue: q.String(),
})
}
6 changes: 6 additions & 0 deletions queues/queues.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,8 @@ type (
// Name gets the name of the queue as string.
Name() Name

String() string

// Prefix gets the prefix of the queue as string.
Prefix() string

Expand Down Expand Up @@ -148,6 +150,10 @@ func (q Name) String() string {
return string(q)
}

func (q *queue) String() string {
return q.name.String()
}

func (q *queue) Name() Name {
return q.name
}
Expand Down

0 comments on commit bd57b08

Please sign in to comment.