2015-06-17 15:44:52 +00:00
package ICCC
import (
"fmt"
"github.com/SommerEngineering/Ocean/ICCC/Scheme"
"github.com/SommerEngineering/Ocean/Log"
LM "github.com/SommerEngineering/Ocean/Log/Meta"
"github.com/SommerEngineering/Ocean/Shutdown"
"gopkg.in/mgo.v2/bson"
"time"
)
2015-06-18 15:44:22 +00:00
// Internal function for the timer logic thread.
2015-06-17 15:44:52 +00:00
func cacheTimerLogic ( waiting bool ) {
2015-06-18 15:44:22 +00:00
// Case: This server goes down now.
2015-06-17 15:44:52 +00:00
if Shutdown . IsDown ( ) {
return
}
2015-06-18 15:44:22 +00:00
// Define the query and get the iterator:
2015-06-17 15:44:52 +00:00
lastCount := cacheListenerDatabase . Len ( )
selection := bson . D { { ` IsActive ` , true } }
entriesIterator := collectionListener . Find ( selection ) . Iter ( )
2015-06-18 15:44:22 +00:00
entry := Scheme . Listener { }
2015-06-17 15:44:52 +00:00
cacheListenerDatabaseLock . Lock ( )
2015-06-18 15:44:22 +00:00
// Re-init the cache:
2015-06-17 15:44:52 +00:00
cacheListenerDatabase . Init ( )
2015-06-18 15:44:22 +00:00
// Loop over all entries
2015-06-17 15:44:52 +00:00
for entriesIterator . Next ( & entry ) {
cacheListenerDatabase . PushBack ( entry )
}
cacheListenerDatabaseLock . Unlock ( )
Log . LogShort ( senderName , LM . CategorySYSTEM , LM . LevelINFO , LM . MessageNameEXECUTE , ` The listener cache was refreshed with the values from the database. ` , fmt . Sprintf ( ` last count=%d ` , lastCount ) , fmt . Sprintf ( ` new count=%d ` , cacheListenerDatabase . Len ( ) ) )
2015-06-18 15:44:22 +00:00
// In case, that this function runs at a thread, we want to wait:
2015-06-17 15:44:52 +00:00
if waiting {
nextDuration := time . Duration ( 5 ) * time . Minute
if cacheListenerDatabase . Len ( ) == 0 {
nextDuration = time . Duration ( 10 ) * time . Second
}
time . Sleep ( nextDuration )
}
}