From c3ab89d10e6475ae833fb4906c4594c7e1ed901a Mon Sep 17 00:00:00 2001 From: Ian Rees Date: Tue, 14 Jan 2025 14:07:57 -0800 Subject: [PATCH] Yet more log ctx updates (#296) * Yet more log ctx updates --- cmd/tlserver/main.go | 8 +++----- finders/actions/feed_fetch.go | 4 ++-- finders/actions/fv.go | 8 ++++---- finders/dbfinder/entity_mutations.go | 6 +++--- finders/dbfinder/finder.go | 8 ++++---- finders/rtfinder/finder.go | 4 ++-- go.mod | 8 ++++---- go.sum | 15 ++++++++------- internal/gbfs/fetch.go | 5 +++-- internal/testconfig/testconfig.go | 9 ++++----- model/finders.go | 5 ++--- server/gql/entity_mutation_resolver_test.go | 4 ++-- 12 files changed, 41 insertions(+), 43 deletions(-) diff --git a/cmd/tlserver/main.go b/cmd/tlserver/main.go index 8a114f05..f852ff6c 100644 --- a/cmd/tlserver/main.go +++ b/cmd/tlserver/main.go @@ -32,7 +32,6 @@ import ( "github.com/interline-io/transitland-server/server/gql" "github.com/interline-io/transitland-server/server/playground" "github.com/interline-io/transitland-server/server/rest" - "github.com/jmoiron/sqlx" "github.com/rs/zerolog" "github.com/spf13/cobra" "github.com/spf13/pflag" @@ -130,10 +129,9 @@ func (cmd *ServerCommand) Parse(args []string) error { return nil } -func (cmd *ServerCommand) Run() error { - ctx := context.Background() +func (cmd *ServerCommand) Run(ctx context.Context) error { // Open database - var db sqlx.Ext + var db tldb.Ext dbx, err := dbutil.OpenDB(cmd.DBURL) if err != nil { return err @@ -301,7 +299,7 @@ func (cmd *versionCommand) Parse(args []string) error { return nil } -func (cmd *versionCommand) Run() error { +func (cmd *versionCommand) Run(context.Context) error { vi := getVersion() log.Print("transitland-server version: %s", vi.Tag) log.Print("transitland-server commit: https://github.com/interline-io/transitland-server/commit/%s (time: %s)", vi.Commit, vi.CommitTime) diff --git a/finders/actions/feed_fetch.go b/finders/actions/feed_fetch.go index 17dd2acb..1c73319f 100644 --- a/finders/actions/feed_fetch.go +++ b/finders/actions/feed_fetch.go @@ -65,7 +65,7 @@ func StaticFetch(ctx context.Context, feedId string, feedSrc io.Reader, feedUrl mr := model.FeedVersionFetchResult{} db := tldb.NewPostgresAdapterFromDBX(dbf.DBX()) if err := db.Tx(func(atx tldb.Adapter) error { - fr, err := fetch.StaticFetch(atx, fetchOpts) + fr, err := fetch.StaticFetch(ctx, atx, fetchOpts) if err != nil { return err } @@ -109,7 +109,7 @@ func RTFetch(ctx context.Context, target string, feedId string, feedUrl string, var rtMsg *pb.FeedMessage var fetchErr error if err := tldb.NewPostgresAdapterFromDBX(cfg.Finder.DBX()).Tx(func(atx tldb.Adapter) error { - fr, err := fetch.RTFetch(atx, fetchOpts) + fr, err := fetch.RTFetch(ctx, atx, fetchOpts) if err != nil { return err } diff --git a/finders/actions/fv.go b/finders/actions/fv.go index 749a3674..8b544ac6 100644 --- a/finders/actions/fv.go +++ b/finders/actions/fv.go @@ -37,7 +37,7 @@ func FeedVersionImport(ctx context.Context, fvid int) (*model.FeedVersionImportR }, } db := tldb.NewPostgresAdapterFromDBX(cfg.Finder.DBX()) - fr, fe := importer.MainImportFeedVersion(db, opts) + fr, fe := importer.MainImportFeedVersion(ctx, db, opts) if fe != nil { return nil, fe } @@ -54,7 +54,7 @@ func FeedVersionUnimport(ctx context.Context, fvid int) (*model.FeedVersionUnimp } db := tldb.NewPostgresAdapterFromDBX(cfg.Finder.DBX()) if err := db.Tx(func(atx tldb.Adapter) error { - return importer.UnimportFeedVersion(atx, fvid, nil) + return importer.UnimportFeedVersion(ctx, atx, fvid, nil) }); err != nil { return nil, err } @@ -91,7 +91,7 @@ func FeedVersionUpdate(ctx context.Context, values model.FeedVersionSetInput) (i } else { fv.Description.Valid = false } - return atx.Update(&fv, cols...) + return atx.Update(ctx, &fv, cols...) }) if err != nil { return 0, err @@ -183,7 +183,7 @@ func ValidateUpload(ctx context.Context, src io.Reader, feedURL *string, rturls result.FailureReason = strptr("Could not validate file") return &result, nil } - r, err := vt.Validate() + r, err := vt.Validate(ctx) if err != nil { result.FailureReason = strptr("Could not validate file") return &result, nil diff --git a/finders/dbfinder/entity_mutations.go b/finders/dbfinder/entity_mutations.go index 7db6e435..e7efd552 100644 --- a/finders/dbfinder/entity_mutations.go +++ b/finders/dbfinder/entity_mutations.go @@ -222,7 +222,7 @@ func createUpdateEnt[T hasTableName]( // Update or create? if update { baseEnt.SetID(*entId) - if err := atx.Find(baseEnt); err != nil { + if err := atx.Find(ctx, baseEnt); err != nil { return 0, err } } else if fvid != nil { @@ -250,9 +250,9 @@ func createUpdateEnt[T hasTableName]( err = nil if update { retId = baseEnt.GetID() - err = atx.Update(baseEnt, cols...) + err = atx.Update(ctx, baseEnt, cols...) } else { - retId, err = atx.Insert(baseEnt) + retId, err = atx.Insert(ctx, baseEnt) } if err != nil { return 0, err diff --git a/finders/dbfinder/finder.go b/finders/dbfinder/finder.go index b517a569..e59316d9 100644 --- a/finders/dbfinder/finder.go +++ b/finders/dbfinder/finder.go @@ -9,23 +9,23 @@ import ( sq "github.com/Masterminds/squirrel" "github.com/interline-io/log" "github.com/interline-io/transitland-dbutil/dbutil" + "github.com/interline-io/transitland-lib/tldb" "github.com/interline-io/transitland-lib/tlxy" "github.com/interline-io/transitland-lib/tt" "github.com/interline-io/transitland-server/internal/clock" "github.com/interline-io/transitland-server/model" - "github.com/jmoiron/sqlx" ) //////// type Finder struct { Clock clock.Clock - db sqlx.Ext + db tldb.Ext adminCache *adminCache fvslCache *clock.ServiceWindowCache } -func NewFinder(db sqlx.Ext) *Finder { +func NewFinder(db tldb.Ext) *Finder { finder := &Finder{ db: db, fvslCache: clock.NewServiceWindowCache(db), @@ -33,7 +33,7 @@ func NewFinder(db sqlx.Ext) *Finder { return finder } -func (f *Finder) DBX() sqlx.Ext { +func (f *Finder) DBX() tldb.Ext { return f.db } diff --git a/finders/rtfinder/finder.go b/finders/rtfinder/finder.go index 832d28da..0d6ecd7c 100644 --- a/finders/rtfinder/finder.go +++ b/finders/rtfinder/finder.go @@ -9,9 +9,9 @@ import ( "github.com/interline-io/log" "github.com/interline-io/transitland-lib/rt/pb" + "github.com/interline-io/transitland-lib/tldb" "github.com/interline-io/transitland-server/internal/clock" "github.com/interline-io/transitland-server/model" - "github.com/jmoiron/sqlx" ) // Cache provides a method for looking up and listening for changed RT data @@ -30,7 +30,7 @@ type Finder struct { lc *lookupCache } -func NewFinder(cache Cache, db sqlx.Ext) *Finder { +func NewFinder(cache Cache, db tldb.Ext) *Finder { return &Finder{ Clock: &clock.Real{}, cache: cache, diff --git a/go.mod b/go.mod index d8c33adf..92480d8d 100644 --- a/go.mod +++ b/go.mod @@ -20,8 +20,8 @@ require ( github.com/interline-io/log v0.0.0-20241212203449-4bcff214cd71 github.com/interline-io/transitland-dbutil v0.0.0-20241212203507-15a69a52c1c4 github.com/interline-io/transitland-jobs v0.0.0-20241212203627-25dc585dcf7e - github.com/interline-io/transitland-lib v1.0.0-rc3.0.20241221023216-8a59b5c6d534 - github.com/interline-io/transitland-mw v0.0.0-20241212203557-2e0c0fea3d26 + github.com/interline-io/transitland-lib v1.0.0-rc3.0.20250114204131-f367913b54e8 + github.com/interline-io/transitland-mw v0.0.0-20250110005713-3ae756e06fec github.com/jmoiron/sqlx v1.4.0 github.com/rs/zerolog v1.33.0 github.com/spf13/cobra v1.8.1 @@ -91,7 +91,7 @@ require ( github.com/lann/builder v0.0.0-20180802200727-47ae307949d0 // indirect github.com/lann/ps v0.0.0-20150810152359-62de8c46ede0 // indirect github.com/mailru/easyjson v0.7.7 // indirect - github.com/mattn/go-colorable v0.1.13 // indirect + github.com/mattn/go-colorable v0.1.14 // indirect github.com/mattn/go-isatty v0.0.20 // indirect github.com/mattn/go-sqlite3 v1.14.22 // indirect github.com/mitchellh/mapstructure v1.5.0 // indirect @@ -119,7 +119,7 @@ require ( golang.org/x/net v0.29.0 // indirect golang.org/x/oauth2 v0.15.0 // indirect golang.org/x/sync v0.10.0 // indirect - golang.org/x/sys v0.28.0 // indirect + golang.org/x/sys v0.29.0 // indirect golang.org/x/text v0.21.0 // indirect golang.org/x/tools v0.24.0 // indirect google.golang.org/appengine v1.6.7 // indirect diff --git a/go.sum b/go.sum index 67482dbe..6d450760 100644 --- a/go.sum +++ b/go.sum @@ -156,10 +156,10 @@ github.com/interline-io/transitland-dbutil v0.0.0-20241212203507-15a69a52c1c4 h1 github.com/interline-io/transitland-dbutil v0.0.0-20241212203507-15a69a52c1c4/go.mod h1:aPEEJCvUPJoxyzA6sP4/PF4thA+hpafYpUcFBgoKK50= github.com/interline-io/transitland-jobs v0.0.0-20241212203627-25dc585dcf7e h1:1yYXM017Sm6qAPqwKT+SQ+WBbpGpVSGqyNbtcPHS+EU= github.com/interline-io/transitland-jobs v0.0.0-20241212203627-25dc585dcf7e/go.mod h1:+lu9T9vafIPrucNWn21+UIrmd3MYeAbV3bEWkpT5Oxo= -github.com/interline-io/transitland-lib v1.0.0-rc3.0.20241221023216-8a59b5c6d534 h1:FnmYxjk0lny0xLGQ7ASCh5+wgxR5eaPT7hF81+0yqfs= -github.com/interline-io/transitland-lib v1.0.0-rc3.0.20241221023216-8a59b5c6d534/go.mod h1:Nmg+bRgs6lVmYe+pyBX9h2g19Vmz3ZXHfpSy82FaIZ0= -github.com/interline-io/transitland-mw v0.0.0-20241212203557-2e0c0fea3d26 h1:YNFsJU00rvfQyoTbKmgIAZJscdEZYn75rTR2d4aHfFs= -github.com/interline-io/transitland-mw v0.0.0-20241212203557-2e0c0fea3d26/go.mod h1:zPjgFce6JY8gHrm35Jdp59PKSQrpNfUSHN8B5I8cXSU= +github.com/interline-io/transitland-lib v1.0.0-rc3.0.20250114204131-f367913b54e8 h1:TB1AM/1tVXoIHFOu6uGPaXkavPLGGD70NSNYMWRCBhc= +github.com/interline-io/transitland-lib v1.0.0-rc3.0.20250114204131-f367913b54e8/go.mod h1:Nmg+bRgs6lVmYe+pyBX9h2g19Vmz3ZXHfpSy82FaIZ0= +github.com/interline-io/transitland-mw v0.0.0-20250110005713-3ae756e06fec h1:bmH2KN76/z1fmA6Fg84qCgit4J/jC0tgEnHo4h4W+q4= +github.com/interline-io/transitland-mw v0.0.0-20250110005713-3ae756e06fec/go.mod h1:zPjgFce6JY8gHrm35Jdp59PKSQrpNfUSHN8B5I8cXSU= github.com/invopop/yaml v0.3.1 h1:f0+ZpmhfBSS4MhG+4HYseMdJhoeeopbSKbq5Rpeelso= github.com/invopop/yaml v0.3.1/go.mod h1:PMOp3nn4/12yEZUFfmOuNHJsZToEEOwoWsT+D81KkeA= github.com/irees/kin-openapi v0.0.0-20240827112008-5f0d6c653b17 h1:8r3a8+gGbNCrB+sF5BhzOUU5N7vo7vbwz1MGmYoq5TI= @@ -206,8 +206,9 @@ github.com/lib/pq v1.10.9 h1:YXG7RB+JIjhP29X+OtkiDnYaXQwpS4JEWq7dtCCRUEw= github.com/lib/pq v1.10.9/go.mod h1:AlVN5x4E4T544tWzH6hKfbfQvm3HdbOxrmggDNAPY9o= github.com/mailru/easyjson v0.7.7 h1:UGYAvKxe3sBsEDzO8ZeWOSlIQfWFlxbzLZe7hwFURr0= github.com/mailru/easyjson v0.7.7/go.mod h1:xzfreul335JAWq5oZzymOObrkdz5UnU4kGfJJLY9Nlc= -github.com/mattn/go-colorable v0.1.13 h1:fFA4WZxdEF4tXPZVKMLwD8oUnCTTo08duU7wxecdEvA= github.com/mattn/go-colorable v0.1.13/go.mod h1:7S9/ev0klgBDR4GtXTXX8a3vIGJpMovkB8vQcUbaXHg= +github.com/mattn/go-colorable v0.1.14 h1:9A9LHSqF/7dyVVX6g0U9cwm9pG3kP9gSzcuIPHPsaIE= +github.com/mattn/go-colorable v0.1.14/go.mod h1:6LmQG8QLFO4G5z1gPvYEzlUgJ2wF+stgPZH1UqBm1s8= github.com/mattn/go-isatty v0.0.16/go.mod h1:kYGgaQfpe5nmfYZH+SKPsOc2e4SrIfOl2e/yFXSvRLM= github.com/mattn/go-isatty v0.0.19/go.mod h1:W+V8PltTTMOvKvAeJH7IuucS94S2C6jfK/D7dTCTo3Y= github.com/mattn/go-isatty v0.0.20 h1:xfD0iDuEKnDkl03q4limB+vH+GxLEtL/jb4xVJSWWEY= @@ -349,8 +350,8 @@ golang.org/x/sys v0.1.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= golang.org/x/sys v0.5.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= golang.org/x/sys v0.6.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= golang.org/x/sys v0.12.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= -golang.org/x/sys v0.28.0 h1:Fksou7UEQUWlKvIdsqzJmUmCX3cZuD2+P3XyyzwMhlA= -golang.org/x/sys v0.28.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA= +golang.org/x/sys v0.29.0 h1:TPYlXGxvx1MGTn2GiZDhnjPA9wZzZeGKHHmKhHYvgaU= +golang.org/x/sys v0.29.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA= golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo= golang.org/x/term v0.0.0-20210927222741-03fcf44c2211/go.mod h1:jbD1KX2456YbFQfuXm/mYQcufACuNUgVhRMnK/tPxf8= golang.org/x/term v0.1.0/go.mod h1:jbD1KX2456YbFQfuXm/mYQcufACuNUgVhRMnK/tPxf8= diff --git a/internal/gbfs/fetch.go b/internal/gbfs/fetch.go index 9bb8351e..1d54628f 100644 --- a/internal/gbfs/fetch.go +++ b/internal/gbfs/fetch.go @@ -74,7 +74,7 @@ func Fetch(ctx context.Context, atx tldb.Adapter, opts Options) ([]GbfsFeed, Res tlfetch.Success = false tlfetch.FetchError.Set(result.FetchError.Error()) } - if _, err := atx.Insert(&tlfetch); err != nil { + if _, err := atx.Insert(context.TODO(), &tlfetch); err != nil { return nil, result, err } } @@ -162,7 +162,8 @@ func fetchAll(ctx context.Context, sf SystemFeeds, reqOpts ...request.RequestOpt } func fetchUnmarshal(url string, ent any, reqOpts ...request.RequestOption) (request.FetchResponse, error) { - fr, err := request.AuthenticatedRequest(url, reqOpts...) + ctx := context.TODO() + fr, err := request.AuthenticatedRequest(ctx, url, reqOpts...) if err != nil { return fr, err } diff --git a/internal/testconfig/testconfig.go b/internal/testconfig/testconfig.go index b02b9c79..729a651f 100644 --- a/internal/testconfig/testconfig.go +++ b/internal/testconfig/testconfig.go @@ -21,7 +21,6 @@ import ( "github.com/interline-io/transitland-server/internal/clock" "github.com/interline-io/transitland-server/model" "github.com/interline-io/transitland-server/testdata" - "github.com/jmoiron/sqlx" "google.golang.org/protobuf/proto" ) @@ -38,8 +37,9 @@ type Options struct { } func Config(t testing.TB, opts Options) model.Config { + ctx := context.Background() db := testutil.MustOpenTestDB(t) - return newTestConfig(t, &tldb.QueryLogger{Ext: db}, opts) + return newTestConfig(t, ctx, &tldb.QueryLogger{Ext: db}, opts) } func ConfigTx(t testing.TB, opts Options, cb func(model.Config) error) { @@ -50,7 +50,7 @@ func ConfigTx(t testing.TB, opts Options, cb func(model.Config) error) { defer tx.Rollback() // Get finders - testEnv := newTestConfig(t, &tldb.QueryLogger{Ext: tx}, opts) + testEnv := newTestConfig(t, ctx, &tldb.QueryLogger{Ext: tx}, opts) // Commit or rollback if err := cb(testEnv); err != nil { @@ -81,8 +81,7 @@ func DefaultRTJson() []RTJsonFile { } } -func newTestConfig(t testing.TB, db sqlx.Ext, opts Options) model.Config { - ctx := context.Background() +func newTestConfig(t testing.TB, ctx context.Context, db tldb.Ext, opts Options) model.Config { // Default time if opts.WhenUtc == "" { opts.WhenUtc = "2022-09-01T00:00:00Z" diff --git a/model/finders.go b/model/finders.go index 6d79eec9..8a45ce94 100644 --- a/model/finders.go +++ b/model/finders.go @@ -6,11 +6,10 @@ import ( "time" "github.com/interline-io/transitland-lib/rt/pb" + "github.com/interline-io/transitland-lib/tldb" "github.com/interline-io/transitland-lib/tt" "github.com/interline-io/transitland-mw/auth/authz" "github.com/interline-io/transitland-server/internal/gbfs" - - "github.com/jmoiron/sqlx" ) // Finder provides all necessary database methods @@ -37,7 +36,7 @@ type EntityFinder interface { FindPlaces(context.Context, *int, *Cursor, []int, *PlaceAggregationLevel, *PlaceFilter) ([]*Place, error) RouteStopBuffer(context.Context, *RouteStopBufferParam) ([]*RouteStopBuffer, error) FindFeedVersionServiceWindow(context.Context, int) (*ServiceWindow, error) - DBX() sqlx.Ext // escape hatch, for now + DBX() tldb.Ext // escape hatch, for now } type EntityMutator interface { diff --git a/server/gql/entity_mutation_resolver_test.go b/server/gql/entity_mutation_resolver_test.go index 3d2123c4..e32c9483 100644 --- a/server/gql/entity_mutation_resolver_test.go +++ b/server/gql/entity_mutation_resolver_test.go @@ -34,7 +34,7 @@ func TestStopCreate(t *testing.T) { checkEnt := gtfs.Stop{} checkEnt.ID = eid atx := tldb.NewPostgresAdapterFromDBX(cfg.Finder.DBX()) - if err := atx.Find(&checkEnt); err != nil { + if err := atx.Find(ctx, &checkEnt); err != nil { t.Fatal(err) } assert.Equal(t, stopInput.StopID, &checkEnt.StopID.Val) @@ -69,7 +69,7 @@ func TestStopUpdate(t *testing.T) { checkEnt := gtfs.Stop{} checkEnt.ID = eid atx := tldb.NewPostgresAdapterFromDBX(cfg.Finder.DBX()) - if err := atx.Find(&checkEnt); err != nil { + if err := atx.Find(ctx, &checkEnt); err != nil { t.Fatal(err) } assert.Equal(t, stopUpdate.StopID, &checkEnt.StopID.Val)