Sorry for the delayed response. @Spencer_Judge
the code format
the workflow client code
wfopts := client.StartWorkflowOptions{
TaskQueue: temporalTaskQueue,
ID: "CreateMatches_" + tournament.Id,
RetryPolicy: &temporal.RetryPolicy{InitialInterval: time.Second, BackoffCoefficient: 1.2},
}
wr, err := s.wfClient.ExecuteWorkflow(ctx, wfopts, workflowCreateMatches, mProfiles, houseId, tournament.Id, tournament.T3Timestamp)
if err != nil {
log.Error(ctx, "error starting create MatchCreation workflow:%w", err, log.KV("tourId", tournament.Id))
return errors.WithStack(err)
}
log.Debug(ctx, "scheduled matchmaking workflow", log.KV("WorkflowID", wr.GetID()),
log.KV("RunID", wr.GetRunID()), log.KV("tourId", tournament.Id))
var wErr error
wErr = wr.Get(ctx, &wErr)
if wErr != nil {
return fmt.Errorf("TriggerCreateMatches failed: %w", wErr)
}
the workflow code
func workflowCreateMatches(ctx workflow.Context, mProfiles []MatchProfile, houseId, tourId string, t3Time time.Time) error {
// should not use custom logger
logger := workflow.GetLogger(ctx)
logger.Debug("starting match creation workflow", tourId)
actx := workflow.WithActivityOptions(ctx, workflow.ActivityOptions{
ScheduleToCloseTimeout: 1 * 24 * 7 * 52 * time.Hour,
HeartbeatTimeout: createAndAssignMatchesActivityTimeOut,
//WaitForCancellation: true,
RetryPolicy: &temporal.RetryPolicy{InitialInterval: time.Second, BackoffCoefficient: 1},
})
err := workflow.ExecuteActivity(actx, new(DirectorSvc).CreateAndAssignMatches, mProfiles, houseId, tourId, t3Time).Get(ctx, nil)
if err != nil {
if temporal.IsCanceledError(err) {
return nil
}
return fmt.Errorf("failed to exicute CreateMatches: %w", err)
}
return nil
}
the activity code
func (d *DirectorSvc) CreateAndAssignMatches(ctx context.Context, mProfiles []MatchProfile, houseId, tourId string, t3Time time.Time) error {
heartBeatTime := time.Time{}
for {
if time.Now().After(t3Time) {
log.Info(ctx, "tournament time ended match assignment stopped", log.KV("tourId", tourId), log.KV("t3Time", t3Time))
return nil
}
select {
case <-ctx.Done():
log.Info(ctx, "context is cancelled, tournament ended match assignment stopped", log.KV("tourId", tourId))
return nil
case <-time.After(2 * time.Second):
{
//temporal internally throttle RecordHeartbeat , we don't need to make it explicitly
activity.RecordHeartbeat(ctx, "")
if time.Now().After(heartBeatTime.Add(1 * time.Minute)) {
log.Debug(ctx, "HeartBeating....", log.KV("tourId", tourId))
heartBeatTime = time.Now()
}
//custom logic
}
}
}
return nil
}
err := s.wfClient.CancelWorkflow(ctx, workflowID, "")
if err != nil {
log.Error(ctx, "Unable to cancel createMatches workflow", err, log.KV("tourId", tournament.Id))
return
}
we are calling CancelWorkflow around time t3Time (parameter to activity) this is also the time when the underline activity has an exit point. ( i suspect this have something to do with the issue)
hope this will help to fix the issue if any.
Thanks and regards,
Junaid