def test_sync(self): with authenticated_user(self.client) as user: rv = self.client.get('/default/') timestamp = dt_from_timestamp(redis_connection.hget(LAST_ACTIVE_KEY, user.id)) sync_last_active_at() user_reloaded = User.query.filter(User.id==user.id).first() self.assertIn('active_at', user_reloaded.details) self.assertEqual(user_reloaded.active_at, timestamp)
def sync_last_active_at(): """ Update User model with the active_at timestamp from Redis. We first fetch all the user_ids to update, and then fetch the timestamp to minimize the time between fetching the value and updating the DB. This is because there might be a more recent update we skip otherwise. """ user_ids = redis_connection.hkeys(LAST_ACTIVE_KEY) for user_id in user_ids: timestamp = redis_connection.hget(LAST_ACTIVE_KEY, user_id) active_at = dt_from_timestamp(timestamp) user = User.query.filter(User.id == user_id).first() if user: user.active_at = active_at redis_connection.hdel(LAST_ACTIVE_KEY, user_id) db.session.commit()
def get(self, query_id): timestamp = self.executions.get(str(query_id)) if timestamp: timestamp = utils.dt_from_timestamp(timestamp) return timestamp