Пример #1
0
    def example():
        job = Job(job_id="123")
        pickled_rv = dumps({'results': None})
        yield job.save()
        yield job.connection.hset(job.key, 'result', pickled_rv)
        res = yield job.result
        my_log.debug('Job result %s', res)

        user = GoogleUser(_userid="1",
                          _token="asdasdf",
                          _refreshtoken="refreshtoken",
                          _resource="unknownresource",
                          _pushtoken="",
                          _badgenumber="0",
                          _pushsound="asdfasdfas",
                          _jid='*****@*****.**')
        my_log.info('User:%s before saving' % (user))
        res = yield user.save()
        my_log.info('User %s: saved. Res %s' % (user, res))
        ret = yield API(user.userid).login(user)
        my_log.info('Login result %s. Sleeping 10 seconds', ret)
        yield sleep(5)
        my_log.info('Result login %s', ret)
        try:
            ret = yield API(user.userid, conf.MACHINEID).logout(user.userid)
        except XMPPUserNotLogged as e:
            my_log.error('Exception launch %s', e)
        try:
            ret = yield API(user.userid).logout(user.userid)
        except XMPPUserNotLogged as e:
            my_log.error('Exception launch %s', e)

        #Perform a timeout
        ret = yield API(user.userid, 'default').login(user)
Пример #2
0
    def post(self, key):
        self.constructor(key, login_arguments, metric=self.METRICS['post'])

        user = yield GoogleUser.load(key)
        user_to_logout = None

        pushtoken = self.args['_pushtoken']

        if user is None:
            if pushtoken:
                user_to_logout = yield GoogleUser.load(pushtoken=pushtoken)
        elif user.connected:
            if user.jid != self.args['_jid']:
                user_to_logout = user

        if not user_to_logout is None:
            if user is None:
                self.log.info('WEB_HANDLER_LOGOUT %s with the same pushtoken',
                              user_to_logout.userid)
            else:
                self.log.info('WEB_HANDLER_LOGOUT %s with other jid: %s->%s',
                              key, user.jid, self.args['_jid'])

            if user_to_logout.connected:
                yield API(key,
                          queue=user_to_logout.worker,
                          synchronous_call=True).logout(user_to_logout.userid)
            else:
                yield GoogleUser.remove(user_to_logout.userid)

            user = None

        try:
            response_data = {'success': False, 'reason': 'Already logged'}
            if user is None or not user.connected:
                if user and not user.connected:
                    yield GoogleUser.remove(user.userid, flush_messages=False)

                user = GoogleUser(_userid=key,
                                  _version=self.user_agent.version,
                                  _iosversion=self.user_agent.iosVersion,
                                  _hwmodel=self.user_agent.hwmodel,
                                  **self.args)
                yield API(key).login(user)
                response_data = {'success': True, 'reason': 'ok'}
        except XMPPUserAlreadyLogged:
            pass

        if self.FAKE_PURCHASE_ENABLED:
            self.perform_fakepurchase(userid=user.jid, deviceid=user.userid)

        response_data.update(
            dict(background_time=conf.XMPP_BACKGROUND_TIME,
                 resource_prefix=conf.XMPP_RESOURCE,
                 gtalk_priority=conf.XMPP_GTALK_PRIORITY,
                 ads_gift=self.FAKE_PURCHASE_ENABLED))
        self._response_json(response_data)
Пример #3
0
    def reconnectUsers(self):
        connected_users = yield GoogleUser.get_connected()
        total_users = len(connected_users)
        self.log.info('reconnectUsers reconnecting %s users', total_users)
        for i in xrange(total_users):
            data = connected_users[i]
            try:
                user = GoogleUser(**data)
                worker, user.worker = user.worker, user.userid
                yield user.save()

                #Enqueing in the next loop iteration of twisted event loop
                reactor.callLater(0, self.reloginUser, user, user.worker)
                self.log.info('[%s] Reconnecting %s/%s user(s)', user.userid,
                              i + 1, total_users)
            except Exception as e:
                self.log.err(
                    e, '[%s] Exception while reconnecting' % (data['_userid']))
Пример #4
0
 def disconnectAwayUsers(self):
     away_users = yield GoogleUser.get_away()
     away_users = [] if not away_users else away_users
     self.log.info('CHECKING_AWAY_USERS: %s', len(away_users))
     for data in away_users:
         try:
             user = GoogleUser(**data)
             API(user.userid, queue=user.worker).disconnect(user.userid)
             APNSAPI(user.userid).sendpush(message=u'{0} {1}'.format(
                 u'\ue252',
                 translate.TRANSLATORS[user.lang]._('disconnected')),
                                           token=user.pushtoken,
                                           badgenumber=user.badgenumber,
                                           sound='')
             self.DISCONNECT_AWAY_METRIC.add(1)
         except Exception as e:
             self.log.err(
                 e, '[%s] Exception disconnecting user' % (data['_userid']))
Пример #5
0
 def delete(self, key):
     self.constructor(key, metric=self.METRICS['delete'])
     user = yield GoogleUser.load(key)
     if user is None:
         raise cyclone.web.HTTPError(404)
     if user.connected:
         yield API(key, queue=user.worker).logout(key)
     else:
         yield user.remove(user.userid)
     self._response_json({'success': True, 'reason': 'ok'})
Пример #6
0
    def processDeathWorkers(self):
        #avoid process death workers when service is not running
        death_workers = yield Worker.getWorkers(
            Worker.redis_death_workers_keys) if self.running else []

        if death_workers:
            self.log.info('DEATH_WORKERS %s',
                          [worker.get('name') for worker in death_workers])
        for worker in death_workers:
            name = worker.get('name')
            if conf.DIST_QUEUE_LOGIN in worker.get('queues', []):
                connected_users = yield GoogleUser.get_connected(name)
                total_users = len(connected_users)
                self.log.info(
                    'Reconnecting %s connected user(s) of death worker %s',
                    total_users, name)
                last_user_index = total_users - 1
                for i in xrange(total_users):
                    try:
                        data = connected_users[i]
                        user = GoogleUser(**data)

                        #Update worker as userid to enqueue new jobs in user own queue
                        user.worker = user.userid
                        yield user.save()

                        #Get pending jobs
                        reactor.callLater(0, self.reloginUser, user, name,
                                          i == last_user_index)
                        self.log.info(
                            '[%s] Reconnecting %s/%s user(s) of worker %s',
                            user.userid, i + 1, total_users, name)
                    except Exception as e:
                        self.log.err(
                            e, '[%s] Exception while reconnecting' %
                            (data['_userid']))

                #Remove worker and queue when no users were assigned
                if total_users == 0:
                    yield self.removeWorker(name)
            else:
                yield self.removeWorker(name)
Пример #7
0
 def logout(self, userid):
     try:
         self.log.info('LOGOUT')
         running_client = KatooApp().getService(userid)
         if running_client is None:
             raise XMPPUserNotLogged('User %s is not running in current worker'%(userid))
         yield running_client.disconnect()
     except XMPPUserNotLogged:
         pass
     finally:
         yield GoogleUser.remove(userid)
Пример #8
0
    def get(self, key):
        self.constructor(key, metric=self.METRICS['get'])
        user = yield GoogleUser.load(key)
        if user is None or not user.connected:
            raise cyclone.web.HTTPError(404)

        response_data = {
            'success': True,
            'reason': 'ok',
            'resource_connected': not user.away
        }
        self._response_json(response_data)
Пример #9
0
    def put(self, key):
        self.constructor(key, contact_arguments, metric=self.METRICS['put'])
        user = yield GoogleUser.load(key)
        if user is None or not user.connected:
            raise cyclone.web.HTTPError(404)

        jid = self.args.pop('jid')
        try:
            yield API(key, queue=user.worker).update_contact(
                user.userid, jid, **self.args)
        except XMPPUserNotLogged as e:
            raise cyclone.web.HTTPError(500, str(e))
        self._response_json({'success': True, 'reason': 'ok'})
Пример #10
0
 def get(self, key):
     self.constructor(key, metric=self.METRICS['get'])
     user = yield GoogleUser.load(key)
     if user is None:
         raise cyclone.web.HTTPError(404)
     messages = yield GoogleMessage.getMessages(key)
     self._response_json({
         'current_time': Timer().isoformat(),
         'success': True,
         'messages': messages,
         'len': len(messages),
         'reason': 'ok',
         'connected': user.connected
     })
Пример #11
0
 def delete(self, key):
     self.constructor(key, metric=self.METRICS['delete'])
     user = yield GoogleUser.load(key)
     if user is None:
         raise cyclone.web.HTTPError(404)
     #Remove messages from database (pending to implement)
     #update badgenumber
     try:
         yield GoogleMessage.flushMessages(key)
         if user.connected:
             yield API(key,
                       queue=user.worker).update(key, **{'badgenumber': 0})
         self._response_json({'success': True, 'reason': 'ok'})
     except XMPPUserNotLogged as e:
         raise cyclone.web.HTTPError(500, str(e))
Пример #12
0
    def processBadAssignedWorkers(self):
        assigned_workers = yield GoogleUser.get_assigned_workers()

        running_workers = yield Worker.getWorkers(Worker.redis_workers_keys)
        running_workers = [
            worker.get('name') for worker in running_workers
            if not worker.get('name') is None
        ]

        death_workers = yield Worker.getWorkers(
            Worker.redis_death_workers_keys)
        death_workers = [
            worker.get('name') for worker in death_workers
            if not worker.get('name') is None
        ]

        registered_workers = set(running_workers + death_workers)
        assigned_workers = set(assigned_workers)
        bad_workers = assigned_workers.difference(registered_workers)

        if bad_workers:
            self.log.warning(
                'BAD_WORKERS %s are assigned to users. Running %s Death %s',
                bad_workers, len(running_workers), len(death_workers))
            for worker in bad_workers:
                bad_users = yield GoogleUser.get_connected(worker_name=worker)
                total_bad_users = len(bad_users)
                if total_bad_users > 0:
                    self.log.info(
                        'Reconnecting %s users assigned to bad worker %s',
                        total_bad_users, worker)
                last_user_index = total_bad_users - 1
                for i in xrange(total_bad_users):
                    try:
                        data = bad_users[i]
                        user = GoogleUser(**data)
                        user.worker = user.userid
                        yield user.save()

                        reactor.callLater(0, self.reloginUser, user, worker,
                                          i == last_user_index)
                        self.log.info(
                            '[%s] Reconnecting %s/%s user(s) of worker %s',
                            user.userid, i + 1, total_bad_users, worker)
                    except Exception as e:
                        self.log.err(
                            e, '[%s] Exception while reconnecting' %
                            (data['_userid']))

                #Remove worker and queue when no users were assigned
                if total_bad_users == 0:
                    yield self.removeWorker(worker)
Пример #13
0
    def put(self, key):
        self.constructor(key, update_arguments, metric=self.METRICS['put'])
        user = yield GoogleUser.load(key)
        if user is None or not user.connected:
            raise cyclone.web.HTTPError(404)
        try:
            yield API(key, queue=user.worker).update(
                key,
                _version=self.user_agent.version,
                _iosversion=self.user_agent.iosVersion,
                _hwmodel=self.user_agent.hwmodel,
                **self.args)

            self._response_json({
                'success': True,
                'reason': 'ok',
                'background_time': conf.XMPP_BACKGROUND_TIME,
                'resource_prefix': conf.XMPP_RESOURCE,
                'gtalk_priority': conf.XMPP_GTALK_PRIORITY,
                'ads_gift': self.FAKE_PURCHASE_ENABLED
            })
        except XMPPUserNotLogged as e:
            raise cyclone.web.HTTPError(500, str(e))
Пример #14
0
 def processOnMigrationUsers(self):
     onMigration_users = yield GoogleUser.get_onMigration()
     total_users = len(onMigration_users)
     if total_users > 0:
         self.log.info("ON_MIGRATION_USERS %s", total_users)
     now = datetime.utcnow()
     for data in onMigration_users:
         user = GoogleUser(**data)
         delta_time = now - user.onMigrationTime
         if delta_time.seconds < conf.XMPP_KEEP_ALIVE_TIME + 30:
             if 60 < delta_time.seconds < 70:
                 self.log.warning(
                     '[%s] USER_MIGRATION_STOPPED %s second(s) ago. Waiting to fix the problem with XMPP_KEEP_ALIVE. User state: %s',
                     user.userid, delta_time.seconds, user)
             continue
         self.log.warning(
             '[%s] USER_MIGRATION_STOPPED %s second(s) ago. Performing new relogin. User state: %s',
             user.userid, delta_time.seconds, user)
         user.worker = user.userid
         user.onMigrationTime = ''
         yield user.save()
         yield API(user.userid).relogin(user, pending_jobs=[])
Пример #15
0
            deferred_list.append(self.user.save())
            deferred_list.append(GoogleMessage.updateRemoveTime(self.user.userid, self.user.lastTimeConnected))
            deferred_list.append(GoogleRosterItem.remove(self.user.userid))
        return defer.DeferredList(deferred_list, consumeErrors=True)
    
    def __str__(self):
        return '<%s object at %s. name: %s>(user: %s)'%(self.__class__.__name__, hex(id(self)), self.name, self.user)

if __name__ == '__main__':
    import os
    from katoo.data import GoogleUser
    from wokkel_extensions import XMPPClient
    from katoo.utils.applog import getLogger, getLoggerAdapter
    from katoo.apns.api import KatooAPNSService
    from katoo.supervisor import XMPPKeepAliveSupervisor
    
    my_log = getLoggerAdapter(getLogger(__name__, level="INFO"), id='MYLOG')
    
    app = KatooApp().app
    KatooAPNSService().service.setServiceParent(app)
    KatooApp().start()
    XMPPKeepAliveSupervisor().setServiceParent(app)
    
    import twisted.python.log
    twisted.python.log.startLoggingWithObserver(KatooApp().log.emit)
    user = GoogleUser("1", _token=os.getenv('TOKEN'), _refreshtoken=os.getenv('REFRESHTOKEN'), _resource="asdfasdf", _pushtoken=os.getenv('PUSHTOKEN', None), _jid=os.getenv('JID'), _pushsound='cell1.aif', _favoritesound='cell7.aif', _away=True)
    my_log.info('Instance user: %s', user)
    xmppclient = XMPPGoogle(user, app)
    my_log.info('Instance xmppclient: %s', xmppclient)
    xmppclient.log.info("Instance XMPPGoogle %s. Instance ReauthXMPP %s Instance XMPPClient %s Instance GoogleUser %s", isinstance(xmppclient, XMPPGoogle), isinstance(xmppclient, ReauthXMPPClient), isinstance(xmppclient, XMPPClient), isinstance(xmppclient, GoogleUser))
    reactor.run()
Пример #16
0
 def _init_metrics_versions(self, name, key):
     versions = yield GoogleUser.get_distinct(key, spec={'_connected': True})
     self._versions[key] = dict([(version, Metric(name='%s.%s'%(name,version), value=None, unit='versions', source='MONGO')) for version in versions])