Skip to content

Commit

Permalink
add waitgroup to reporting calls to ensure consistency
Browse files Browse the repository at this point in the history
  • Loading branch information
lucamrgs committed Sep 27, 2024
1 parent c5e89c9 commit aa60f54
Showing 1 changed file with 13 additions and 2 deletions.
15 changes: 13 additions & 2 deletions internal/reporter/reporter.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"fmt"
"reflect"
"strconv"
"sync"

downstreamReporter "soarca/internal/reporter/downstream_reporter"
"soarca/logger"
Expand Down Expand Up @@ -39,8 +40,9 @@ const MaxReporters int = 10

// High-level reporter class with injection of specific reporters
type Reporter struct {
reporters []downstreamReporter.IDownStreamReporter
maxReporters int
reporters []downstreamReporter.IDownStreamReporter
maxReporters int
reportingWaitGroup sync.WaitGroup
}

func New(reporters []downstreamReporter.IDownStreamReporter) *Reporter {
Expand All @@ -64,6 +66,7 @@ func (reporter *Reporter) RegisterReporters(reporters []downstreamReporter.IDown
// ######################## IWorkflowReporter interface

func (reporter *Reporter) reportWorkflowStart(executionId uuid.UUID, playbook cacao.Playbook) {
defer reporter.reportingWaitGroup.Done()
for _, rep := range reporter.reporters {
err := rep.ReportWorkflowStart(executionId, playbook)
if err != nil {
Expand All @@ -72,11 +75,13 @@ func (reporter *Reporter) reportWorkflowStart(executionId uuid.UUID, playbook ca
}
}
func (reporter *Reporter) ReportWorkflowStart(executionId uuid.UUID, playbook cacao.Playbook) {
reporter.reportingWaitGroup.Add(1)
log.Trace(fmt.Sprintf("[execution: %s, playbook: %s] reporting workflow start", executionId, playbook.ID))
go reporter.reportWorkflowStart(executionId, playbook)
}

func (reporter *Reporter) reportWorkflowEnd(executionId uuid.UUID, playbook cacao.Playbook, workflowError error) {
defer reporter.reportingWaitGroup.Done()
for _, rep := range reporter.reporters {
err := rep.ReportWorkflowEnd(executionId, playbook, workflowError)
if err != nil {
Expand All @@ -85,13 +90,16 @@ func (reporter *Reporter) reportWorkflowEnd(executionId uuid.UUID, playbook caca
}
}
func (reporter *Reporter) ReportWorkflowEnd(executionId uuid.UUID, playbook cacao.Playbook, workflowError error) {
reporter.reportingWaitGroup.Wait()
reporter.reportingWaitGroup.Add(1)
log.Trace(fmt.Sprintf("[execution: %s, playbook: %s] reporting workflow end", executionId, playbook.ID))
go reporter.reportWorkflowEnd(executionId, playbook, workflowError)
}

// ######################## IStepReporter interface

func (reporter *Reporter) reporStepStart(executionId uuid.UUID, step cacao.Step, returnVars cacao.Variables) {
defer reporter.reportingWaitGroup.Done()
for _, rep := range reporter.reporters {
err := rep.ReportStepStart(executionId, step, returnVars)
if err != nil {
Expand All @@ -100,11 +108,13 @@ func (reporter *Reporter) reporStepStart(executionId uuid.UUID, step cacao.Step,
}
}
func (reporter *Reporter) ReportStepStart(executionId uuid.UUID, step cacao.Step, returnVars cacao.Variables) {
reporter.reportingWaitGroup.Add(1)
log.Trace(fmt.Sprintf("[execution: %s, step: %s] reporting step start", executionId, step.ID))
go reporter.reporStepStart(executionId, step, returnVars)
}

func (reporter *Reporter) reportStepEnd(executionId uuid.UUID, step cacao.Step, returnVars cacao.Variables, stepError error) {
defer reporter.reportingWaitGroup.Done()
for _, rep := range reporter.reporters {
err := rep.ReportStepEnd(executionId, step, returnVars, stepError)
if err != nil {
Expand All @@ -113,6 +123,7 @@ func (reporter *Reporter) reportStepEnd(executionId uuid.UUID, step cacao.Step,
}
}
func (reporter *Reporter) ReportStepEnd(executionId uuid.UUID, step cacao.Step, returnVars cacao.Variables, stepError error) {
reporter.reportingWaitGroup.Add(1)
log.Trace(fmt.Sprintf("[execution: %s, step: %s] reporting step end", executionId, step.ID))
go reporter.reportStepEnd(executionId, step, returnVars, stepError)
}

0 comments on commit aa60f54

Please sign in to comment.