Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore(relayer): close db connection before stop service. #17709

Merged
merged 1 commit into from
Jul 2, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 @@ -259,6 +259,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 @@ -390,6 +390,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 @@ -210,6 +210,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
Loading