aboutsummaryrefslogtreecommitdiff
path: root/database/status.go
blob: 14f24ed2677f324e25c0f685a204a7ce07e10bdf (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
package database

import (
	"context"
	"fmt"
	"metachan/types"
	"metachan/utils/logger"
	"time"
)

func DatabaseConnectionStatus() bool {
	if DB == nil {
		return false
	}

	sqlDB, err := DB.DB()
	if err != nil {
		logger.Log(fmt.Sprintf("Unable to get SQL DB: %v", err), types.LogOptions{
			Prefix: "Database",
			Level:  types.Error,
		})
		return false
	}

	ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
	defer cancel()

	err = sqlDB.PingContext(ctx)
	if err != nil {
		logger.Log(fmt.Sprintf("Database connection error: %v", err), types.LogOptions{
			Prefix: "Database",
			Level:  types.Error,
		})
		return false
	}
	return true
}