Skip to content

Commit

Permalink
Add backoff to download function
Browse files Browse the repository at this point in the history
This endpoint has been unreliable
  • Loading branch information
charlieegan3 committed Sep 8, 2021
1 parent b02e7e3 commit 77837ce
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 9 deletions.
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ require (
github.com/andreyvit/diff v0.0.0-20170406064948-c7f18ee00883 // indirect
github.com/antchfx/xmlquery v1.3.6 // indirect
github.com/armon/consul-api v0.0.0-20180202201655-eb2c6b5be1b6 // indirect
github.com/cenkalti/backoff/v4 v4.1.1 // indirect
github.com/charlieegan3/special-days v0.0.0-20210701231319-175bc74510e3 // indirect
github.com/coreos/go-etcd v2.0.0+incompatible // indirect
github.com/cpuguy83/go-md2man v1.0.10 // indirect
Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,8 @@ github.com/beorn7/perks v0.0.0-20180321164747-3a771d992973/go.mod h1:Dwedo/Wpr24
github.com/beorn7/perks v1.0.0/go.mod h1:KWe93zE9D1o94FZ5RNwFwVgaQK1VOXiVxmqh+CedLV8=
github.com/bgentry/speakeasy v0.1.0/go.mod h1:+zsyZBPWlz7T6j88CTgSN5bM796AkVf0kBD4zp0CCIs=
github.com/bketelsen/crypt v0.0.3-0.20200106085610-5cbc8cc4026c/go.mod h1:MKsuJmJgSg28kpZDP6UIiPt0e0Oz0kqKNGyRaWEPv84=
github.com/cenkalti/backoff/v4 v4.1.1 h1:G2HAfAmvm/GcKan2oOQpBXOd2tT2G57ZnZGWa1PxPBQ=
github.com/cenkalti/backoff/v4 v4.1.1/go.mod h1:scbssz8iZGpm3xbr14ovlUdkxfGXNInqkPWOWmG2CLw=
github.com/census-instrumentation/opencensus-proto v0.2.1/go.mod h1:f6KPmirojxKA12rnyqOA5BBL4O983OfeGPqjHWSTneU=
github.com/cespare/xxhash v1.1.0/go.mod h1:XrSqR1VqqWfGrhpAt58auRo0WTKS1nRRg3ghfAqPWnc=
github.com/charlieegan3/special-days v0.0.0-20210701231319-175bc74510e3 h1:8QwBaAB9UJ+Br7K15qN1veIG8RKiguoz99LUQWxwQKw=
Expand Down
34 changes: 25 additions & 9 deletions pkg/airtable/download.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@ package airtable

import (
"fmt"
"time"

"github.com/cenkalti/backoff/v4"
air "github.com/mehanizm/airtable"
)

Expand All @@ -13,17 +15,31 @@ func Download(client *air.Client, databaseID, tableName, viewName string) ([]map

offset := ""
for {
result, err := table.GetRecords().
FromView(viewName).
WithOffset(offset).
ReturnFields("Display Name", "JSON Phone Numbers", "JSON Emails", "Note", "Company", "Profile Image", "Birthday", "JSON Special Days").
Do()
if err != nil {
return records, fmt.Errorf("failed to get records: %s", err)
var result *air.Records
var err error
operation := func() error {
result, err = table.GetRecords().
FromView(viewName).
WithOffset(offset).
ReturnFields("Display Name", "JSON Phone Numbers", "JSON Emails", "Note", "Company", "Profile Image", "Birthday", "JSON Special Days").
Do()
if err != nil {
return fmt.Errorf("failed to get records: %s", err)
}

for _, v := range result.Records {
records = append(records, v.Fields)
}

return nil
}

for _, v := range result.Records {
records = append(records, v.Fields)
b := backoff.NewExponentialBackOff()
b.MaxElapsedTime = 3 * time.Minute

err = backoff.Retry(operation, b)
if err != nil {
return records, fmt.Errorf("failed to get records after backoff: %s", err)
}

// have reached the end
Expand Down

0 comments on commit 77837ce

Please sign in to comment.