fix: check.sh — не падать до docker compose, проверка после старта
Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
+67
-23
@@ -4,8 +4,10 @@ import (
|
||||
"context"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"net/url"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"strings"
|
||||
|
||||
"github.com/jackc/pgx/v5/pgxpool"
|
||||
|
||||
@@ -15,38 +17,30 @@ import (
|
||||
func main() {
|
||||
loadDotEnv()
|
||||
|
||||
skipDB := os.Getenv("CHECK_SKIP_DB") == "1"
|
||||
hostTools := os.Getenv("CHECK_HOST_TOOLS") != "0"
|
||||
|
||||
ctx := context.Background()
|
||||
report := check.AppInfo()
|
||||
report.Items = append(report.Items, check.ToolVersions(ctx)...)
|
||||
if hostTools {
|
||||
report.Items = append(report.Items, check.ToolVersions(ctx)...)
|
||||
}
|
||||
|
||||
dbURL := os.Getenv("DATABASE_URL")
|
||||
if dbURL == "" {
|
||||
if skipDB {
|
||||
report.Items = append(report.Items, check.Item{
|
||||
Name: "database",
|
||||
Status: check.StatusWarn,
|
||||
Detail: "DATABASE_URL не задан — запустите: go run ./cmd/install",
|
||||
Detail: "проверка отложена — сначала выполните: docker compose up -d, затем ./check.sh --after-start",
|
||||
})
|
||||
} else if dbURL == "" {
|
||||
report.Items = append(report.Items, check.Item{
|
||||
Name: "database",
|
||||
Status: check.StatusWarn,
|
||||
Detail: "DATABASE_URL не задан — запустите: ./install.sh",
|
||||
})
|
||||
} else {
|
||||
pool, err := pgxpool.New(ctx, dbURL)
|
||||
if err != nil {
|
||||
report.Items = append(report.Items, check.Item{
|
||||
Name: "database",
|
||||
Status: check.StatusError,
|
||||
Detail: err.Error(),
|
||||
})
|
||||
} else {
|
||||
defer pool.Close()
|
||||
dbItems, err := check.Database(ctx, pool)
|
||||
if err != nil {
|
||||
report.Items = append(report.Items, check.Item{
|
||||
Name: "database",
|
||||
Status: check.StatusError,
|
||||
Detail: err.Error(),
|
||||
})
|
||||
} else {
|
||||
report.Items = append(report.Items, dbItems...)
|
||||
}
|
||||
}
|
||||
appendDBChecks(ctx, &report, dbURL)
|
||||
}
|
||||
|
||||
enc := json.NewEncoder(os.Stdout)
|
||||
@@ -61,6 +55,56 @@ func main() {
|
||||
}
|
||||
}
|
||||
|
||||
func appendDBChecks(ctx context.Context, report *check.Report, dbURL string) {
|
||||
pool, err := pgxpool.New(ctx, dbURL)
|
||||
if err != nil {
|
||||
report.Items = append(report.Items, dbCheckItem(err, dbURL))
|
||||
return
|
||||
}
|
||||
defer pool.Close()
|
||||
|
||||
dbItems, err := check.Database(ctx, pool)
|
||||
if err != nil {
|
||||
report.Items = append(report.Items, check.Item{
|
||||
Name: "database",
|
||||
Status: dbCheckStatus(err, dbURL),
|
||||
Detail: err.Error(),
|
||||
})
|
||||
return
|
||||
}
|
||||
report.Items = append(report.Items, dbItems...)
|
||||
}
|
||||
|
||||
func dbCheckItem(err error, dbURL string) check.Item {
|
||||
return check.Item{
|
||||
Name: "database",
|
||||
Status: dbCheckStatus(err, dbURL),
|
||||
Detail: err.Error(),
|
||||
}
|
||||
}
|
||||
|
||||
func dbCheckStatus(err error, dbURL string) check.Status {
|
||||
if err == nil {
|
||||
return check.StatusOK
|
||||
}
|
||||
msg := strings.ToLower(err.Error())
|
||||
if isDockerInternalHost(dbURL) && (strings.Contains(msg, "no such host") ||
|
||||
strings.Contains(msg, "name or service not known") ||
|
||||
strings.Contains(msg, "hostname resolving")) {
|
||||
return check.StatusWarn
|
||||
}
|
||||
return check.StatusError
|
||||
}
|
||||
|
||||
func isDockerInternalHost(dbURL string) bool {
|
||||
u, err := url.Parse(dbURL)
|
||||
if err != nil {
|
||||
return false
|
||||
}
|
||||
host := u.Hostname()
|
||||
return host == "postgres" || host == "db" || host == "shop-postgres"
|
||||
}
|
||||
|
||||
func printSummary(r check.Report) {
|
||||
fmt.Printf("ShopNova %s | %s\n\n", r.AppVersion, r.GoVersion)
|
||||
for _, it := range r.Items {
|
||||
|
||||
Reference in New Issue
Block a user