예제 #1
0
    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()
예제 #2
0
    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()
예제 #3
0
    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()
예제 #4
0
    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
예제 #5
0
    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
예제 #6
0
    def get_from_uid(uid):
        """:rtype : Iterable[GcmToken]"""

        return get_session().query(GcmToken).filter(GcmToken.uid == uid)
예제 #7
0
 def get_count():
     return get_session().query(NtfToken).count()
예제 #8
0
 def get_last_fetch_time():
     return get_session().query().add_column(NtfToken.last_fetch_time).order_by(NtfToken.last_fetch_time.desc()).first()
예제 #9
0
 def del_for_user(uid):
     get_session().query(NtfToken).filter(NtfToken.uid == uid).delete()
예제 #10
0
    def get_from_uid(uid):
        """:rtype : None | NtfToken"""

        return get_session().query(NtfToken).filter(NtfToken.uid == uid).scalar()