Skip to content

Commit

Permalink
Yet more log ctx updates (#296)
Browse files Browse the repository at this point in the history
* Yet more log ctx updates
  • Loading branch information
irees authored Jan 14, 2025
1 parent 699403b commit c3ab89d
Show file tree
Hide file tree
Showing 12 changed files with 41 additions and 43 deletions.
8 changes: 3 additions & 5 deletions cmd/tlserver/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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)
Expand Down
4 changes: 2 additions & 2 deletions finders/actions/feed_fetch.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down Expand Up @@ -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
}
Expand Down
8 changes: 4 additions & 4 deletions finders/actions/fv.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand All @@ -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
}
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
6 changes: 3 additions & 3 deletions finders/dbfinder/entity_mutations.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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
Expand Down
8 changes: 4 additions & 4 deletions finders/dbfinder/finder.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,31 +9,31 @@ 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),
}
return finder
}

func (f *Finder) DBX() sqlx.Ext {
func (f *Finder) DBX() tldb.Ext {
return f.db
}

Expand Down
4 changes: 2 additions & 2 deletions finders/rtfinder/finder.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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,
Expand Down
8 changes: 4 additions & 4 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
15 changes: 8 additions & 7 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -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=
Expand Down Expand Up @@ -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=
Expand Down Expand Up @@ -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=
Expand Down
5 changes: 3 additions & 2 deletions internal/gbfs/fetch.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
}
Expand Down Expand Up @@ -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
}
Expand Down
9 changes: 4 additions & 5 deletions internal/testconfig/testconfig.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)

Expand All @@ -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) {
Expand All @@ -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 {
Expand Down Expand Up @@ -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"
Expand Down
5 changes: 2 additions & 3 deletions model/finders.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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 {
Expand Down
4 changes: 2 additions & 2 deletions server/gql/entity_mutation_resolver_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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)
Expand Down

0 comments on commit c3ab89d

Please sign in to comment.