From c62916c46e1cdfb0e04a9f17dd72ff7048924c99 Mon Sep 17 00:00:00 2001 From: Gibheer Date: Tue, 11 Dec 2018 09:25:45 +0100 Subject: [PATCH] monwork - make updates for nodes and commands This change triggers updates for checks when a node or command was updated. This way everything gets adjusted without much work. --- cmd/monwork/main.go | 54 +++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 54 insertions(+) diff --git a/cmd/monwork/main.go b/cmd/monwork/main.go index 392ab61..5c71a60 100644 --- a/cmd/monwork/main.go +++ b/cmd/monwork/main.go @@ -47,6 +47,8 @@ func main() { log.Fatalf("could not open database connection: %s", err) } + go startNodeGen(db, checkInterval) + go startCommandGen(db, checkInterval) go startConfigGen(db, checkInterval) // don't exit, we have work to do @@ -55,6 +57,58 @@ func main() { wg.Wait() } +func startNodeGen(db *sql.DB, checkInterval time.Duration) { + for { + tx, err := db.Begin() + if err != nil { + log.Printf("could not create transaction: %s", err) + time.Sleep(checkInterval) + continue + } + _, err = tx.Exec(`update checks c + set updated = n.updated + from nodes n + where c.node_id = n.id + and c.last_refresh < n.updated;`) + if err != nil { + log.Printf("could not update nodes: %s", err) + tx.Rollback() + time.Sleep(checkInterval) + } + if err := tx.Commit(); err != nil { + log.Printf("could not commit node updates: %s", err) + tx.Rollback() + } + time.Sleep(checkInterval) + } +} + +func startCommandGen(db *sql.DB, checkInterval time.Duration) { + for { + tx, err := db.Begin() + if err != nil { + log.Printf("could not create transaction: %s", err) + time.Sleep(checkInterval) + continue + } + _, err = tx.Exec(`update checks c + set updated = co.updated + from commands co + where c.command_id = co.id + and c.last_refresh < co.updated;`) + if err != nil { + log.Printf("could not update checks: %s", err) + tx.Rollback() + time.Sleep(checkInterval) + } + if err := tx.Commit(); err != nil { + log.Printf("could not commit command updates: %s", err) + tx.Rollback() + } + time.Sleep(checkInterval) + } +} + func startConfigGen(db *sql.DB, checkInterval time.Duration) { for { tx, err := db.Begin()