def run(self): logging.info('[worker] worker started') while not self.stop: logging.debug('[worker] perform one loop') uri = db.mongodb_uri() conn = Connection(uri) users = conn.db['users'] current = int(time.time()) for user in users.find(): user_id = user.get('user_id') access_token = user.get('access_token') expires = user.get('expires') last_scan_time = user.get('last_scan_time', 0) if not access_token: continue if expires > 0 and expires < current: logging.debug('[worker] expires time smaller than current time') continue # logging.debug('[worker] last_scan_time={0}, current={1}'.format(last_scan_time, current)) # if last_scan_time > current: # logging.debug('[worker] last_scan_time:{0} greater than current time'.format(last_scan_time)) # continue logging.info('[worker] scan news feed for user_id: ' + user_id) feeds = fb_call('me/home', args={'access_token': access_token}) scan_feeds(feeds, access_token, last_scan_time) db.update_last_scan_time(user_id, int(time.time())) time.sleep(30)
def mongotest(): uri = db.mongodb_uri() conn = Connection(uri) coll = conn.db['ts'] coll.insert(dict(now=int(time.time()))) last_few = [str(x['now']) for x in coll.find(sort=[("_id", -1)], limit=10)] body = "\n".join(last_few) return Response(body, content_type="text/plain;charset=UTF-8")
def echo_users(): uri = db.mongodb_uri() conn = Connection(uri) users = conn.db['users'] body = '' for user in users.find(): body += user.get('user_id', 'no_user_id') + ', ' + user.get('access_token', 'no_access_token') + ', ' + str(user.get('expires', -1)) + ', ' + str(user.get('last_scan_time', -1)) + '\n' return Response(body, content_type="text/plain;charset=UTF-8")