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

Pass workflow events as pointers #348

Merged
merged 1 commit into from
May 17, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion backend/backend.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ type Backend interface {
// completed or other workflow instances.
CompleteWorkflowTask(
ctx context.Context, task *WorkflowTask, instance *workflow.Instance, state core.WorkflowInstanceState,
executedEvents, activityEvents, timerEvents []*history.Event, workflowEvents []history.WorkflowEvent) error
executedEvents, activityEvents, timerEvents []*history.Event, workflowEvents []*history.WorkflowEvent) error

// GetActivityTask returns a pending activity task or nil if there are no pending activities
GetActivityTask(ctx context.Context) (*ActivityTask, error)
Expand Down
6 changes: 3 additions & 3 deletions backend/history/grouping.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,14 @@ package history

import "github.com/cschleiden/go-workflows/core"

func EventsByWorkflowInstance(events []WorkflowEvent) map[core.WorkflowInstance][]WorkflowEvent {
groupedEvents := make(map[core.WorkflowInstance][]WorkflowEvent)
func EventsByWorkflowInstance(events []*WorkflowEvent) map[core.WorkflowInstance][]*WorkflowEvent {
groupedEvents := make(map[core.WorkflowInstance][]*WorkflowEvent)

for _, m := range events {
instance := *m.WorkflowInstance

if _, ok := groupedEvents[instance]; !ok {
groupedEvents[instance] = []WorkflowEvent{}
groupedEvents[instance] = []*WorkflowEvent{}
}

groupedEvents[instance] = append(groupedEvents[instance], m)
Expand Down
2 changes: 1 addition & 1 deletion backend/history/grouping_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ func TestGrouping_MultipleEventsSameInstance(t *testing.T) {
id := uuid.NewString()
instance := core.NewWorkflowInstance(id, "exid")

r := EventsByWorkflowInstance([]WorkflowEvent{
r := EventsByWorkflowInstance([]*WorkflowEvent{
{
WorkflowInstance: instance,
HistoryEvent: NewPendingEvent(time.Now(), EventType_SubWorkflowScheduled, &SubWorkflowScheduledAttributes{}),
Expand Down
4 changes: 2 additions & 2 deletions backend/mock_Backend.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion backend/monoprocess/monoprocess.go
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ func (b *monoprocessBackend) CompleteWorkflowTask(
instance *workflow.Instance,
state core.WorkflowInstanceState,
executedEvents, activityEvents, timerEvents []*history.Event,
workflowEvents []history.WorkflowEvent,
workflowEvents []*history.WorkflowEvent,
) error {
if err := b.Backend.CompleteWorkflowTask(ctx, task, instance, state, executedEvents, activityEvents, timerEvents, workflowEvents); err != nil {
return err
Expand Down
2 changes: 1 addition & 1 deletion backend/mysql/mysql.go
Original file line number Diff line number Diff line change
Expand Up @@ -535,7 +535,7 @@ func (b *mysqlBackend) CompleteWorkflowTask(
instance *workflow.Instance,
state core.WorkflowInstanceState,
executedEvents, activityEvents, timerEvents []*history.Event,
workflowEvents []history.WorkflowEvent,
workflowEvents []*history.WorkflowEvent,
) error {
tx, err := b.db.BeginTx(ctx, &sql.TxOptions{
Isolation: sql.LevelReadCommitted,
Expand Down
4 changes: 4 additions & 0 deletions backend/options.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,10 @@ type Options struct {
// ActivityLockTimeout determines how long an activity task can be locked for. If the activity task is not completed
// by that timeframe, it's considered abandoned and another worker might pick it up
ActivityLockTimeout time.Duration

WorkflowNamespace string

ActivityNamespace string
}

var DefaultOptions Options = Options{
Expand Down
2 changes: 1 addition & 1 deletion backend/redis/workflow.go
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ func (rb *redisBackend) CompleteWorkflowTask(
instance *core.WorkflowInstance,
state core.WorkflowInstanceState,
executedEvents, activityEvents, timerEvents []*history.Event,
workflowEvents []history.WorkflowEvent,
workflowEvents []*history.WorkflowEvent,
) error {
keys := make([]string, 0)
args := make([]interface{}, 0)
Expand Down
2 changes: 1 addition & 1 deletion backend/sqlite/sqlite.go
Original file line number Diff line number Diff line change
Expand Up @@ -459,7 +459,7 @@ func (sb *sqliteBackend) CompleteWorkflowTask(
instance *workflow.Instance,
state core.WorkflowInstanceState,
executedEvents, activityEvents, timerEvents []*history.Event,
workflowEvents []history.WorkflowEvent,
workflowEvents []*history.WorkflowEvent,
) error {
tx, err := sb.db.BeginTx(ctx, nil)
if err != nil {
Expand Down
12 changes: 6 additions & 6 deletions backend/test/backendtest.go
Original file line number Diff line number Diff line change
Expand Up @@ -174,11 +174,11 @@ func BackendTest(t *testing.T, setup func(options ...backend.BackendOption) Test
require.NotNil(t, tk)

// Complete workflow task
err = b.CompleteWorkflowTask(ctx, tk, wfi, core.WorkflowInstanceStateActive, tk.NewEvents, []*history.Event{}, []*history.Event{}, []history.WorkflowEvent{})
err = b.CompleteWorkflowTask(ctx, tk, wfi, core.WorkflowInstanceStateActive, tk.NewEvents, []*history.Event{}, []*history.Event{}, []*history.WorkflowEvent{})
require.NoError(t, err)

// Task is already completed, this should error
err = b.CompleteWorkflowTask(ctx, tk, wfi, core.WorkflowInstanceStateActive, tk.NewEvents, []*history.Event{}, []*history.Event{}, []history.WorkflowEvent{})
err = b.CompleteWorkflowTask(ctx, tk, wfi, core.WorkflowInstanceStateActive, tk.NewEvents, []*history.Event{}, []*history.Event{}, []*history.WorkflowEvent{})
require.Error(t, err)
},
},
Expand Down Expand Up @@ -212,7 +212,7 @@ func BackendTest(t *testing.T, setup func(options ...backend.BackendOption) Test
activityScheduledEvent,
}

workflowEvents := []history.WorkflowEvent{}
workflowEvents := []*history.WorkflowEvent{}

err = b.CompleteWorkflowTask(ctx, task, wfi, core.WorkflowInstanceStateActive, events, activityEvents, []*history.Event{}, workflowEvents)
require.NoError(t, err)
Expand Down Expand Up @@ -257,7 +257,7 @@ func BackendTest(t *testing.T, setup func(options ...backend.BackendOption) Test
events[i].SequenceID = sequenceID
}

err = b.CompleteWorkflowTask(ctx, task, wfi, core.WorkflowInstanceStateFinished, events, []*history.Event{}, []*history.Event{}, []history.WorkflowEvent{})
err = b.CompleteWorkflowTask(ctx, task, wfi, core.WorkflowInstanceStateFinished, events, []*history.Event{}, []*history.Event{}, []*history.WorkflowEvent{})
require.NoError(t, err)

time.Sleep(time.Second)
Expand Down Expand Up @@ -285,7 +285,7 @@ func BackendTest(t *testing.T, setup func(options ...backend.BackendOption) Test
// Simulate context and sub-workflow cancellation
task, err := b.GetWorkflowTask(ctx)
require.NoError(t, err)
err = b.CompleteWorkflowTask(ctx, task, instance, core.WorkflowInstanceStateActive, task.NewEvents, []*history.Event{}, []*history.Event{}, []history.WorkflowEvent{
err = b.CompleteWorkflowTask(ctx, task, instance, core.WorkflowInstanceStateActive, task.NewEvents, []*history.Event{}, []*history.Event{}, []*history.WorkflowEvent{
{
WorkflowInstance: subInstance1,
HistoryEvent: history.NewHistoryEvent(1, time.Now(), history.EventType_WorkflowExecutionCanceled, &history.SubWorkflowCancellationRequestedAttributes{
Expand Down Expand Up @@ -374,6 +374,6 @@ func startWorkflow(t *testing.T, ctx context.Context, b backend.Backend, c *clie
require.NoError(t, err)

err = b.CompleteWorkflowTask(
ctx, task, instance, core.WorkflowInstanceStateActive, task.NewEvents, []*history.Event{}, []*history.Event{}, []history.WorkflowEvent{})
ctx, task, instance, core.WorkflowInstanceStateActive, task.NewEvents, []*history.Event{}, []*history.Event{}, []*history.WorkflowEvent{})
require.NoError(t, err)
}
2 changes: 1 addition & 1 deletion internal/command/command.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ type CommandResult struct {
Events []*history.Event
ActivityEvents []*history.Event
TimerEvents []*history.Event
WorkflowEvents []history.WorkflowEvent
WorkflowEvents []*history.WorkflowEvent
}

type command struct {
Expand Down
2 changes: 1 addition & 1 deletion internal/command/complete_workflow.go
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ func (c *CompleteWorkflowCommand) Execute(clock clock.Clock) *CommandResult {
)
}

r.WorkflowEvents = []history.WorkflowEvent{
r.WorkflowEvents = []*history.WorkflowEvent{
{
WorkflowInstance: c.Instance.Parent,
HistoryEvent: historyEvent,
Expand Down
2 changes: 1 addition & 1 deletion internal/command/continueasnew.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ func (c *ContinueAsNewCommand) Execute(clock clock.Clock) *CommandResult {
},
),
},
WorkflowEvents: []history.WorkflowEvent{
WorkflowEvents: []*history.WorkflowEvent{
// Schedule a new workflow execution
{
WorkflowInstance: continuedInstance,
Expand Down
4 changes: 2 additions & 2 deletions internal/command/schedule_subworkflow.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ func (c *ScheduleSubWorkflowCommand) Execute(clock clock.Clock) *CommandResult {
),
},
// Send event to new workflow instance
WorkflowEvents: []history.WorkflowEvent{
WorkflowEvents: []*history.WorkflowEvent{
{
WorkflowInstance: c.Instance,
HistoryEvent: history.NewPendingEvent(
Expand Down Expand Up @@ -98,7 +98,7 @@ func (c *ScheduleSubWorkflowCommand) Execute(clock clock.Clock) *CommandResult {
},

// Send cancellation event to sub-workflow
WorkflowEvents: []history.WorkflowEvent{
WorkflowEvents: []*history.WorkflowEvent{
{
WorkflowInstance: c.Instance,
HistoryEvent: history.NewWorkflowCancellationEvent(clock.Now()),
Expand Down
4 changes: 2 additions & 2 deletions tester/tester.go
Original file line number Diff line number Diff line change
Expand Up @@ -762,7 +762,7 @@ func (wt *workflowTester[TResult]) addWorkflow(instance *core.WorkflowInstance,
return tw
}

func (wt *workflowTester[TResult]) scheduleSubWorkflow(event history.WorkflowEvent) {
func (wt *workflowTester[TResult]) scheduleSubWorkflow(event *history.WorkflowEvent) {
a := event.HistoryEvent.Attributes.(*history.ExecutionStartedAttributes)

// TODO: Right location to call handler?
Expand Down Expand Up @@ -829,7 +829,7 @@ func (wt *workflowTester[TResult]) scheduleSubWorkflow(event history.WorkflowEve
0, event.WorkflowInstance, workflowResult, workflowerrors.FromError(workflowRawErr),
).Execute(wt.clock)

return &r.WorkflowEvents[0]
return r.WorkflowEvents[0]
}
}

Expand Down
4 changes: 2 additions & 2 deletions workflow/executor/executor.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ type ExecutionResult struct {
TimerEvents []*history.Event

// Events for other workflow instances
WorkflowEvents []history.WorkflowEvent
WorkflowEvents []*history.WorkflowEvent
}

type WorkflowHistoryProvider interface {
Expand Down Expand Up @@ -205,7 +205,7 @@ func (e *executor) ExecuteTask(ctx context.Context, t *backend.WorkflowTask) (*E
newCommandEvents := make([]*history.Event, 0)
activityEvents := make([]*history.Event, 0)
timerEvents := make([]*history.Event, 0)
workflowEvents := make([]history.WorkflowEvent, 0)
workflowEvents := make([]*history.WorkflowEvent, 0)

for _, c := range e.workflowState.Commands() {
if c.State() == command.CommandState_Done {
Expand Down
Loading