Skip to content

Commit 748a5bb

Browse files
authored
Merge pull request #5 from shiranr/beta_change_url_handling
change the way we handle retry
2 parents 9d17745 + b66e445 commit 748a5bb

File tree

2 files changed

+14
-16
lines changed

2 files changed

+14
-16
lines changed

mega-linter-plugin-linkcheck/linkcheck.megalinter-descriptor.yml

+1-1
Original file line numberDiff line numberDiff line change
@@ -26,4 +26,4 @@ linters:
2626
- "linkcheck --config linkcheck.json"
2727
install:
2828
dockerfile:
29-
- RUN export GO111MODULE=on && go install github.com/shiranr/linkcheck@v1.1.4 && go clean --cache
29+
- RUN export GO111MODULE=on && go install github.com/shiranr/linkcheck@v2.0.1.beta && go clean --cache

models/url_handler.go

+13-15
Original file line numberDiff line numberDiff line change
@@ -23,16 +23,9 @@ func GetURLHandlerInstance() LinkHandlerInterface {
2323

2424
// Handle - using scrap lib, check the link status
2525
func (handler *urlHandler) Handle(linkPath string) int {
26-
respStatus, err := handler.scrap(linkPath)
27-
for i := 0; i < 2 && err != nil; i++ {
26+
respStatus, err := handler.scrap(linkPath, true)
27+
if err != nil {
2828
errLower := strings.ToLower(err.Error())
29-
if strings.Contains(errLower, "eof") || strings.Contains(errLower, "timeout") {
30-
respStatus, err = handler.scrap(linkPath)
31-
if err == nil {
32-
return respStatus
33-
}
34-
errLower = strings.ToLower(err.Error())
35-
}
3629
if strings.Contains(errLower, "not found") {
3730
return 404
3831
}
@@ -42,19 +35,24 @@ func (handler *urlHandler) Handle(linkPath string) int {
4235
if strings.Contains(errLower, "timeout") {
4336
return 504
4437
}
45-
log.WithFields(log.Fields{
46-
"link": linkPath,
47-
"error": err,
48-
}).Error("Failed get URL data")
4938
}
5039
return respStatus
5140
}
5241

53-
func (handler *urlHandler) scrap(linkPath string) (int, error) {
42+
func (handler *urlHandler) scrap(linkPath string, retry bool) (int, error) {
43+
var err error
5444
c := colly.NewCollector()
5545
respStatus := 0
5646
c.OnResponse(func(resp *colly.Response) {
5747
respStatus = resp.StatusCode
5848
})
59-
return respStatus, c.Visit(linkPath)
49+
err = c.Visit(linkPath)
50+
if retry && (err != nil || respStatus == 0) {
51+
log.WithFields(log.Fields{
52+
"link": linkPath,
53+
"error": err,
54+
}).Error("Failed get URL data, retrying")
55+
err = c.Visit(linkPath)
56+
}
57+
return respStatus, err
6058
}

0 commit comments

Comments
 (0)