Exemplo n.º 1
0
    def finish_auth(self, request):
        try:
            if not self.get_arguments(request, "service"):
                raise HandlerError("Missing argument 'service'", 400)

            service_name = self.get_argument(request, "service")
            try:
                service = self.get_service(service_name, request)
            except KeyError:
                raise HandlerError("Service '%s' unknown" % service_name, 404)

            client_name = self.get_client_name(request)

            authorization = yield service.finish_authorization(client_name=client_name, args=self.arguments(request))

            # Upon successful initial authz, enqueue the daemon for right away.
            if authorization.is_new and service.daemon_class.__name__ in daemons.DAEMONS and self.application.redis:
                try:
                    from twistedpyres import ResQ

                    resq = ResQ(redis=self.application.redis)

                    for method in service.daemon_class._recurring:
                        yield resq.enqueue_at(
                            (datetime.now() + timedelta(seconds=1)),
                            service.daemon_class,
                            {"sql": service.datastore.config, "environment": service.environment},
                            authorization.uuid,
                            method,
                            redis=self.application.redis,
                        )

                except Exception as e:
                    logging.error("Error trying to queue: %s" % e.message)
                    raise e
            self.set_header(request, "Location", "/auth/%s" % authorization.uuid)
            self.write(request, {"guid": authorization.uuid}, status=201)
            self.finish(request)

            # Set account timestamp after we return a guid since this may be an
            # expensive API call.
            if authorization.is_new:
                if "account_created_timestamp" in service._methods["GET"]:
                    try:
                        yield service.account_created_timestamp(authorization=authorization)
                    except Exception as e:
                        logging.error("Error getting account timestamp: %s" % e)

        except LightningError as exc:
            self.write(request, exc.error_msg, exc.code)
            self.finish(request)
        except HandlerError as exc:
            self.write(request, error_format(exc.message), exc.code)
            self.finish(request)
        except Exception as exc:
            logging.error(exc)
            self.write_error(request, exc)
            self.finish(request)
Exemplo n.º 2
0
    def start_pyres(self):
        # I don't think we actually need the scheduler in the tests. This is
        # likely a mistake.
        #self.scheduler = Popen(
        #    args=['pyres_scheduler', '--port', str(self.redis_port)],
        #    stdout=PIPE, stderr=STDOUT,
        #)

        # Needed because I can't seem to get the env parameter to Popen to work
        os.environ['PYTHONPATH'] = os.path.dirname(__file__) + '/../../..'
        self.worker = Popen(
            args=[
                '%s/bin/worker' % os.environ['PYTHONPATH'],
                '--server=localhost:%d' %(self.redis_port),
                '--queue=Service',
                '--log=INFO',
            ],
#            stdout=PIPE, stderr=STDOUT,
        )

        d = ResQ.connect(
            server=':'.join([
                'localhost',
                str(self.redis_port),
            ]),
        )
        def set_resq(resq):
            self.resq = resq
        d.addCallback(set_resq)

        return d
Exemplo n.º 3
0
#!/usr/bin/env python

import os
import sys
sys.path.append(os.path.dirname(__file__) + '/..')

from twistedpyres import ResQ
from twisted.internet import reactor

d = ResQ.connect()

if False:
    from lightning.service.loopback import LoopbackDaemon
    def enqueue(resq):
        return resq.enqueue(LoopbackDaemon, {
            'redis':{
                'host': 'localhost',
                'port': 6379,
            },
            'environment': 'local',
        }, 'token_1234')
    d.addCallback(enqueue)
else:
    from lightning.service.facebook import FacebookDaemon
    from lightning.service.instagram import InstagramDaemon
    from lightning.service.twitter import TwitterDaemon
    from lightning.service.loopback import LoopbackDaemon
    def enqueue(resq):
        return resq.enqueue(FacebookDaemon, {
            'sql':{
                'connection':'dsn=SQLServer;uid=fakeuser;pwd=fakepassword;database=LIGHTNING;driver={SQL Server Native Client 10.0}',