diff options
author | Gibheer <gibheer+git@zero-knowledge.org> | 2018-12-11 09:25:45 +0100 |
---|---|---|
committer | Gibheer <gibheer+git@zero-knowledge.org> | 2018-12-11 09:25:45 +0100 |
commit | c62916c46e1cdfb0e04a9f17dd72ff7048924c99 (patch) | |
tree | f9757928158dfa9315aaa7a278e1ef00ddf40c75 /cmd | |
parent | 3f4b1b2421c0242520a5271c83331c2c96ba7c09 (diff) |
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.
Diffstat (limited to 'cmd')
-rw-r--r-- | cmd/monwork/main.go | 54 |
1 files changed, 54 insertions, 0 deletions
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() |