Skip to content

Commit

Permalink
Database service
Browse files Browse the repository at this point in the history
  • Loading branch information
qdm12 committed Jun 13, 2024
1 parent ab8f3da commit e53f537
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 14 deletions.
10 changes: 2 additions & 8 deletions cmd/updater/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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)
Expand Down
15 changes: 15 additions & 0 deletions internal/data/data.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package data

import (
"context"
"sync"

"github.com/qdm12/ddns-updater/internal/records"
Expand All @@ -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()
}
6 changes: 0 additions & 6 deletions internal/data/persistence.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()
}

0 comments on commit e53f537

Please sign in to comment.