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:
parent
3f4b1b2421
commit
c62916c46e
@ -47,6 +47,8 @@ func main() {
|
|||||||
log.Fatalf("could not open database connection: %s", err)
|
log.Fatalf("could not open database connection: %s", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
go startNodeGen(db, checkInterval)
|
||||||
|
go startCommandGen(db, checkInterval)
|
||||||
go startConfigGen(db, checkInterval)
|
go startConfigGen(db, checkInterval)
|
||||||
|
|
||||||
// don't exit, we have work to do
|
// don't exit, we have work to do
|
||||||
@ -55,6 +57,58 @@ func main() {
|
|||||||
wg.Wait()
|
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) {
|
func startConfigGen(db *sql.DB, checkInterval time.Duration) {
|
||||||
for {
|
for {
|
||||||
tx, err := db.Begin()
|
tx, err := db.Begin()
|
||||||
|
Loading…
Reference in New Issue
Block a user