Skip to content

Commit

Permalink
fix: db singleton
Browse files Browse the repository at this point in the history
  • Loading branch information
kevincobain2000 committed Dec 16, 2024
1 parent a9b298a commit 60bb492
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 9 deletions.
2 changes: 1 addition & 1 deletion main.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ var filePathsMutex sync.Mutex

func main() {
flags()
pkg.SetupLoggingStdout(f.LogLevel, f.Log)
pkg.SetupLoggingStdout(f.LogLevel, f.Log) // nolint: errcheck
flag.VisitAll(func(f *flag.Flag) {
slog.Info(f.Name, slog.String("value", f.Value.String()))
})
Expand Down
23 changes: 16 additions & 7 deletions pkg/database.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,21 @@ import (
_ "github.com/glebarez/go-sqlite" // nolint: revive
)

var (
db *sql.DB
)

func InitDB(dbName string) (*sql.DB, error) {
slog.Info("Initializing database", "dbName", dbName)
db, err := sql.Open("sqlite", dbName)
if err != nil {
slog.Error("Error opening database", "error", err.Error())
return nil, err
var err error
if db == nil {
slog.Info("Initializing database", "dbName", dbName)
db, err = sql.Open("sqlite", dbName)
if err != nil {
slog.Error("Error opening database", "error", err.Error())
return nil, err
}
} else {
slog.Info("Database already initialized")
}

_, err = db.Exec(`
Expand All @@ -38,8 +47,8 @@ func InitDB(dbName string) (*sql.DB, error) {
slog.Error("Error creating plot table", "error", err.Error())
return nil, err
}
db.SetMaxOpenConns(5)
db.SetMaxIdleConns(5)
db.SetMaxOpenConns(10)
db.SetMaxIdleConns(10)
db.SetConnMaxLifetime(time.Hour)

return db, nil
Expand Down
1 change: 1 addition & 0 deletions pkg/log.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ func SetupLoggingStdout(logLevel int, logFile string) error {
}
if logFile == "" {
slog.SetDefault(slog.New(slogcolor.NewHandler(os.Stderr, opts)))
return nil
}
slog.SetDefault(slog.New(slogcolor.NewHandler(&lumberjack.Logger{
Filename: logFile,
Expand Down
3 changes: 2 additions & 1 deletion pkg/watcher.go
Original file line number Diff line number Diff line change
Expand Up @@ -197,5 +197,6 @@ func (w *Watcher) saveState() error {
}

func (w *Watcher) Close() error {
return w.db.Close()
// return w.db.Close()
return nil
}

0 comments on commit 60bb492

Please sign in to comment.