aboutsummaryrefslogtreecommitdiff
path: root/cmd/monwork/main.go
diff options
context:
space:
mode:
Diffstat (limited to 'cmd/monwork/main.go')
-rw-r--r--cmd/monwork/main.go54
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()