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
|
Description string
|
||||||
Created time.Time
|
Created time.Time
|
||||||
Formats []Format
|
Formats []Format
|
||||||
Results Results
|
Results ResultsRO
|
||||||
Version uint16
|
Version uint16
|
||||||
Status Status
|
Status Status
|
||||||
Meta Meta
|
Meta Meta
|
||||||
@@ -83,25 +83,27 @@ func (p *Page) Process(ctx context.Context, processor Processor) {
|
|||||||
p.Meta = meta
|
p.Meta = meta
|
||||||
}
|
}
|
||||||
|
|
||||||
|
results := Results{}
|
||||||
|
|
||||||
for _, format := range p.Formats {
|
for _, format := range p.Formats {
|
||||||
go func(format Format) {
|
go func(format Format) {
|
||||||
defer innerWG.Done()
|
defer innerWG.Done()
|
||||||
|
|
||||||
defer func() {
|
defer func() {
|
||||||
if err := recover(); err != nil {
|
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)
|
result := processor.Process(ctx, format, p.URL)
|
||||||
p.Results.Add(result)
|
results.Add(result)
|
||||||
}(format)
|
}(format)
|
||||||
}
|
}
|
||||||
|
|
||||||
innerWG.Wait()
|
innerWG.Wait()
|
||||||
|
|
||||||
var hasResultWithOutErrors bool
|
var hasResultWithOutErrors bool
|
||||||
for _, result := range p.Results.Results() {
|
for _, result := range results.Results() {
|
||||||
if result.Err != nil {
|
if result.Err != nil {
|
||||||
p.Status = StatusWithErrors
|
p.Status = StatusWithErrors
|
||||||
} else {
|
} else {
|
||||||
@@ -116,4 +118,6 @@ func (p *Page) Process(ctx context.Context, processor Processor) {
|
|||||||
if p.Status == StatusProcessing {
|
if p.Status == StatusProcessing {
|
||||||
p.Status = StatusDone
|
p.Status = StatusDone
|
||||||
}
|
}
|
||||||
|
|
||||||
|
p.Results = results.RO()
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -6,6 +6,8 @@ import (
|
|||||||
"github.com/vmihailenco/msgpack/v5"
|
"github.com/vmihailenco/msgpack/v5"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
type ResultsRO []Result
|
||||||
|
|
||||||
type Result struct {
|
type Result struct {
|
||||||
Format Format
|
Format Format
|
||||||
Err error
|
Err error
|
||||||
@@ -17,6 +19,13 @@ type Results struct {
|
|||||||
results []Result
|
results []Result
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (r *Results) RO() ResultsRO {
|
||||||
|
r.mu.Lock()
|
||||||
|
defer r.mu.Unlock()
|
||||||
|
|
||||||
|
return r.results
|
||||||
|
}
|
||||||
|
|
||||||
func (r *Results) MarshalMsgpack() ([]byte, error) {
|
func (r *Results) MarshalMsgpack() ([]byte, error) {
|
||||||
return msgpack.Marshal(r.results)
|
return msgpack.Marshal(r.results)
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user