Skip to content

Commit 9b843d4

Browse files
committed
circuit breaker
1 parent e805ef8 commit 9b843d4

1 file changed

Lines changed: 16 additions & 1 deletion

File tree

internal/service/broadcast/orchestrator.go

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -496,6 +496,9 @@ func (o *BroadcastOrchestrator) Process(ctx context.Context, task *domain.Task,
496496
lastSaveTime := o.timeProvider.Now()
497497
lastLogTime := o.timeProvider.Now()
498498

499+
// Track if circuit breaker was triggered during this processing cycle
500+
circuitBreakerTriggered := false
501+
499502
// Phase 1: Get recipient count if not already set
500503
if broadcastState.TotalRecipients == 0 {
501504
count, countErr := o.GetTotalRecipientCount(ctx, task.WorkspaceID, broadcastState.BroadcastID)
@@ -896,7 +899,7 @@ func (o *BroadcastOrchestrator) Process(ctx context.Context, task *domain.Task,
896899
if sendErr != nil {
897900
// Check if this is a circuit breaker error
898901
if broadcastErr, ok := sendErr.(*BroadcastError); ok && broadcastErr.Code == ErrCodeCircuitOpen {
899-
// Circuit breaker is open - pause the broadcast
902+
// Circuit breaker is open - pause the broadcast immediately
900903
o.logger.WithFields(map[string]interface{}{
901904
"task_id": task.ID,
902905
"broadcast_id": broadcastState.BroadcastID,
@@ -978,6 +981,9 @@ func (o *BroadcastOrchestrator) Process(ctx context.Context, task *domain.Task,
978981
}
979982
}
980983

984+
// Mark that circuit breaker was triggered
985+
circuitBreakerTriggered = true
986+
981987
// Return the circuit breaker error to stop processing
982988
err = sendErr
983989
return false, err
@@ -1068,6 +1074,15 @@ func (o *BroadcastOrchestrator) Process(ctx context.Context, task *domain.Task,
10681074

10691075
// If the task is complete, update the broadcast status appropriately
10701076
if allDone {
1077+
// Don't mark as sent if circuit breaker was triggered during processing
1078+
if circuitBreakerTriggered {
1079+
o.logger.WithFields(map[string]interface{}{
1080+
"task_id": task.ID,
1081+
"broadcast_id": broadcastState.BroadcastID,
1082+
}).Warn("Circuit breaker was triggered - not marking broadcast as sent despite completion")
1083+
return false, fmt.Errorf("circuit breaker triggered during processing")
1084+
}
1085+
10711086
var statusMessage string
10721087

10731088
switch broadcastState.Phase {

0 commit comments

Comments
 (0)