Run workflows sequentially

I’m trying to implement a logic that runs workflow sequentially. The only way i think about is using signals and SignalWithStartWorkflow


func (wf *Workflow) Do(ctx workflow.Context) error {
	for {
		signalChan := workflow.GetSignalChannel(ctx, SignalName)
		var signal MySignal
		signalChan.Receive(ctx, &signal)
		workflow.ExecuteActivity(workflow.WithActivityOptions(ctx, workflow.ActivityOptions{
			ScheduleToCloseTimeout: time.Second * 10,
		}), wf.Activity, signal.Id).Get(ctx, nil)
		// ...
	}
}

Looks like this is working implementation, but i fear about infinite for and event history


func (wf *Workflow) Do(ctx workflow.Context) error {
	signalChan := workflow.GetSignalChannel(ctx, SignalName)
	var signal MySignal
	signalChan.Receive(ctx, &signal)
	workflow.ExecuteActivity(workflow.WithActivityOptions(ctx, workflow.ActivityOptions{
		ScheduleToCloseTimeout: time.Second * 10,
	}), wf.Activity, signal.Id).Get(ctx, nil)
	return workflow.NewContinueAsNewError(ctx, wf.Do)
}

The implementation with continue-as-new doesn’t work correctly becase it loose some signals.
What is the best and correct way to implement sequentially running workflows?

You must drain signals from the channel before calling continue as new. Draining means checking that the channel is empty by calling Channel.ReceiveAsync until no messages are left there. You cannot block workflow code between calling ReceiveAsync and returning from the workflow function.

But in this case we will loose this signals. I need to execute activity on each signal.

You will not lose signals if you follow the rules I outlined in my previous message.