mirror of
https://github.com/derfenix/webarchive.git
synced 2026-03-11 12:41:54 +03:00
Refactoring
This commit is contained in:
@@ -62,7 +62,7 @@ type Page struct {
|
||||
Description string
|
||||
Created time.Time
|
||||
Formats []Format
|
||||
Results Results
|
||||
Results ResultsRO
|
||||
Version uint16
|
||||
Status Status
|
||||
Meta Meta
|
||||
@@ -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 {
|
||||
@@ -116,4 +118,6 @@ func (p *Page) Process(ctx context.Context, processor Processor) {
|
||||
if p.Status == StatusProcessing {
|
||||
p.Status = StatusDone
|
||||
}
|
||||
|
||||
p.Results = results.RO()
|
||||
}
|
||||
|
||||
@@ -6,6 +6,8 @@ import (
|
||||
"github.com/vmihailenco/msgpack/v5"
|
||||
)
|
||||
|
||||
type ResultsRO []Result
|
||||
|
||||
type Result struct {
|
||||
Format Format
|
||||
Err error
|
||||
@@ -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)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user