def run(self): import time from v2ex.service.fetcher_manager import FetcherManager while not self._stop_flag: time.sleep(1) nid = FetcherManager.get_task() ntf_token = NtfTokenDao.get_from_nid(nid) if ntf_token is None: FetcherManager.remove_task(nid) continue if self.should_check_update(ntf_token): if self.has_update(ntf_token): logging.debug("found update for %s", ntf_token.token) self.gcm_send(ntf_token.uid, gcm.send_notify) else: logging.debug("not found update for %s", ntf_token.token) ntf_token.last_fetch_time = func.now() db.get_session().commit() elif self.should_ping(ntf_token): self.gcm_send(ntf_token.uid, gcm.send_ping) ntf_token.last_ping_time = func.now() db.get_session().commit()
def update_token(cls, uid, old_token, new_token): gcm_token = None for token in cls.get_from_uid(uid): if token.token == old_token: gcm_token = token break if gcm_token is None: raise Exception("old token not found") gcm_token.token = new_token get_session().commit()
def add_gcm_token(cls, uid, token): db = get_session() for gcm_token in cls.get_from_uid(uid): if gcm_token.token == token: return gcm_token = GcmToken(uid=uid, token=token) db.add(gcm_token) db.commit()
def set_for_user(cls, uid, token): db = get_session() ntf_token = cls.get_from_uid(uid) if ntf_token is None: ntf_token = NtfToken(uid=uid, token=token, last_update_time=func.now()) db.add(ntf_token) else: ntf_token.token = token db.commit() return ntf_token.id
def del_gcm_token(cls, uid, old_token): gcm_token = None has_left = False for token in cls.get_from_uid(uid): if token.token == old_token: gcm_token = token # continue loop to find it has left continue has_left = True if gcm_token is not None: # if already found token, break loop break if gcm_token is None: return db = get_session() db.delete(gcm_token) if not has_left: NtfTokenDao.del_for_user(uid) db.commit() return
def get_from_uid(uid): """:rtype : Iterable[GcmToken]""" return get_session().query(GcmToken).filter(GcmToken.uid == uid)
def get_count(): return get_session().query(NtfToken).count()
def get_last_fetch_time(): return get_session().query().add_column(NtfToken.last_fetch_time).order_by(NtfToken.last_fetch_time.desc()).first()
def del_for_user(uid): get_session().query(NtfToken).filter(NtfToken.uid == uid).delete()
def get_from_uid(uid): """:rtype : None | NtfToken""" return get_session().query(NtfToken).filter(NtfToken.uid == uid).scalar()