mirror of
https://github.com/crocofied/CoreControl.git
synced 2025-12-17 23:47:13 +00:00
v0.0.5 uptime_fix->main
v0.0.5
This commit is contained in:
commit
f024b0166f
@ -102,45 +102,70 @@ func getApplications(db *sql.DB) []Application {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func checkAndUpdateStatus(db *sql.DB, client *http.Client, apps []Application) {
|
func checkAndUpdateStatus(db *sql.DB, client *http.Client, apps []Application) {
|
||||||
for _, app := range apps {
|
fmt.Printf("Start checking %d applications at %v\n", len(apps), time.Now())
|
||||||
// Context for HTTP request
|
|
||||||
|
for i, app := range apps {
|
||||||
|
logPrefix := fmt.Sprintf("[App %d/%d URL: %s]", i+1, len(apps), app.PublicURL)
|
||||||
|
fmt.Printf("%s Starting check\n", logPrefix)
|
||||||
|
|
||||||
|
// HTTP Check
|
||||||
|
startHTTP := time.Now()
|
||||||
httpCtx, httpCancel := context.WithTimeout(context.Background(), 4*time.Second)
|
httpCtx, httpCancel := context.WithTimeout(context.Background(), 4*time.Second)
|
||||||
defer httpCancel()
|
defer httpCancel()
|
||||||
|
|
||||||
req, err := http.NewRequestWithContext(httpCtx, "HEAD", app.PublicURL, nil)
|
req, err := http.NewRequestWithContext(httpCtx, "HEAD", app.PublicURL, nil)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
fmt.Printf("Error creating request: %v\n", err)
|
fmt.Printf("%s Request creation failed: %v\n", logPrefix, err)
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
|
|
||||||
resp, err := client.Do(req)
|
resp, err := client.Do(req)
|
||||||
isOnline := false
|
httpDuration := time.Since(startHTTP)
|
||||||
if err == nil && resp.StatusCode >= 200 && resp.StatusCode < 300 {
|
|
||||||
isOnline = true
|
// Log HTTP details
|
||||||
|
if err != nil {
|
||||||
|
fmt.Printf("%s HTTP error after %v: %v\n", logPrefix, httpDuration, err)
|
||||||
|
} else {
|
||||||
|
fmt.Printf("%s HTTP %d after %v (ContentLength: %d)\n",
|
||||||
|
logPrefix, resp.StatusCode, httpDuration, resp.ContentLength)
|
||||||
|
resp.Body.Close() // Important to prevent leaks
|
||||||
}
|
}
|
||||||
|
|
||||||
// Create a new context for database operations with a separate timeout
|
isOnline := err == nil && resp != nil && resp.StatusCode >= 200 && resp.StatusCode < 300 || resp.StatusCode == 405
|
||||||
|
|
||||||
|
// Database Update
|
||||||
dbCtx, dbCancel := context.WithTimeout(context.Background(), 5*time.Second)
|
dbCtx, dbCancel := context.WithTimeout(context.Background(), 5*time.Second)
|
||||||
defer dbCancel()
|
defer dbCancel()
|
||||||
|
|
||||||
// Update application status
|
startUpdate := time.Now()
|
||||||
_, err = db.ExecContext(dbCtx,
|
updateRes, err := db.ExecContext(dbCtx,
|
||||||
`UPDATE application SET online = $1 WHERE id = $2`,
|
`UPDATE application SET online = $1 WHERE id = $2`,
|
||||||
isOnline,
|
isOnline,
|
||||||
app.ID,
|
app.ID,
|
||||||
)
|
)
|
||||||
|
updateDuration := time.Since(startUpdate)
|
||||||
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
fmt.Printf("Update failed for app %d: %v\n", app.ID, err)
|
fmt.Printf("%s UPDATE failed after %v: %v\n", logPrefix, updateDuration, err)
|
||||||
|
} else {
|
||||||
|
affected, _ := updateRes.RowsAffected()
|
||||||
|
fmt.Printf("%s UPDATE OK (%d rows) after %v\n", logPrefix, affected, updateDuration)
|
||||||
}
|
}
|
||||||
|
|
||||||
// Insert into uptime_history
|
// History Insert
|
||||||
_, err = db.ExecContext(dbCtx,
|
startInsert := time.Now()
|
||||||
|
insertRes, err := db.ExecContext(dbCtx,
|
||||||
`INSERT INTO uptime_history ("applicationId", online, "createdAt") VALUES ($1, $2, now())`,
|
`INSERT INTO uptime_history ("applicationId", online, "createdAt") VALUES ($1, $2, now())`,
|
||||||
app.ID,
|
app.ID,
|
||||||
isOnline,
|
isOnline,
|
||||||
)
|
)
|
||||||
|
insertDuration := time.Since(startInsert)
|
||||||
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
fmt.Printf("Insert into uptime_history failed for app %d: %v\n", app.ID, err)
|
fmt.Printf("%s INSERT failed after %v: %v\n", logPrefix, insertDuration, err)
|
||||||
|
} else {
|
||||||
|
inserted, _ := insertRes.RowsAffected()
|
||||||
|
fmt.Printf("%s INSERT OK (%d rows) after %v\n", logPrefix, inserted, insertDuration)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user