mirror of
https://github.com/crocofied/CoreControl.git
synced 2025-12-18 07:56:57 +00:00
uptime_history for agent
This commit is contained in:
parent
19ef051e1e
commit
3f1f7b730e
@ -34,19 +34,48 @@ func main() {
|
|||||||
}
|
}
|
||||||
defer db.Close()
|
defer db.Close()
|
||||||
|
|
||||||
ticker := time.NewTicker(5 * time.Second)
|
go func() {
|
||||||
|
deletionTicker := time.NewTicker(1 * time.Hour)
|
||||||
|
defer deletionTicker.Stop()
|
||||||
|
|
||||||
|
for range deletionTicker.C {
|
||||||
|
if err := deleteOldEntries(db); err != nil {
|
||||||
|
fmt.Printf("Error deleting old entries: %v\n", err)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}()
|
||||||
|
|
||||||
|
ticker := time.NewTicker(1 * time.Second)
|
||||||
defer ticker.Stop()
|
defer ticker.Stop()
|
||||||
|
|
||||||
client := &http.Client{
|
client := &http.Client{
|
||||||
Timeout: 4 * time.Second,
|
Timeout: 4 * time.Second,
|
||||||
}
|
}
|
||||||
|
|
||||||
for range ticker.C {
|
for now := range ticker.C {
|
||||||
|
if now.Second()%10 != 0 {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
|
||||||
apps := getApplications(db)
|
apps := getApplications(db)
|
||||||
checkAndUpdateStatus(db, client, apps)
|
checkAndUpdateStatus(db, client, apps)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func deleteOldEntries(db *sql.DB) error {
|
||||||
|
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
|
||||||
|
defer cancel()
|
||||||
|
|
||||||
|
res, err := db.ExecContext(ctx,
|
||||||
|
`DELETE FROM uptime_history WHERE "createdAt" < now() - interval '30 days'`)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
affected, _ := res.RowsAffected()
|
||||||
|
fmt.Printf("Deleted %d old entries from uptime_history\n", affected)
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
func getApplications(db *sql.DB) []Application {
|
func getApplications(db *sql.DB) []Application {
|
||||||
rows, err := db.Query(`
|
rows, err := db.Query(`
|
||||||
SELECT id, "publicURL", online
|
SELECT id, "publicURL", online
|
||||||
@ -90,12 +119,21 @@ func checkAndUpdateStatus(db *sql.DB, client *http.Client, apps []Application) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
_, err = db.ExecContext(ctx,
|
_, err = db.ExecContext(ctx,
|
||||||
"UPDATE application SET online = $1 WHERE id = $2",
|
`UPDATE application SET online = $1 WHERE id = $2`,
|
||||||
isOnline,
|
isOnline,
|
||||||
app.ID,
|
app.ID,
|
||||||
)
|
)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
fmt.Printf("Update failed for app %d: %v\n", app.ID, err)
|
fmt.Printf("Update failed for app %d: %v\n", app.ID, err)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
_, err = db.ExecContext(ctx,
|
||||||
|
`INSERT INTO uptime_history ("applicationId", online, "createdAt") VALUES ($1, $2, now())`,
|
||||||
|
app.ID,
|
||||||
|
isOnline,
|
||||||
|
)
|
||||||
|
if err != nil {
|
||||||
|
fmt.Printf("Insert into uptime_history failed for app %d: %v\n", app.ID, err)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user