func (ev *HistoryEvent) persistEvent(c redis.Client) { // Get new ever-incrementing event id id, err := c.Incr(HISTORY_KEY) if err != nil { log.Err(err.Error()) return } // Persist to object internally ev.Id = id // New key historyKey := HISTORY_BASE + ":id:" + string(id) ev.HistoryKey = historyKey // Persist values to history key ev.PersistAtomicHistoryKV(c, "timestamp", []byte(ev.Timestamp.String())) // TODO: FIXME: More keys // Build additional indices... // 1. Master index. _, err = c.Zadd(HISTORY_LIST, float64(ev.Timestamp.Unix()), []byte(historyKey)) if err != nil { log.Err(err.Error()) } // 2. Date index log.Info("Logging to " + HISTORY_LIST + ":date:" + ev.Timestamp.Format("%Y-%m-%d")) _, err = c.Zadd(HISTORY_LIST+":date:"+ev.Timestamp.Format("%Y-%m-%d"), float64(ev.Timestamp.Unix()), []byte(historyKey)) if err != nil { log.Err(err.Error()) } // 3. Host index log.Info("Logging to " + HISTORY_LIST + ":host:" + ev.Host) _, err = c.Zadd(HISTORY_LIST+":host:"+ev.Host, float64(ev.Timestamp.Unix()), []byte(historyKey)) if err != nil { log.Err(err.Error()) } // 4. Check index log.Info("Logging to " + HISTORY_LIST + ":check:" + ev.Check) _, err = c.Zadd(HISTORY_LIST+":check:"+ev.Check, float64(ev.Timestamp.Unix()), []byte(historyKey)) if err != nil { log.Err(err.Error()) } }
// Attempt to use SETNX to assert that we have the control thread. // If this happens, we have to set the expiration time. func grabControlThread(c redis.Client) bool { val, err := c.Setnx(CONTROL_THREAD_LOCK, []byte(fmt.Sprint(*hostId))) if err == nil { if val { // New lock acquired, attempt to set expiry properly log.Info("grabControlThread: Acquired lock") c.Expire(CONTROL_THREAD_LOCK, CONTROL_THREAD_EXPIRY) return true } else { // Already there return false } } else { log.Err("grabControlThread: " + err.Error()) return true } log.Warning("grabControlThread: Should never get here, defaulting to false") return false }
func getContact(c redis.Client, name string) Contact { contact := Contact{} items, err := c.Hgetall(CONTACT_PREFIX + ":" + name) if err == nil { for j := 0; j < len(items)/2; j += 2 { k := string(items[j]) v := string(items[j+1]) switch k { case "name": { contact.Name = v } case "email": { contact.EmailAddress = v } } } } return contact }
func (ev *HistoryEvent) PersistAtomicHistoryKV(c redis.Client, k string, v []byte) { err := c.Hset(ev.HistoryKey, k, v) if err != nil { log.Err(fmt.Sprintf("Error persisting %s k %s v %s", ev.HistoryKey, k, v)) } }
func extendControlExpiry(c redis.Client) { c.Expire(CONTROL_THREAD_LOCK, CONTROL_THREAD_EXPIRY) }