コード例 #1
0
// Fires up the EntityManager service.  Once started, the service will pull
// the latest content (documents and entities) from the configured ContentSource
// calculate some aggreate stats on them, and then make the stats available to
// this web app.
func startEntityManager(cfg AppConfig, contentSource server.ContentSource) *server.EntityManager {
	// Create and configure a new EntityManager object.
	entityManager := server.NewEntityManager(server.EntityManagerConfig{
		TimeRanges:    cfg.TimeRanges,
		ContentSource: contentSource,
	})

	now := unixtime.Now()

	// If it exists, load global content saved prior to previous app shutdown.
	dataDir := cfg.DataDir
	if server.FileExists(dataDir) {
		logger.Printf("Loading saved content from %v", dataDir)
		entityManager.LoadState(dataDir, now)
		entityManager.ContentDAO.Load(dataDir)
	} else {
		logger.Printf("Loading recent content directly from content source into content buffer.")
		entityManager.PreFill(now.Subtract(cfg.DataPreFetchWindow), now)
	}

	logger.Printf("Calculating entity stats for the first time...")
	entityManager.RefreshStats(now)

	refreshStatsLock = &sync.Mutex{}

	logger.Printf("Starting MemDb content polling loop.")
	go func() {
		// The show must go on.  Trap any panics, log them as an error, and then
		// continue to attempt to get more data from MemDb service.
		for {
			func() {
				defer func() {
					if r := recover(); r != nil {
						logger.Printf("ERROR: unexpected application error: \n%v\n\n", r)
						debug.PrintStack()
					}
				}()

				ticker := time.NewTicker(cfg.RefreshInterval)
				for _ = range ticker.C {
					refreshStatsLock.Lock()
					startTime := time.Now()
					entityManager.FetchMoreContent(unixtime.Now())
					logger.Printf("entityManager.FetchMoreContent() took %v", time.Since(startTime))
					refreshStatsLock.Unlock()
				}
			}()
		}
	}()

	return entityManager
}
コード例 #2
0
// Sets the 'LastLogin' timestamp to the current system time.
func (me *UserDb) SetLastLoginToNow(userId int) {
	for i, _ := range me.users {
		user := &me.users[i]
		if user.Id == userId {
			user.LastLogin = unixtime.Now()
		}
	}
}
コード例 #3
0
func TestSetLastLoginToNow(t *testing.T) {
	userId := 100
	userEmail := "*****@*****.**"
	userDb := UserDb{
		users: []User{
			User{Id: userId, Email: userEmail},
		},
	}

	userDb.SetLastLoginToNow(userId)
	user, userExists := userDb.GetUserByEmail(userEmail)
	assert.True(t, userExists)
	assert.Equal(t, unixtime.Now(), user.LastLogin)
}