Skip to content

Commit

Permalink
chore(relayer): close db connection before stop service. (#17709)
Browse files Browse the repository at this point in the history
  • Loading branch information
mask-pp authored Jul 2, 2024
1 parent d0e25ba commit 9e928ad
Show file tree
Hide file tree
Showing 7 changed files with 37 additions and 0 deletions.
1 change: 1 addition & 0 deletions packages/relayer/event.go
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,7 @@ type FindAllByAddressOpts struct {

// EventRepository is used to interact with events in the store
type EventRepository interface {
Close() error
Save(ctx context.Context, opts *SaveEventOpts) (*Event, error)
UpdateStatus(ctx context.Context, id int, status EventStatus) error
UpdateFeesAndProfitability(ctx context.Context, id int, opts *UpdateFeesAndProfitabilityOpts) error
Expand Down
5 changes: 5 additions & 0 deletions packages/relayer/indexer/indexer.go
Original file line number Diff line number Diff line change
Expand Up @@ -252,6 +252,11 @@ func (i *Indexer) Name() string {
// context is stopped externally by cmd/main.go shutdown.
func (i *Indexer) Close(ctx context.Context) {
i.wg.Wait()

// Close db connection.
if err := i.eventRepo.Close(); err != nil {
slog.Error("Failed to close db connection", "err", err)
}
}

// Start starts the indexer, which should initialize the queue, add to wait groups,
Expand Down
6 changes: 6 additions & 0 deletions packages/relayer/pkg/http/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package http

import (
"context"
"log/slog"
"math/big"
"net/http"
"os"
Expand Down Expand Up @@ -130,6 +131,11 @@ func (srv *Server) Start(address string) error {

// Shutdown shuts down the HTTP server
func (srv *Server) Shutdown(ctx context.Context) error {
// Close db connection.
if err := srv.eventRepo.Close(); err != nil {
slog.Error("Failed to close db connection", "err", err)
}

return srv.echo.Shutdown(ctx)
}

Expand Down
5 changes: 5 additions & 0 deletions packages/relayer/pkg/mock/event_repository.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,11 @@ func NewEventRepository() *EventRepository {
events: make([]*relayer.Event, 0),
}
}

func (r *EventRepository) Close() error {
return nil
}

func (r *EventRepository) Save(ctx context.Context, opts *relayer.SaveEventOpts) (*relayer.Event, error) {
r.events = append(r.events, &relayer.Event{
ID: rand.Int(), // nolint: gosec
Expand Down
10 changes: 10 additions & 0 deletions packages/relayer/pkg/repo/event.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,16 @@ func NewEventRepository(dbHandler db.DB) (*EventRepository, error) {
}, nil
}

// Close closes the database connection.
func (r *EventRepository) Close() error {
sqlDB, err := r.db.DB()
if err != nil {
return err
}

return sqlDB.Close()
}

func (r *EventRepository) Save(ctx context.Context, opts *relayer.SaveEventOpts) (*relayer.Event, error) {
e := &relayer.Event{
Data: datatypes.JSON(opts.Data),
Expand Down
5 changes: 5 additions & 0 deletions packages/relayer/processor/processor.go
Original file line number Diff line number Diff line change
Expand Up @@ -383,6 +383,11 @@ func (p *Processor) Close(ctx context.Context) {
p.cancel()

p.wg.Wait()

// Close db connection.
if err := p.eventRepo.Close(); err != nil {
slog.Error("Failed to close db connection", "err", err)
}
}

func (p *Processor) Start() error {
Expand Down
5 changes: 5 additions & 0 deletions packages/relayer/watchdog/watchdog.go
Original file line number Diff line number Diff line change
Expand Up @@ -202,6 +202,11 @@ func (w *Watchdog) Close(ctx context.Context) {
w.cancel()

w.wg.Wait()

// Close db connection.
if err := w.eventRepo.Close(); err != nil {
slog.Error("Failed to close db connection", "err", err)
}
}

func (w *Watchdog) Start() error {
Expand Down

0 comments on commit 9e928ad

Please sign in to comment.