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.
This commit is contained in:
Gibheer 2018-12-11 09:25:45 +01:00
parent 3f4b1b2421
commit c62916c46e

View File

@ -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()