Session recovery after work restart

Hey, I’m trying to use sessions on my Temporal workers. When I’m redeploying workers the activities get shut down gracefully(not always), however the session is then lost and not completed. Is there a way when the workflow gets started again to recreate the same session and continue the activities?

I’ve created a very basic test setup where I’ve tested multiple approaches but can’t really get it to work.

  1. Start the worker.
  2. Start the workflow.
  3. When activity 1 is being executed send a SIGINT.
  4. Restart the worker.
  5. Activity 2 is cancelled.
func Workflow(ctx workflow.Context, name string) (string, error) {
	ao := workflow.ActivityOptions{
		StartToCloseTimeout: 300 * time.Second,
	}

	var err error
	ctx = workflow.WithActivityOptions(ctx, ao)
	ctx, err = workflow.CreateSession(ctx, &workflow.SessionOptions{
		ExecutionTimeout: 300 * time.Second,
		CreationTimeout:  60 * time.Second,
		HeartbeatTimeout: 10 * time.Second,
	})
	if err != nil {
		return "", err
	}
	defer workflow.CompleteSession(ctx)

	logger := workflow.GetLogger(ctx)
	logger.Info("HelloWorld workflow started", "name", name)

	var r1 string
	var r2 string

	// activity 1
	if err := workflow.ExecuteActivity(ctx, Activity, name).Get(ctx, &r1); err != nil {
		return "", err
	}

	// activity 2
	if err := workflow.ExecuteActivity(ctx, Activity, name).Get(ctx, &r2); err != nil {
		return "", err
	}

	return r1 + r2, nil
}

func Activity(ctx context.Context, name string) (string, error) {
	logger := activity.GetLogger(ctx)
	logger.Info("Activity start", "name", name)

	for i := 0; i <= 5; i++ {
		time.Sleep(time.Second)
		fmt.Println(name, i)
		activity.RecordHeartbeat(ctx)
	}

	logger.Info("Activity end", "name", name)
	return "Hello " + name + "!", nil
}