Skip to content

Commit

Permalink
Merge branch 'main' into agodnic/event-watcher-create-service-boilerp…
Browse files Browse the repository at this point in the history
…late
  • Loading branch information
agodnic committed Aug 7, 2023
2 parents f761c97 + 3c7bab3 commit e943fc3
Show file tree
Hide file tree
Showing 22 changed files with 181 additions and 263 deletions.
9 changes: 5 additions & 4 deletions analytics/cmd/metrics/volume_mongo.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import (
"time"

"github.com/wormhole-foundation/wormhole-explorer/analytics/prices"
"github.com/wormhole-foundation/wormhole-explorer/common/db"
"github.com/wormhole-foundation/wormhole-explorer/common/dbutil"
"github.com/wormhole-foundation/wormhole-explorer/common/logger"
"github.com/wormhole-foundation/wormhole-explorer/common/repository"
"go.uber.org/zap"
Expand All @@ -25,7 +25,7 @@ func RunVaaVolumeFromMongo(mongoUri, mongoDb, outputFile, pricesFile string) {
logger.Info("starting wormhole-explorer-analytics ...")

//setup DB connection
db, err := db.New(rootCtx, logger, mongoUri, mongoDb)
db, err := dbutil.Connect(rootCtx, logger, mongoUri, mongoDb)
if err != nil {
logger.Fatal("Failed to connect MongoDB", zap.Error(err))
}
Expand Down Expand Up @@ -98,8 +98,9 @@ func RunVaaVolumeFromMongo(mongoUri, mongoDb, outputFile, pricesFile string) {
}
page++
}
logger.Info("Closing database connections ...")
db.Close()

logger.Info("closing MongoDB connection...")
db.DisconnectWithTimeout(10 * time.Second)

for k := range converter.MissingTokensCounter {
fmissingTokens.WriteString(fmt.Sprintf("%s,%s,%d\n", k.String(), converter.MissingTokens[k], converter.MissingTokensCounter[k]))
Expand Down
11 changes: 8 additions & 3 deletions analytics/cmd/service/run.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"os"
"os/signal"
"syscall"
"time"

"github.com/aws/aws-sdk-go-v2/aws"
awsconfig "github.com/aws/aws-sdk-go-v2/config"
Expand All @@ -23,7 +24,7 @@ import (
"github.com/wormhole-foundation/wormhole-explorer/analytics/queue"
wormscanNotionalCache "github.com/wormhole-foundation/wormhole-explorer/common/client/cache/notional"
sqs_client "github.com/wormhole-foundation/wormhole-explorer/common/client/sqs"
"github.com/wormhole-foundation/wormhole-explorer/common/db"
"github.com/wormhole-foundation/wormhole-explorer/common/dbutil"
health "github.com/wormhole-foundation/wormhole-explorer/common/health"
"github.com/wormhole-foundation/wormhole-explorer/common/logger"
"go.mongodb.org/mongo-driver/mongo"
Expand Down Expand Up @@ -57,7 +58,7 @@ func Run() {

// setup DB connection
logger.Info("connecting to MongoDB...")
db, err := db.New(rootCtx, logger, config.MongodbURI, config.MongodbDatabase)
db, err := dbutil.Connect(rootCtx, logger, config.MongodbURI, config.MongodbDatabase)
if err != nil {
logger.Fatal("failed to connect MongoDB", zap.Error(err))
}
Expand Down Expand Up @@ -119,12 +120,16 @@ func Run() {

logger.Info("cancelling root context...")
rootCtxCancel()

logger.Info("closing metrics client...")
metric.Close()

logger.Info("closing HTTP server...")
server.Stop()

logger.Info("closing MongoDB connection...")
db.Close()
db.DisconnectWithTimeout(10 * time.Second)

logger.Info("terminated successfully")
}

Expand Down
19 changes: 0 additions & 19 deletions api/internal/db/db.go

This file was deleted.

25 changes: 15 additions & 10 deletions api/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,14 +32,14 @@ import (
"github.com/wormhole-foundation/wormhole-explorer/api/handlers/transactions"
"github.com/wormhole-foundation/wormhole-explorer/api/handlers/vaa"
"github.com/wormhole-foundation/wormhole-explorer/api/internal/config"
"github.com/wormhole-foundation/wormhole-explorer/api/internal/db"
"github.com/wormhole-foundation/wormhole-explorer/api/internal/tvl"
"github.com/wormhole-foundation/wormhole-explorer/api/middleware"
"github.com/wormhole-foundation/wormhole-explorer/api/response"
"github.com/wormhole-foundation/wormhole-explorer/api/routes/guardian"
"github.com/wormhole-foundation/wormhole-explorer/api/routes/wormscan"
rpcApi "github.com/wormhole-foundation/wormhole-explorer/api/rpc"
wormscanCache "github.com/wormhole-foundation/wormhole-explorer/common/client/cache"
"github.com/wormhole-foundation/wormhole-explorer/common/dbutil"
xlogger "github.com/wormhole-foundation/wormhole-explorer/common/logger"
"github.com/wormhole-foundation/wormhole-explorer/common/utils"
"go.uber.org/zap"
Expand Down Expand Up @@ -103,11 +103,10 @@ func main() {

// Setup DB
rootLogger.Info("connecting to MongoDB")
cli, err := db.Connect(appCtx, cfg.DB.URL)
db, err := dbutil.Connect(appCtx, rootLogger, cfg.DB.URL, cfg.DB.Name)
if err != nil {
rootLogger.Fatal("failed to connect to MongoDB", zap.Error(err))
}
db := cli.Database(cfg.DB.Name)

// Get cache get function
rootLogger.Info("initializing cache")
Expand All @@ -126,20 +125,20 @@ func main() {

// Set up repositories
rootLogger.Info("initializing repositories")
addressRepo := address.NewRepository(db, rootLogger)
vaaRepo := vaa.NewRepository(db, rootLogger)
obsRepo := observations.NewRepository(db, rootLogger)
governorRepo := governor.NewRepository(db, rootLogger)
infrastructureRepo := infrastructure.NewRepository(db, rootLogger)
heartbeatsRepo := heartbeats.NewRepository(db, rootLogger)
addressRepo := address.NewRepository(db.Database, rootLogger)
vaaRepo := vaa.NewRepository(db.Database, rootLogger)
obsRepo := observations.NewRepository(db.Database, rootLogger)
governorRepo := governor.NewRepository(db.Database, rootLogger)
infrastructureRepo := infrastructure.NewRepository(db.Database, rootLogger)
heartbeatsRepo := heartbeats.NewRepository(db.Database, rootLogger)
transactionsRepo := transactions.NewRepository(
tvl,
influxCli,
cfg.Influx.Organization,
cfg.Influx.Bucket24Hours,
cfg.Influx.Bucket30Days,
cfg.Influx.BucketInfinite,
db,
db.Database,
rootLogger,
)

Expand Down Expand Up @@ -223,10 +222,16 @@ func main() {
}

rootLogger.Info("cleanup tasks...")

rootLogger.Info("shutting down server...")
app.Shutdown()

rootLogger.Info("closing cache...")
cache.Close()

rootLogger.Info("closing MongoDB connection...")
db.DisconnectWithTimeout(10 * time.Second)

rootLogger.Info("terminated API service successfully")
}

Expand Down
31 changes: 0 additions & 31 deletions common/db/db.go

This file was deleted.

76 changes: 76 additions & 0 deletions common/dbutil/session.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
package dbutil

import (
"context"
"fmt"
"time"

"go.mongodb.org/mongo-driver/mongo"
"go.mongodb.org/mongo-driver/mongo/options"
"go.mongodb.org/mongo-driver/mongo/readpref"
"go.uber.org/zap"
)

// Session is a plain-old-data struct that represents a handle to a MongoDB database.
type Session struct {
Client *mongo.Client
Database *mongo.Database
logger *zap.Logger
}

// Connect to a MongoDB database.
func Connect(
ctx context.Context,
logger *zap.Logger,
uri string,
databaseName string,
) (*Session, error) {

// Create a timed sub-context for the connection attempt
const connectTimeout = 10 * time.Second
subContext, cancelFunc := context.WithTimeout(ctx, connectTimeout)
defer cancelFunc()

// Connect to MongoDB
client, err := mongo.Connect(subContext, options.Client().ApplyURI(uri))
if err != nil {
return nil, fmt.Errorf("failed to connect to MongoDB: %w", err)
}

// Ping the database to make sure we're actually connected
//
// This can detect a misconfuiguration error when a service is being initialized,
// rather than waiting for the first query to fail in the service's processing loop.
err = client.Ping(subContext, readpref.Primary())
if err != nil {
return nil, fmt.Errorf("failed to ping MongoDB database: %w", err)
}

// Populate the result struct and return
db := &Session{
Client: client,
Database: client.Database(databaseName),
}
return db, nil
}

// Disconnect from a MongoDB database.
func (s *Session) DisconnectWithTimeout(timeout time.Duration) error {

// Create a timed sub-context for the disconnection attempt
subContext, cancelFunc := context.WithTimeout(context.Background(), timeout)
defer cancelFunc()

// Attempt to disconnect
err := s.Client.Disconnect(subContext)
if err != nil {
s.logger.Warn(
"failed to disconnect from MongoDB",
zap.Duration("timeout", timeout),
zap.Error(err),
)
return fmt.Errorf("failed to disconnect from MongoDB: %w", err)
}

return nil
}
9 changes: 5 additions & 4 deletions contract-watcher/cmd/backfiller/run.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,14 @@ package backfiller

import (
"context"
"time"

"github.com/wormhole-foundation/wormhole-explorer/common/client/alert"
"github.com/wormhole-foundation/wormhole-explorer/common/dbutil"
"github.com/wormhole-foundation/wormhole-explorer/common/domain"
"github.com/wormhole-foundation/wormhole-explorer/common/logger"
"github.com/wormhole-foundation/wormhole-explorer/contract-watcher/builder"
"github.com/wormhole-foundation/wormhole-explorer/contract-watcher/config"
"github.com/wormhole-foundation/wormhole-explorer/contract-watcher/internal/db"
"github.com/wormhole-foundation/wormhole-explorer/contract-watcher/internal/metrics"
"github.com/wormhole-foundation/wormhole-explorer/contract-watcher/storage"
"github.com/wormhole-foundation/wormhole-explorer/contract-watcher/watcher"
Expand All @@ -24,7 +25,7 @@ func Run(config *config.BackfillerConfiguration) {
logger.Info("Starting wormhole-explorer-contract-watcher as backfiller ...")

//setup DB connection
db, err := db.New(rootCtx, logger, config.MongoURI, config.MongoDatabase)
db, err := dbutil.Connect(rootCtx, logger, config.MongoURI, config.MongoDatabase)
if err != nil {
logger.Fatal("failed to connect MongoDB", zap.Error(err))
}
Expand Down Expand Up @@ -58,8 +59,8 @@ func Run(config *config.BackfillerConfiguration) {

watcher.Backfill(rootCtx, config.FromBlock, config.ToBlock, config.PageSize, config.PersistBlock)

logger.Info("Closing database connections ...")
db.Close()
logger.Info("closing MongoDB connection...")
db.DisconnectWithTimeout(10 * time.Second)

logger.Info("Finish wormhole-explorer-contract-watcher as backfiller")

Expand Down
11 changes: 7 additions & 4 deletions contract-watcher/cmd/service/run.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
"time"

"github.com/wormhole-foundation/wormhole-explorer/common/client/alert"
"github.com/wormhole-foundation/wormhole-explorer/common/dbutil"
"github.com/wormhole-foundation/wormhole-explorer/common/domain"
"github.com/wormhole-foundation/wormhole-explorer/common/health"
"github.com/wormhole-foundation/wormhole-explorer/common/logger"
Expand All @@ -17,7 +18,6 @@ import (
"github.com/wormhole-foundation/wormhole-explorer/contract-watcher/http/infrastructure"
cwAlert "github.com/wormhole-foundation/wormhole-explorer/contract-watcher/internal/alert"
"github.com/wormhole-foundation/wormhole-explorer/contract-watcher/internal/ankr"
"github.com/wormhole-foundation/wormhole-explorer/contract-watcher/internal/db"
"github.com/wormhole-foundation/wormhole-explorer/contract-watcher/internal/metrics"
"github.com/wormhole-foundation/wormhole-explorer/contract-watcher/processor"
"github.com/wormhole-foundation/wormhole-explorer/contract-watcher/storage"
Expand Down Expand Up @@ -78,7 +78,7 @@ func Run() {
logger.Info("Starting wormhole-explorer-contract-watcher ...")

//setup DB connection
db, err := db.New(rootCtx, logger, config.MongoURI, config.MongoDatabase)
db, err := dbutil.Connect(rootCtx, logger, config.MongoURI, config.MongoDatabase)
if err != nil {
logger.Fatal("failed to connect MongoDB", zap.Error(err))
}
Expand Down Expand Up @@ -126,10 +126,13 @@ func Run() {

logger.Info("Closing processor ...")
processor.Close()
logger.Info("Closing database connections ...")
db.Close()

logger.Info("closing MongoDB connection...")
db.DisconnectWithTimeout(10 * time.Second)

logger.Info("Closing Http server ...")
server.Stop()

logger.Info("Finished wormhole-explorer-contract-watcher")
}

Expand Down
31 changes: 0 additions & 31 deletions contract-watcher/internal/db/db.go

This file was deleted.

Loading

0 comments on commit e943fc3

Please sign in to comment.