「✨」 feat(Mariadb): working health check f*ck yeaaaaahh
This commit is contained in:
@ -1,60 +1,51 @@
|
|||||||
package main
|
package main
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"database/sql"
|
|
||||||
"fmt"
|
"fmt"
|
||||||
"os"
|
"os"
|
||||||
|
"os/exec"
|
||||||
"strings"
|
"strings"
|
||||||
|
"time"
|
||||||
|
|
||||||
"git.keyzox.me/42_adjoly/inception/internal/env"
|
"git.keyzox.me/42_adjoly/inception/internal/env"
|
||||||
"git.keyzox.me/42_adjoly/inception/internal/log"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
func escapeIdentifier(identifier string) string {
|
func checkMariaDB(user, password, host, port string) bool {
|
||||||
// Replace backticks with double backticks to safely escape identifiers
|
// Create the command to run mariadb client
|
||||||
return fmt.Sprintf("`%s`", strings.ReplaceAll(identifier, "'", "\""))
|
cmd := exec.Command("mariadb", "-u"+user, "-p"+password, "-h"+host, "-P"+port, "-e", "SELECT 1;")
|
||||||
|
|
||||||
|
// Run the command
|
||||||
|
err := cmd.Run()
|
||||||
|
if err != nil {
|
||||||
|
fmt.Printf("Health check failed: %v\n", err)
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
fmt.Println("MariaDB is healthy")
|
||||||
|
return true
|
||||||
}
|
}
|
||||||
|
|
||||||
func escapePassword(password string) string {
|
func escapePassword(password string) string {
|
||||||
// Escape single quotes in passwords
|
// Escape single quotes in passwords
|
||||||
return strings.ReplaceAll(password, "'", "\\'")
|
new := strings.ReplaceAll(password, "\"", "")
|
||||||
}
|
return strings.ReplaceAll(new, "'", "\\'")
|
||||||
|
|
||||||
func checkHealth(host, user, pass, port, dbName string) bool {
|
|
||||||
dsn := fmt.Sprintf("%s:%s@tcp(%s:%s)/%s", user, pass, host, port, dbName)
|
|
||||||
|
|
||||||
// Attempt to open a database connection
|
|
||||||
db, err := sql.Open("mysql", dsn)
|
|
||||||
if err != nil {
|
|
||||||
_log.Log("warning", fmt.Sprintf("Failed to open database connection: %v", err))
|
|
||||||
return false
|
|
||||||
}
|
|
||||||
defer db.Close()
|
|
||||||
|
|
||||||
// Attempt to ping the database
|
|
||||||
if err := db.Ping(); err != nil {
|
|
||||||
_log.Log("warning", fmt.Sprintf("Health check failed: %v", err))
|
|
||||||
return false
|
|
||||||
}
|
|
||||||
|
|
||||||
_log.Log("note", "Health check passed successfully")
|
|
||||||
return true
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
// Load environment variables
|
// Configuration
|
||||||
pass := escapePassword(env.FileEnv("MYSQL_PASSWORD", "default"))
|
user := escapePassword(env.FileEnv("MYSQL_USER", "mariadb"))
|
||||||
user := escapeIdentifier(env.FileEnv("MYSQL_USER", "mariadb"))
|
password := escapePassword(env.FileEnv("MYSQL_PASSWORD", "default"))
|
||||||
dbName := escapeIdentifier(env.EnvCheck("MYSQL_DATABASE", "default"))
|
host := "127.0.0.1"
|
||||||
dbHost := "127.0.0.1"
|
port := "3306"
|
||||||
|
|
||||||
// Perform the health check
|
// Retry health check for MariaDB
|
||||||
res := checkHealth(dbHost, user, pass, "3306", dbName)
|
for i := 0; i < 10; i++ {
|
||||||
if res {
|
if checkMariaDB(user, password, host, port) {
|
||||||
_log.Log("note", "MariaDB is healthy")
|
os.Exit(0) // Success
|
||||||
os.Exit(0)
|
}
|
||||||
|
fmt.Println("Waiting for MariaDB to become ready...")
|
||||||
|
time.Sleep(2 * time.Second)
|
||||||
}
|
}
|
||||||
|
|
||||||
_log.Log("warning", "Health check failed")
|
fmt.Println("MariaDB health check failed")
|
||||||
os.Exit(1)
|
os.Exit(1) // Failure
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user