Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adding Accumulator Example #355

Open
wants to merge 14 commits into
base: main
Choose a base branch
from
Open

Conversation

joshmsmith
Copy link

What was changed

Adding accumulator pattern sample: accumulator and associated readme changes

Why?

This is a pattern that's pretty common, and I wanted to have a sample that I could share about it. Gathering a lot of signals, managing continue as new, demonstrating signal with start, all good stuff to have.

Checklist

  1. Closes N/A

  2. How was this tested:
    Tested locally, tests in accumulate_signals_workflow_test.go

  3. Any docs updates needed?
    No

Copy link
Member

@cretz cretz left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There's still a lot of little stuff, but I did a first skim for obvious things

accumulator/accumulate_signals_activities.go Outdated Show resolved Hide resolved
accumulator/accumulate_signals_activities.go Outdated Show resolved Hide resolved
accumulator/accumulate_signals_activities.go Outdated Show resolved Hide resolved
accumulator/accumulate_signals_activities.go Outdated Show resolved Hide resolved
accumulator/accumulate_signals_workflow.go Outdated Show resolved Hide resolved
accumulator/accumulate_signals_workflow.go Outdated Show resolved Hide resolved
accumulator/starter/main.go Outdated Show resolved Hide resolved
accumulator/starter/main.go Outdated Show resolved Hide resolved
accumulator/starter/main.go Outdated Show resolved Hide resolved
accumulator/starter/main.go Outdated Show resolved Hide resolved
}

log.Debug("Awaiting for " + timeout.String())
gotSignalBeforeTimeout, _ := workflow.AwaitWithTimeout(ctx, timeout, func() bool {
Copy link
Member

@cretz cretz Jun 27, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

May be worth noting that you're adding two events for every task that has a signal here. You are adding a timer start and potentially a timer cancel. This is technically fine, but may be worth noting. Some users choose to try to reuse timers instead of creating a new one each time.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I reworked this due to your feedback above about using a timer with a selector.

@cretz
Copy link
Member

cretz commented Jun 27, 2024

@Quinn-With-Two-Ns - unsure if you want to give this a review

@Quinn-With-Two-Ns
Copy link
Contributor

@cretz I saw this, I just had no additional comments to add after yours

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

/**
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Probably don't need to duplicate readme content in the file

if totalLeft <= 0 {
return 0, nil
}
if signalToSignalTimeout > totalLeft {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Don't you mean:

Suggested change
if signalToSignalTimeout > totalLeft {
if totalLeft > signalToSignalTimeout {

Since signalToSignalTimeout says it's the "maximum"?

* signal with the GetNextTimeout() function.
*/

// signalToSignalTimeout is them maximum time between signals
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
// signalToSignalTimeout is them maximum time between signals
// signalToSignalTimeout is the maximum time between signals

// This waits for the greater of the remaining fromStartTimeout and signalToSignalTimeout
// fromStartTimeout and signalToSignalTimeout can be adjusted to wait for the right amount of time as desired
// This resets with Continue As New
func (a *AccumulateGreeting) GetNextTimeout(ctx workflow.Context, startTime time.Time, exitRequested bool) (time.Duration, error) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why is this a method on AccumulateGreeting struct? And why exported?



// GetNextTimeout returns the maximum time allowed to wait for the next signal.
func (a *AccumulateGreeting) GetNextTimeout(ctx workflow.Context, timeToExit bool, firstSignalTime time.Time ) (time.Duration, error) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Unresolving, this was not changed

Comment on lines +77 to +79
if greetings.GreetingsList == nil {
greetings.GreetingsList = []AccumulateGreeting{}
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
if greetings.GreetingsList == nil {
greetings.GreetingsList = []AccumulateGreeting{}
}

This is not needed

Comment on lines +155 to +157



Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Go format usually fixes all of these extra lines, is it run?

"fmt"
"log"
"time"

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Usually stdlib imports are grouped together and modern autoformat fixes this usually


"math/rand"

accumulator "github.com/temporalio/samples-go/accumulator"
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
accumulator "github.com/temporalio/samples-go/accumulator"
"github.com/temporalio/samples-go/accumulator"

No need to alias

}
defer c.Close()

// setup which tests to run
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You are inconsistent with whether you capitalize/punctuate sentences. Also, this is a bit confusing to follow.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants