Skip to content

Commit ad6fe25

Browse files
authored
Merge pull request #95 from nyaruka/latest_gocommon
Update to latest gocommon
2 parents d2944f8 + ab4c4af commit ad6fe25

File tree

9 files changed

+60
-53
lines changed

9 files changed

+60
-53
lines changed

daemon.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -141,7 +141,7 @@ func (d *Daemon) reportStats(includeLag bool) {
141141
}
142142

143143
func (d *Daemon) calculateLag(ctx context.Context, ix indexers.Indexer) (time.Duration, error) {
144-
esLastModified, err := ix.GetESLastModified(ix.Name())
144+
esLastModified, err := ix.GetESLastModified(ctx, ix.Name())
145145
if err != nil {
146146
return 0, fmt.Errorf("error getting ES last modified: %w", err)
147147
}

go.mod

+8-8
Original file line numberDiff line numberDiff line change
@@ -7,24 +7,24 @@ require (
77
github.com/getsentry/sentry-go v0.31.1
88
github.com/lib/pq v1.10.9
99
github.com/nyaruka/ezconf v0.3.0
10-
github.com/nyaruka/gocommon v1.60.5
10+
github.com/nyaruka/gocommon v1.61.1
1111
github.com/samber/slog-multi v1.4.0
1212
github.com/samber/slog-sentry/v2 v2.9.3
1313
github.com/stretchr/testify v1.10.0
1414
)
1515

1616
require (
1717
github.com/aws/aws-sdk-go-v2 v1.36.3 // indirect
18-
github.com/aws/aws-sdk-go-v2/config v1.29.9 // indirect
19-
github.com/aws/aws-sdk-go-v2/credentials v1.17.62 // indirect
18+
github.com/aws/aws-sdk-go-v2/config v1.29.12 // indirect
19+
github.com/aws/aws-sdk-go-v2/credentials v1.17.65 // indirect
2020
github.com/aws/aws-sdk-go-v2/feature/ec2/imds v1.16.30 // indirect
2121
github.com/aws/aws-sdk-go-v2/internal/configsources v1.3.34 // indirect
2222
github.com/aws/aws-sdk-go-v2/internal/endpoints/v2 v2.6.34 // indirect
2323
github.com/aws/aws-sdk-go-v2/internal/ini v1.8.3 // indirect
2424
github.com/aws/aws-sdk-go-v2/service/internal/accept-encoding v1.12.3 // indirect
2525
github.com/aws/aws-sdk-go-v2/service/internal/presigned-url v1.12.15 // indirect
26-
github.com/aws/aws-sdk-go-v2/service/sso v1.25.1 // indirect
27-
github.com/aws/aws-sdk-go-v2/service/ssooidc v1.29.1 // indirect
26+
github.com/aws/aws-sdk-go-v2/service/sso v1.25.2 // indirect
27+
github.com/aws/aws-sdk-go-v2/service/ssooidc v1.30.0 // indirect
2828
github.com/aws/aws-sdk-go-v2/service/sts v1.33.17 // indirect
2929
github.com/aws/smithy-go v1.22.3 // indirect
3030
github.com/davecgh/go-spew v1.1.1 // indirect
@@ -36,15 +36,15 @@ require (
3636
github.com/naoina/go-stringutil v0.1.0 // indirect
3737
github.com/naoina/toml v0.1.1 // indirect
3838
github.com/nyaruka/null/v2 v2.0.3 // indirect
39-
github.com/nyaruka/phonenumbers v1.5.0 // indirect
39+
github.com/nyaruka/phonenumbers v1.6.0 // indirect
4040
github.com/pmezard/go-difflib v1.0.0 // indirect
4141
github.com/samber/lo v1.49.1 // indirect
4242
github.com/samber/slog-common v0.18.1 // indirect
4343
github.com/shopspring/decimal v1.4.0 // indirect
4444
golang.org/x/exp v0.0.0-20250305212735-054e65f0b394 // indirect
45-
golang.org/x/net v0.37.0 // indirect
45+
golang.org/x/net v0.38.0 // indirect
4646
golang.org/x/sys v0.31.0 // indirect
4747
golang.org/x/text v0.23.0 // indirect
48-
google.golang.org/protobuf v1.36.5 // indirect
48+
google.golang.org/protobuf v1.36.6 // indirect
4949
gopkg.in/yaml.v3 v3.0.1 // indirect
5050
)

go.sum

+16-16
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
github.com/aws/aws-sdk-go-v2 v1.36.3 h1:mJoei2CxPutQVxaATCzDUjcZEjVRdpsiiXi2o38yqWM=
22
github.com/aws/aws-sdk-go-v2 v1.36.3/go.mod h1:LLXuLpgzEbD766Z5ECcRmi8AzSwfZItDtmABVkRLGzg=
3-
github.com/aws/aws-sdk-go-v2/config v1.29.9 h1:Kg+fAYNaJeGXp1vmjtidss8O2uXIsXwaRqsQJKXVr+0=
4-
github.com/aws/aws-sdk-go-v2/config v1.29.9/go.mod h1:oU3jj2O53kgOU4TXq/yipt6ryiooYjlkqqVaZk7gY/U=
5-
github.com/aws/aws-sdk-go-v2/credentials v1.17.62 h1:fvtQY3zFzYJ9CfixuAQ96IxDrBajbBWGqjNTCa79ocU=
6-
github.com/aws/aws-sdk-go-v2/credentials v1.17.62/go.mod h1:ElETBxIQqcxej++Cs8GyPBbgMys5DgQPTwo7cUPDKt8=
3+
github.com/aws/aws-sdk-go-v2/config v1.29.12 h1:Y/2a+jLPrPbHpFkpAAYkVEtJmxORlXoo5k2g1fa2sUo=
4+
github.com/aws/aws-sdk-go-v2/config v1.29.12/go.mod h1:xse1YTjmORlb/6fhkWi8qJh3cvZi4JoVNhc+NbJt4kI=
5+
github.com/aws/aws-sdk-go-v2/credentials v1.17.65 h1:q+nV2yYegofO/SUXruT+pn4KxkxmaQ++1B/QedcKBFM=
6+
github.com/aws/aws-sdk-go-v2/credentials v1.17.65/go.mod h1:4zyjAuGOdikpNYiSGpsGz8hLGmUzlY8pc8r9QQ/RXYQ=
77
github.com/aws/aws-sdk-go-v2/feature/ec2/imds v1.16.30 h1:x793wxmUWVDhshP8WW2mlnXuFrO4cOd3HLBroh1paFw=
88
github.com/aws/aws-sdk-go-v2/feature/ec2/imds v1.16.30/go.mod h1:Jpne2tDnYiFascUEs2AWHJL9Yp7A5ZVy3TNyxaAjD6M=
99
github.com/aws/aws-sdk-go-v2/internal/configsources v1.3.34 h1:ZK5jHhnrioRkUNOc+hOgQKlUL5JeC3S6JgLxtQ+Rm0Q=
@@ -18,10 +18,10 @@ github.com/aws/aws-sdk-go-v2/service/internal/accept-encoding v1.12.3 h1:eAh2A4b
1818
github.com/aws/aws-sdk-go-v2/service/internal/accept-encoding v1.12.3/go.mod h1:0yKJC/kb8sAnmlYa6Zs3QVYqaC8ug2AbnNChv5Ox3uA=
1919
github.com/aws/aws-sdk-go-v2/service/internal/presigned-url v1.12.15 h1:dM9/92u2F1JbDaGooxTq18wmmFzbJRfXfVfy96/1CXM=
2020
github.com/aws/aws-sdk-go-v2/service/internal/presigned-url v1.12.15/go.mod h1:SwFBy2vjtA0vZbjjaFtfN045boopadnoVPhu4Fv66vY=
21-
github.com/aws/aws-sdk-go-v2/service/sso v1.25.1 h1:8JdC7Gr9NROg1Rusk25IcZeTO59zLxsKgE0gkh5O6h0=
22-
github.com/aws/aws-sdk-go-v2/service/sso v1.25.1/go.mod h1:qs4a9T5EMLl/Cajiw2TcbNt2UNo/Hqlyp+GiuG4CFDI=
23-
github.com/aws/aws-sdk-go-v2/service/ssooidc v1.29.1 h1:KwuLovgQPcdjNMfFt9OhUd9a2OwcOKhxfvF4glTzLuA=
24-
github.com/aws/aws-sdk-go-v2/service/ssooidc v1.29.1/go.mod h1:MlYRNmYu/fGPoxBQVvBYr9nyr948aY/WLUvwBMBJubs=
21+
github.com/aws/aws-sdk-go-v2/service/sso v1.25.2 h1:pdgODsAhGo4dvzC3JAG5Ce0PX8kWXrTZGx+jxADD+5E=
22+
github.com/aws/aws-sdk-go-v2/service/sso v1.25.2/go.mod h1:qs4a9T5EMLl/Cajiw2TcbNt2UNo/Hqlyp+GiuG4CFDI=
23+
github.com/aws/aws-sdk-go-v2/service/ssooidc v1.30.0 h1:90uX0veLKcdHVfvxhkWUQSCi5VabtwMLFutYiRke4oo=
24+
github.com/aws/aws-sdk-go-v2/service/ssooidc v1.30.0/go.mod h1:MlYRNmYu/fGPoxBQVvBYr9nyr948aY/WLUvwBMBJubs=
2525
github.com/aws/aws-sdk-go-v2/service/sts v1.33.17 h1:PZV5W8yk4OtH1JAuhV2PXwwO9v5G5Aoj+eMCn4T+1Kc=
2626
github.com/aws/aws-sdk-go-v2/service/sts v1.33.17/go.mod h1:cQnB8CUnxbMU82JvlqjKR2HBOm3fe9pWorWBza6MBJ4=
2727
github.com/aws/smithy-go v1.22.3 h1:Z//5NuZCSW6R4PhQ93hShNbyBbn8BWCmCVCt+Q8Io5k=
@@ -58,12 +58,12 @@ github.com/naoina/toml v0.1.1 h1:PT/lllxVVN0gzzSqSlHEmP8MJB4MY2U7STGxiouV4X8=
5858
github.com/naoina/toml v0.1.1/go.mod h1:NBIhNtsFMo3G2szEBne+bO4gS192HuIYRqfvOWb4i1E=
5959
github.com/nyaruka/ezconf v0.3.0 h1:kGvJqVN8AHowb4HdaHAviJ0Z3yI5Pyekp1WqibFEaGk=
6060
github.com/nyaruka/ezconf v0.3.0/go.mod h1:89GUW6EPRNLIxT7lC4LWnjWTgZeQwRoX7lBmc8ralAU=
61-
github.com/nyaruka/gocommon v1.60.5 h1:V10rosGzVArRspilfbi65TyHBZzjLQbwmaBeicr2Drw=
62-
github.com/nyaruka/gocommon v1.60.5/go.mod h1:kFJuOq8COneV7ssfK6xgCMJ8gP8fQifLQnNXBnE4YL0=
61+
github.com/nyaruka/gocommon v1.61.1 h1:qulMx0jHDWoDagCJbVs7CMLZA33jfa5kYutlXR66pHM=
62+
github.com/nyaruka/gocommon v1.61.1/go.mod h1:HcwpCzwt8XK7SxmGHYRF1R9viOAvlU7VtXTGuEmJx/8=
6363
github.com/nyaruka/null/v2 v2.0.3 h1:rdmMRQyVzrOF3Jff/gpU/7BDR9mQX0lcLl4yImsA3kw=
6464
github.com/nyaruka/null/v2 v2.0.3/go.mod h1:OCVeCkCXwrg5/qE6RU0c1oUVZBy+ZDrT+xYg1XSaIWA=
65-
github.com/nyaruka/phonenumbers v1.5.0 h1:0M+Gd9zl53QC4Nl5z1Yj1O/zPk2XXBUwR/vlzdXSJv4=
66-
github.com/nyaruka/phonenumbers v1.5.0/go.mod h1:gv+CtldaFz+G3vHHnasBSirAi3O2XLqZzVWz4V1pl2E=
65+
github.com/nyaruka/phonenumbers v1.6.0 h1:r9ax45fFg+YLUs2X4bNXm5RAxWl00hYjFgNlv32vtHk=
66+
github.com/nyaruka/phonenumbers v1.6.0/go.mod h1:7gjs+Lchqm49adhAKB5cdcng5ZXgt6x7Jgvi0ZorUtU=
6767
github.com/pingcap/errors v0.11.4 h1:lFuQV/oaUMGcD2tqt+01ROSmJs75VG1ToEOkZIZ4nE4=
6868
github.com/pingcap/errors v0.11.4/go.mod h1:Oi8TUi2kEtXXLMJk9l1cGmz20kV3TaQ0usTwv5KuLY8=
6969
github.com/pkg/errors v0.9.1 h1:FEBLx1zS214owpjy7qsBeixbURkuhQAwrK5UwLGTwt4=
@@ -88,14 +88,14 @@ go.uber.org/goleak v1.3.0 h1:2K3zAYmnTNqV73imy9J1T3WC+gmCePx2hEGkimedGto=
8888
go.uber.org/goleak v1.3.0/go.mod h1:CoHD4mav9JJNrW/WLlf7HGZPjdw8EucARQHekz1X6bE=
8989
golang.org/x/exp v0.0.0-20250305212735-054e65f0b394 h1:nDVHiLt8aIbd/VzvPWN6kSOPE7+F/fNFDSXLVYkE/Iw=
9090
golang.org/x/exp v0.0.0-20250305212735-054e65f0b394/go.mod h1:sIifuuw/Yco/y6yb6+bDNfyeQ/MdPUy/hKEMYQV17cM=
91-
golang.org/x/net v0.37.0 h1:1zLorHbz+LYj7MQlSf1+2tPIIgibq2eL5xkrGk6f+2c=
92-
golang.org/x/net v0.37.0/go.mod h1:ivrbrMbzFq5J41QOQh0siUuly180yBYtLp+CKbEaFx8=
91+
golang.org/x/net v0.38.0 h1:vRMAPTMaeGqVhG5QyLJHqNDwecKTomGeqbnfZyKlBI8=
92+
golang.org/x/net v0.38.0/go.mod h1:ivrbrMbzFq5J41QOQh0siUuly180yBYtLp+CKbEaFx8=
9393
golang.org/x/sys v0.31.0 h1:ioabZlmFYtWhL+TRYpcnNlLwhyxaM9kWTDEmfnprqik=
9494
golang.org/x/sys v0.31.0/go.mod h1:BJP2sWEmIv4KK5OTEluFJCKSidICx8ciO85XgH3Ak8k=
9595
golang.org/x/text v0.23.0 h1:D71I7dUrlY+VX0gQShAThNGHFxZ13dGLBHQLVl1mJlY=
9696
golang.org/x/text v0.23.0/go.mod h1:/BLNzu4aZCJ1+kcD0DNRotWKage4q2rGVAg4o22unh4=
97-
google.golang.org/protobuf v1.36.5 h1:tPhr+woSbjfYvY6/GPufUoYizxw1cF/yFoxJ2fmpwlM=
98-
google.golang.org/protobuf v1.36.5/go.mod h1:9fA7Ob0pmnwhb644+1+CVWFRbNajQ6iRojtC/QF5bRE=
97+
google.golang.org/protobuf v1.36.6 h1:z1NpPI8ku2WgiWnf+t9wTPsn6eP1L7ksHUlkfLvd9xY=
98+
google.golang.org/protobuf v1.36.6/go.mod h1:jduwjTPXsFjZGTmRluh+L6NjiWu7pchiJ2/5YcXBHnY=
9999
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
100100
gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c h1:Hei/4ADfdWqJk1ZMxUNpqntNwaWcugrBjAiHlqqRiVk=
101101
gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c/go.mod h1:JHkPIbrfpd72SG/EVd6muEfDQjcINNoR0C8j2r3qZ4Q=

indexers/base.go

+16-15
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ type Indexer interface {
3434
Index(rt *runtime.Runtime, rebuild, cleanup bool) (string, error)
3535
Stats() Stats
3636

37-
GetESLastModified(index string) (time.Time, error)
37+
GetESLastModified(ctx context.Context, index string) (time.Time, error)
3838
GetDBLastModified(ctx context.Context, db *sql.DB) (time.Time, error)
3939
}
4040

@@ -98,9 +98,9 @@ func (i *baseIndexer) recordActivity(indexed, deleted int, elapsed time.Duration
9898
type infoResponse map[string]interface{}
9999

100100
// FindIndexes finds all our physical indexes
101-
func (i *baseIndexer) FindIndexes() []string {
101+
func (i *baseIndexer) FindIndexes(ctx context.Context) []string {
102102
response := infoResponse{}
103-
_, err := utils.MakeJSONRequest(http.MethodGet, fmt.Sprintf("%s/%s", i.elasticURL, i.name), nil, &response)
103+
_, err := utils.MakeJSONRequest(ctx, http.MethodGet, fmt.Sprintf("%s/%s", i.elasticURL, i.name), nil, &response)
104104
indexes := make([]string, 0)
105105

106106
// error could mean a variety of things, but we'll figure that out later
@@ -128,7 +128,7 @@ func (i *baseIndexer) FindIndexes() []string {
128128
// that index to `contacts`.
129129
//
130130
// If the day-specific name already exists, we append a .1 or .2 to the name.
131-
func (i *baseIndexer) createNewIndex(def *IndexDefinition) (string, error) {
131+
func (i *baseIndexer) createNewIndex(ctx context.Context, def *IndexDefinition) (string, error) {
132132
// create our day-specific name
133133
index := fmt.Sprintf("%s_%s", i.name, time.Now().Format("2006_01_02"))
134134
idx := 0
@@ -152,7 +152,7 @@ func (i *baseIndexer) createNewIndex(def *IndexDefinition) (string, error) {
152152
// create the new index
153153
settings := jsonx.MustMarshal(def)
154154

155-
_, err := utils.MakeJSONRequest(http.MethodPut, fmt.Sprintf("%s/%s", i.elasticURL, index), settings, nil)
155+
_, err := utils.MakeJSONRequest(ctx, http.MethodPut, fmt.Sprintf("%s/%s", i.elasticURL, index), settings, nil)
156156
if err != nil {
157157
return "", err
158158
}
@@ -185,11 +185,11 @@ type removeAliasCommand struct {
185185
}
186186

187187
// maps this indexer's alias to the new physical index, removing existing aliases if they exist
188-
func (i *baseIndexer) updateAlias(newIndex string) error {
188+
func (i *baseIndexer) updateAlias(ctx context.Context, newIndex string) error {
189189
commands := make([]interface{}, 0)
190190

191191
// find existing physical indexes
192-
existing := i.FindIndexes()
192+
existing := i.FindIndexes(ctx)
193193
for _, idx := range existing {
194194
remove := removeAliasCommand{}
195195
remove.Remove.Alias = i.name
@@ -207,7 +207,7 @@ func (i *baseIndexer) updateAlias(newIndex string) error {
207207

208208
aliasJSON := jsonx.MustMarshal(aliasCommand{Actions: commands})
209209

210-
_, err := utils.MakeJSONRequest(http.MethodPost, fmt.Sprintf("%s/_aliases", i.elasticURL), aliasJSON, nil)
210+
_, err := utils.MakeJSONRequest(ctx, http.MethodPost, fmt.Sprintf("%s/_aliases", i.elasticURL), aliasJSON, nil)
211211

212212
i.log().Info("updated alias", "index", newIndex)
213213

@@ -222,9 +222,9 @@ type healthResponse struct {
222222
}
223223

224224
// removes all indexes that are older than the currently active index
225-
func (i *baseIndexer) cleanupIndexes() error {
225+
func (i *baseIndexer) cleanupIndexes(ctx context.Context) error {
226226
// find our current indexes
227-
currents := i.FindIndexes()
227+
currents := i.FindIndexes(ctx)
228228

229229
// no current indexes? this a noop
230230
if len(currents) == 0 {
@@ -233,7 +233,7 @@ func (i *baseIndexer) cleanupIndexes() error {
233233

234234
// find all the current indexes
235235
healthResponse := healthResponse{}
236-
_, err := utils.MakeJSONRequest(http.MethodGet, fmt.Sprintf("%s/%s", i.elasticURL, "_cluster/health?level=indices"), nil, &healthResponse)
236+
_, err := utils.MakeJSONRequest(ctx, http.MethodGet, fmt.Sprintf("%s/%s", i.elasticURL, "_cluster/health?level=indices"), nil, &healthResponse)
237237
if err != nil {
238238
return err
239239
}
@@ -242,7 +242,7 @@ func (i *baseIndexer) cleanupIndexes() error {
242242
for key := range healthResponse.Indices {
243243
if strings.HasPrefix(key, i.name) && strings.Compare(key, currents[0]) < 0 {
244244
slog.Info("removing old index", "index", key)
245-
_, err = utils.MakeJSONRequest(http.MethodDelete, fmt.Sprintf("%s/%s", i.elasticURL, key), nil, nil)
245+
_, err = utils.MakeJSONRequest(ctx, http.MethodDelete, fmt.Sprintf("%s/%s", i.elasticURL, key), nil, nil)
246246
if err != nil {
247247
return err
248248
}
@@ -268,11 +268,11 @@ type indexResponse struct {
268268
}
269269

270270
// indexes the batch of contacts
271-
func (i *baseIndexer) indexBatch(index string, batch []byte) (int, int, int, error) {
271+
func (i *baseIndexer) indexBatch(ctx context.Context, index string, batch []byte) (int, int, int, error) {
272272
response := indexResponse{}
273273
indexURL := fmt.Sprintf("%s/%s/_bulk", i.elasticURL, index)
274274

275-
_, err := utils.MakeJSONRequest(http.MethodPut, indexURL, batch, &response)
275+
_, err := utils.MakeJSONRequest(ctx, http.MethodPut, indexURL, batch, &response)
276276
if err != nil {
277277
return 0, 0, 0, err
278278
}
@@ -324,12 +324,13 @@ type queryResponse struct {
324324
}
325325

326326
// GetESLastModified queries a concrete index and finds the last modified document, returning its modified time
327-
func (i *baseIndexer) GetESLastModified(index string) (time.Time, error) {
327+
func (i *baseIndexer) GetESLastModified(ctx context.Context, index string) (time.Time, error) {
328328
lastModified := time.Time{}
329329

330330
// get the newest document on our index
331331
queryResponse := &queryResponse{}
332332
_, err := utils.MakeJSONRequest(
333+
ctx,
333334
http.MethodPost,
334335
fmt.Sprintf("%s/%s/_search", i.elasticURL, index),
335336
[]byte(`{ "sort": [{ "modified_on_mu": "desc" }], "_source": {"includes": ["modified_on", "id"]}, "size": 1, "track_total_hits": false}`),

indexers/base_test.go

+2-1
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package indexers_test
22

33
import (
44
"bytes"
5+
"context"
56
"database/sql"
67
"io"
78
"log/slog"
@@ -90,7 +91,7 @@ func elasticRequest(t *testing.T, cfg *runtime.Config, method, path string, data
9091
if data != nil {
9192
body = bytes.NewReader(jsonx.MustMarshal(data))
9293
}
93-
req, err := httpx.NewRequest(method, cfg.ElasticURL+path, body, map[string]string{"Content-Type": "application/json"})
94+
req, err := httpx.NewRequest(context.Background(), method, cfg.ElasticURL+path, body, map[string]string{"Content-Type": "application/json"})
9495
require.NoError(t, err)
9596

9697
trace, err := httpx.DoTrace(http.DefaultClient, req, nil, nil, -1)

indexers/contacts.go

+6-6
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ func (i *ContactIndexer) Index(rt *runtime.Runtime, rebuild, cleanup bool) (stri
3737
var err error
3838

3939
// find our physical index
40-
physicalIndexes := i.FindIndexes()
40+
physicalIndexes := i.FindIndexes(ctx)
4141

4242
physicalIndex := ""
4343
if len(physicalIndexes) > 0 {
@@ -49,15 +49,15 @@ func (i *ContactIndexer) Index(rt *runtime.Runtime, rebuild, cleanup bool) (stri
4949

5050
// doesn't exist or we are rebuilding, create it
5151
if physicalIndex == "" || rebuild {
52-
physicalIndex, err = i.createNewIndex(i.definition)
52+
physicalIndex, err = i.createNewIndex(ctx, i.definition)
5353
if err != nil {
5454
return "", fmt.Errorf("error creating new index: %w", err)
5555
}
5656
i.log().Info("created new physical index", "index", physicalIndex)
5757
remapAlias = true
5858
}
5959

60-
lastModified, err := i.GetESLastModified(physicalIndex)
60+
lastModified, err := i.GetESLastModified(ctx, physicalIndex)
6161
if err != nil {
6262
return "", fmt.Errorf("error finding last modified: %w", err)
6363
}
@@ -72,7 +72,7 @@ func (i *ContactIndexer) Index(rt *runtime.Runtime, rebuild, cleanup bool) (stri
7272

7373
// if the index didn't previously exist or we are rebuilding, remap to our alias
7474
if remapAlias {
75-
err := i.updateAlias(physicalIndex)
75+
err := i.updateAlias(ctx, physicalIndex)
7676
if err != nil {
7777
return "", fmt.Errorf("error updating alias: %w", err)
7878
}
@@ -81,7 +81,7 @@ func (i *ContactIndexer) Index(rt *runtime.Runtime, rebuild, cleanup bool) (stri
8181

8282
// cleanup our aliases if appropriate
8383
if cleanup {
84-
err := i.cleanupIndexes()
84+
err := i.cleanupIndexes(ctx)
8585
if err != nil {
8686
return "", fmt.Errorf("error cleaning up old indexes: %w", err)
8787
}
@@ -176,7 +176,7 @@ func (i *ContactIndexer) indexModified(ctx context.Context, db *sql.DB, index st
176176

177177
indexSubBatch := func(b *bytes.Buffer) error {
178178
t := time.Now()
179-
created, updated, deleted, err := i.indexBatch(index, b.Bytes())
179+
created, updated, deleted, err := i.indexBatch(ctx, index, b.Bytes())
180180
if err != nil {
181181
return err
182182
}

indexers/contacts_test.go

+4-3
Original file line numberDiff line numberDiff line change
@@ -165,6 +165,7 @@ var contactQueryTests = []struct {
165165
}
166166

167167
func TestContacts(t *testing.T) {
168+
ctx := context.Background()
168169
rt := setup(t)
169170

170171
ix1 := indexers.NewContactIndexer(rt.Config.ElasticURL, rt.Config.ContactsIndex, 2, 1, 4)
@@ -175,7 +176,7 @@ func TestContacts(t *testing.T) {
175176
assert.WithinDuration(t, time.Date(2017, 11, 10, 21, 11, 59, 890662000, time.UTC), dbModified, 0)
176177

177178
// error trying to get ES last modified on before index exists
178-
_, err = ix1.GetESLastModified(rt.Config.ContactsIndex)
179+
_, err = ix1.GetESLastModified(ctx, rt.Config.ContactsIndex)
179180
assert.Error(t, err)
180181

181182
expectedIndexName := fmt.Sprintf("indexer_test_%s", time.Now().Format("2006_01_02"))
@@ -186,7 +187,7 @@ func TestContacts(t *testing.T) {
186187

187188
time.Sleep(1 * time.Second)
188189

189-
esModified, err := ix1.GetESLastModified(rt.Config.ContactsIndex)
190+
esModified, err := ix1.GetESLastModified(ctx, rt.Config.ContactsIndex)
190191
assert.NoError(t, err)
191192
assert.WithinDuration(t, time.Date(2017, 11, 10, 21, 11, 59, 890662000, time.UTC), esModified, 0)
192193

@@ -197,7 +198,7 @@ func TestContacts(t *testing.T) {
197198
assertQuery(t, rt.Config, tc.query, tc.expected, "query mismatch for %s", tc.query)
198199
}
199200

200-
lastModified, err := ix1.GetESLastModified(indexName)
201+
lastModified, err := ix1.GetESLastModified(ctx, indexName)
201202
assert.NoError(t, err)
202203
assert.Equal(t, time.Date(2017, 11, 10, 21, 11, 59, 890662000, time.UTC), lastModified.In(time.UTC))
203204

utils/http.go

+3-2
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package utils
22

33
import (
44
"bytes"
5+
"context"
56
"encoding/json"
67
"fmt"
78
"io"
@@ -50,10 +51,10 @@ func shouldRetry(request *http.Request, response *http.Response, withDelay time.
5051
}
5152

5253
// MakeJSONRequest is a utility function to make a JSON request, optionally decoding the response into the passed in struct
53-
func MakeJSONRequest(method string, url string, body []byte, dest any) (*http.Response, error) {
54+
func MakeJSONRequest(ctx context.Context, method string, url string, body []byte, dest any) (*http.Response, error) {
5455
l := slog.With("url", url, "method", method)
5556

56-
req, _ := httpx.NewRequest(method, url, bytes.NewReader(body), map[string]string{"Content-Type": "application/json"})
57+
req, _ := httpx.NewRequest(ctx, method, url, bytes.NewReader(body), map[string]string{"Content-Type": "application/json"})
5758
resp, err := httpx.Do(http.DefaultClient, req, retryConfig, nil)
5859
if err != nil {
5960
l.Error("error making request", "error", err)

utils/http_test.go

+4-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package utils_test
22

33
import (
4+
"context"
45
"net/http"
56
"net/http/httptest"
67
"testing"
@@ -11,6 +12,8 @@ import (
1112
)
1213

1314
func TestRetryServer(t *testing.T) {
15+
ctx := context.Background()
16+
1417
responseCounter := 0
1518
responses := []func(w http.ResponseWriter, r *http.Request){
1619
func(w http.ResponseWriter, r *http.Request) {
@@ -32,7 +35,7 @@ func TestRetryServer(t *testing.T) {
3235
}))
3336
defer ts.Close()
3437

35-
resp, err := utils.MakeJSONRequest("GET", ts.URL, nil, nil)
38+
resp, err := utils.MakeJSONRequest(ctx, "GET", ts.URL, nil, nil)
3639
assert.NoError(t, err)
3740
assert.Equal(t, 200, resp.StatusCode)
3841

0 commit comments

Comments
 (0)