Skip to content

Commit

Permalink
Refactoring
Browse files Browse the repository at this point in the history
  • Loading branch information
derfenix committed Apr 14, 2023
1 parent 571c6ce commit e0c91df
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 4 deletions.
12 changes: 8 additions & 4 deletions entity/page.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ type Page struct {
Description string
Created time.Time
Formats []Format
Results Results
Results ResultsRO
Version uint16
Status Status
Meta Meta
Expand All @@ -83,25 +83,27 @@ func (p *Page) Process(ctx context.Context, processor Processor) {
p.Meta = meta
}

results := Results{}

for _, format := range p.Formats {
go func(format Format) {
defer innerWG.Done()

defer func() {
if err := recover(); err != nil {
p.Results.Add(Result{Format: format, Err: fmt.Errorf("recovered from panic: %v", err)})
results.Add(Result{Format: format, Err: fmt.Errorf("recovered from panic: %v", err)})
}
}()

result := processor.Process(ctx, format, p.URL)
p.Results.Add(result)
results.Add(result)
}(format)
}

innerWG.Wait()

var hasResultWithOutErrors bool
for _, result := range p.Results.Results() {
for _, result := range results.Results() {
if result.Err != nil {
p.Status = StatusWithErrors
} else {
Expand All @@ -116,4 +118,6 @@ func (p *Page) Process(ctx context.Context, processor Processor) {
if p.Status == StatusProcessing {
p.Status = StatusDone
}

p.Results = results.RO()
}
9 changes: 9 additions & 0 deletions entity/result.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ import (
"github.com/vmihailenco/msgpack/v5"
)

type ResultsRO []Result

type Result struct {
Format Format
Err error
Expand All @@ -17,6 +19,13 @@ type Results struct {
results []Result
}

func (r *Results) RO() ResultsRO {
r.mu.Lock()
defer r.mu.Unlock()

return r.results
}

func (r *Results) MarshalMsgpack() ([]byte, error) {
return msgpack.Marshal(r.results)
}
Expand Down

0 comments on commit e0c91df

Please sign in to comment.