From e53f537f193e569e0684151f1f3afa2e5ba0074a Mon Sep 17 00:00:00 2001 From: Quentin McGaw Date: Thu, 13 Jun 2024 08:51:28 +0000 Subject: [PATCH] Database service --- cmd/updater/main.go | 10 ++-------- internal/data/data.go | 15 +++++++++++++++ internal/data/persistence.go | 6 ------ 3 files changed, 17 insertions(+), 14 deletions(-) diff --git a/cmd/updater/main.go b/cmd/updater/main.go index d1085b413..514b5c078 100644 --- a/cmd/updater/main.go +++ b/cmd/updater/main.go @@ -173,12 +173,6 @@ func _main(ctx context.Context, reader *reader.Reader, args []string, logger log } db := data.NewDatabase(records, persistentDB) - defer func() { - err := db.Close() - if err != nil { - logger.Error(err.Error()) - } - }() httpSettings := publicip.HTTPSettings{ Enabled: *config.PubIP.HTTPEnabled, @@ -236,8 +230,8 @@ func _main(ctx context.Context, reader *reader.Reader, args []string, logger log } servicesSequence, err := goservices.NewSequence(goservices.SequenceSettings{ - ServicesStart: []goservices.Service{updaterService, healthServer, server, backupService}, - ServicesStop: []goservices.Service{server, healthServer, updaterService, backupService}, + ServicesStart: []goservices.Service{db, updaterService, healthServer, server, backupService}, + ServicesStop: []goservices.Service{server, healthServer, updaterService, backupService, db}, }) if err != nil { return fmt.Errorf("creating services sequence: %w", err) diff --git a/internal/data/data.go b/internal/data/data.go index b5eff641d..4a3816fa3 100644 --- a/internal/data/data.go +++ b/internal/data/data.go @@ -1,6 +1,7 @@ package data import ( + "context" "sync" "github.com/qdm12/ddns-updater/internal/records" @@ -19,3 +20,17 @@ func NewDatabase(data []records.Record, persistentDB PersistentDatabase) *Databa persistentDB: persistentDB, } } + +func (db *Database) String() string { + return "database" +} + +func (db *Database) Start(_ context.Context) (_ <-chan error, err error) { + return nil, nil //nolint:nilnil +} + +func (db *Database) Stop() (err error) { + db.Lock() // ensure write operation finishes + defer db.Unlock() + return db.persistentDB.Close() +} diff --git a/internal/data/persistence.go b/internal/data/persistence.go index 75048dadc..018bdcffa 100644 --- a/internal/data/persistence.go +++ b/internal/data/persistence.go @@ -28,9 +28,3 @@ func (db *Database) Update(id uint, record records.Record) (err error) { } return nil } - -func (db *Database) Close() (err error) { - db.Lock() // ensure write operation finishes - defer db.Unlock() - return db.persistentDB.Close() -}