Retry five times getcurrentip before error
This commit is contained in:
parent
32ac90376f
commit
64d066ba4b
1
go.mod
1
go.mod
@ -3,6 +3,7 @@ module gogs.lehouerou.net/Laurent/dnsupdater
|
||||
go 1.12
|
||||
|
||||
require (
|
||||
github.com/matryer/try v0.0.0-20161228173917-9ac251b645a2
|
||||
github.com/sirupsen/logrus v1.4.2
|
||||
github.com/urfave/cli v1.20.0
|
||||
gopkg.in/gomail.v2 v2.0.0-20160411212932-81ebce5c23df
|
||||
|
2
go.sum
2
go.sum
@ -2,6 +2,8 @@ github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c
|
||||
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
|
||||
github.com/konsorten/go-windows-terminal-sequences v1.0.1 h1:mweAR1A6xJ3oS2pRaGiHgQ4OO8tzTaLawm8vnODuwDk=
|
||||
github.com/konsorten/go-windows-terminal-sequences v1.0.1/go.mod h1:T0+1ngSBFLxvqU3pZ+m/2kptfBszLMUkC4ZK/EgS/cQ=
|
||||
github.com/matryer/try v0.0.0-20161228173917-9ac251b645a2 h1:JAEbJn3j/FrhdWA9jW8B5ajsLIjeuEHLi8xE4fk997o=
|
||||
github.com/matryer/try v0.0.0-20161228173917-9ac251b645a2/go.mod h1:0KeJpeMD6o+O4hW7qJOT7vyQPKrWmj26uf5wMc/IiIs=
|
||||
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
|
||||
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
|
||||
github.com/sirupsen/logrus v1.4.2 h1:SPIRibHv4MatM3XXNO2BJeFLZwZ2LvZgfQ5+UNI2im4=
|
||||
|
56
main.go
56
main.go
@ -4,6 +4,7 @@ import (
|
||||
"bytes"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"github.com/matryer/try"
|
||||
log "github.com/sirupsen/logrus"
|
||||
"github.com/urfave/cli"
|
||||
"gopkg.in/gomail.v2"
|
||||
@ -12,6 +13,7 @@ import (
|
||||
"os"
|
||||
"path"
|
||||
"regexp"
|
||||
"time"
|
||||
)
|
||||
|
||||
type Configuration struct {
|
||||
@ -115,7 +117,7 @@ func update() error {
|
||||
continue
|
||||
}
|
||||
|
||||
log.Infoln("%s -> Ip has changed %s -> %s\n", domain, currentIp, newIp)
|
||||
log.Infoln(fmt.Sprintf("%s -> Ip has changed %s -> %s", domain, currentIp, newIp))
|
||||
|
||||
err = SetCurrentIp(newIp, domain, *configuration)
|
||||
if err != nil {
|
||||
@ -144,7 +146,8 @@ func SendStatusMail(messageText string) {
|
||||
d := gomail.NewDialer(settings.SmtpHost, settings.SmtpPort, settings.SmtpLogin, settings.SmtpPassword)
|
||||
s, err := d.Dial()
|
||||
if err != nil {
|
||||
log.Errorln("error while dialing smtp server : %v", err)
|
||||
log.Errorln(fmt.Sprintf("error while dialing smtp server : %v", err))
|
||||
return
|
||||
}
|
||||
|
||||
m := gomail.NewMessage()
|
||||
@ -155,33 +158,48 @@ func SendStatusMail(messageText string) {
|
||||
m.SetBody("text/plain", messageText)
|
||||
|
||||
if err := gomail.Send(s, m); err != nil {
|
||||
log.Warnln("could not send email to %q: %v", r, err)
|
||||
log.Warnln(fmt.Sprintf("could not send email to %q: %v", r, err))
|
||||
}
|
||||
m.Reset()
|
||||
}
|
||||
}
|
||||
|
||||
func GetCurrentIp(configuration Configuration, domain string) (string, error) {
|
||||
url := fmt.Sprintf(GandiARecordUrl, domain)
|
||||
req, err := http.NewRequest("GET", url, nil)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
req.Header.Set("X-Api-Key", configuration.GandiKey)
|
||||
client := &http.Client{}
|
||||
resp, err := client.Do(req)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
defer resp.Body.Close()
|
||||
var value string
|
||||
err := try.Do(func(attempt int) (bool, error) {
|
||||
var err error
|
||||
value, err = func(configuration Configuration, domain string) (string, error) {
|
||||
url := fmt.Sprintf(GandiARecordUrl, domain)
|
||||
req, err := http.NewRequest("GET", url, nil)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
req.Header.Set("X-Api-Key", configuration.GandiKey)
|
||||
client := &http.Client{}
|
||||
resp, err := client.Do(req)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
defer resp.Body.Close()
|
||||
|
||||
decoder := json.NewDecoder(resp.Body)
|
||||
dnsresponse := DnsResponse{}
|
||||
err = decoder.Decode(&dnsresponse)
|
||||
decoder := json.NewDecoder(resp.Body)
|
||||
dnsresponse := DnsResponse{}
|
||||
err = decoder.Decode(&dnsresponse)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
return dnsresponse.Values[0], nil
|
||||
}(configuration, domain)
|
||||
if err != nil {
|
||||
log.Warnln(fmt.Sprintf("Error while getting current ip from gandi : %v", err))
|
||||
time.Sleep(30 * time.Second)
|
||||
}
|
||||
return attempt < 5, err
|
||||
})
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
return dnsresponse.Values[0], nil
|
||||
return value, nil
|
||||
}
|
||||
|
||||
func SetCurrentIp(newip string, domain string, configuration Configuration) error {
|
||||
|
Loading…
Reference in New Issue
Block a user