package log import ( "sync" "time" "github.com/globalsign/mgo/bson" ) var scheduleCharacterUpdate = func() func() { var mutex sync.Mutex var scheduled bool return func() { mutex.Lock() if !scheduled { go func() { time.Sleep(time.Second * 60) // If another comes along in the next 2-3 seconds, it should schedule a new // round to avoid a character only appearing in half their logs. mutex.Lock() scheduled = false mutex.Unlock() UpdateAllCharacters() }() scheduled = true } mutex.Unlock() } }() // ScheduleCharacterUpdate schedules a full update within the minute. // Subsequent calls within that time will not schedule anything. Even // if the operation takes a few seconds at most, it need not be ran often. func ScheduleCharacterUpdate() { scheduleCharacterUpdate() } // UpdateCharacters is a shorthand for getting a log and updaing its characters func UpdateCharacters(logID string) error { log, err := FindID(logID) if err != nil { return err } return log.UpdateCharacters() } // UpdateAllCharacters updates character list on all logs. This should // be done if one or more characters failed to be added. func UpdateAllCharacters() (updated int, err error) { updated = 0 err = clearUnknownNicks() if err != nil { return } iter := iterLogs(bson.M{}, 0) err = iter.Err() if err != nil { return } log := Log{} for iter.Next(&log) { err = log.UpdateCharacters() if err != nil { return } updated++ } err = iter.Err() if err != nil { return } return }