예제 #1
0
파일: handlers.py 프로젝트: ilmiacs/pulsar
 def __iter__(self):
     request = self.request
     rpc = request['rpc']
     status_code = 200
     try:
         result = rpc.process(request)
     except Exception as e:
         result = as_failure(e)
     handler = rpc.handler
     result = maybe_async(result)
     while is_async(result):
         yield b''
         result = maybe_async(result)
     try:
         if is_failure(result):
             e = result.trace[1]
             status_code = getattr(e, 'status', 400)
             log_failure(result)
             result = handler.dumps(rpc.id, rpc.version, error=e)
         else:
             result = handler.dumps(rpc.id, rpc.version, result=result)
     except Exception as e:
         LOGGER.error('Could not serialize', exc_info=True)
         status_code = 500
         result = handler.dumps(rpc.id, rpc.version, error=e)
     content = to_bytes(result)
     response = WsgiResponse(status_code, content,
                             content_type=handler.content_type)
     for c in self.start(response):
         yield c
예제 #2
0
파일: __init__.py 프로젝트: japaks/pulsar
 def is_error(self):
     if self.status_code:
         return not is_succesful(self.status_code)
     elif self.on_finished.done():
         return is_failure(self.on_finished.result)
     else:
         return False
예제 #3
0
파일: deferred.py 프로젝트: cyberj/pulsar
 def testDeferredErrorbackInGenerator(self):
     d = Deferred()
     ioloop = IOLoop()
     # Add a callback which returns a deferred
     rd = Cbk()
     d.add_callback(lambda r : rd.add(r))
     d.add_callback(lambda r : r + ('second',))
     def _gen():
         yield d
     a = make_async(_gen())
     result = d.callback('ciao')
     self.assertTrue(d.called)
     self.assertEqual(d.paused, 1)
     # The generator has added its consume callback
     self.assertEqual(len(d._callbacks), 2)
     self.assertEqual(len(rd._callbacks), 1)
     #
     self.assertEqual(rd.r, ('ciao',))
     self.assertFalse(a.called)
     #
     # set Error back
     rd.set_error()
     self.assertFalse(d.paused)
     self.assertTrue(a.called)
     self.assertTrue(is_failure(d.result))
예제 #4
0
파일: __init__.py 프로젝트: cyberj/pulsar
 def fire(result, app):
     if not is_failure(result):
         result = app
     else:
         result.log()
     events.fire(name, app)
     app.events[name].callback(app)
예제 #5
0
파일: wsgi.py 프로젝트: ilmiacs/pulsar
def handle_wsgi_error(environ, trace=None, content_type=None, encoding=None):
    """The default handler for errors while serving an Http requests.

:parameter environ: The WSGI environment.
:parameter trace: the error traceback. If not avaiable it will be obtained from
    ``sys.exc_info()``.
:parameter content_type: Optional content type.
:parameter encoding: Optional charset.
:return: a :class:`WsgiResponse`
"""
    content_type = content_type or environ.get("CONTENT_TYPE")
    response = WsgiResponse(content_type=content_type, environ=environ, encoding=encoding)
    if not trace:
        trace = sys.exc_info()
    error = trace[1]
    content = None
    response.status_code = getattr(error, "status", 500)
    response.headers.update(getattr(error, "headers", None) or ())
    path = " @ path %s" % environ.get("PATH_INFO", "/")
    if response.status_code == 500:
        LOGGER.critical("Unhandled exception during WSGI response %s", path, exc_info=trace)
    else:
        LOGGER.info("WSGI %s status code %s", response.status_code, path)
    if has_empty_content(response.status_code) or response.status_code in REDIRECT_CODES:
        content = ()
        response.content_type = None
    else:
        renderer = environ.get("wsgi_error_handler")
        if renderer:
            try:
                content = renderer(environ, response, trace)
                if is_failure(content):
                    content.log()
                    content = None
            except:
                LOGGER.critical("Error while rendering error", exc_info=True)
                content = None
    if content is None:
        msg = error_messages.get(response.status_code) or response.response
        if response.content_type == "text/html":
            content = textwrap.dedent(
                """\
            <!DOCTYPE html>
            <html>
              <head>
                <title>{0[reason]}</title>
              </head>
              <body>
                <h1>{0[reason]}</h1>
                {0[msg]}
                <h3>{0[version]}</h3>
              </body>
            </html>
            """
            ).format({"reason": response.status, "msg": msg, "version": pulsar.SERVER_SOFTWARE})
        else:
            content = wsgi_error_msg(response, msg)
    response.content = content
    return response
예제 #6
0
    def render(self, request=None):
        '''A shortcut function for synchronously rendering a Content.
This is useful during testing. It is the synchronous equivalent of
:meth:`content`.'''
        value = maybe_async(self.content(request))
        if is_failure(value):
            value.throw()
        return value
예제 #7
0
    def render(self, request=None):
        '''A shortcut function for synchronously rendering a Content.
This is useful during testing. It is the synchronous equivalent of
:meth:`content`.'''
        value = maybe_async(self.content(request))
        if is_failure(value):
            value.throw()
        return value
예제 #8
0
파일: httpurl.py 프로젝트: cyberj/pulsar
 def testTooManyRedirects(self):
     http = self.client()
     r = safe_async(http.get, (self.httpbin('redirect', '5'),),
                             {'max_redirects': 2})
     # do this so that the test suite does not fail on the test
     yield r.addBoth(lambda f: [f])
     r = r.result[0]
     self.assertTrue(is_failure(r))
     self.assertTrue(isinstance(r.trace[1], httpurl.TooManyRedirects))
예제 #9
0
파일: rpc.py 프로젝트: pombredanne/pulsar
def task_to_json(task):
    if task:
        if pulsar.is_failure(task):
            err = task.trace[1]
            if isinstance(err, TaskNotAvailable):
                raise rpc.InvalidParams('Job "%s" is not available.' % err.task_name)
        if isinstance(task, (list, tuple)):
            task = [task_to_json(t) for t in task]
        elif isinstance(task, Task):
            task = task.tojson()
    return task
예제 #10
0
파일: rpc.py 프로젝트: japaks/pulsar
def task_to_json(task):
    if task:
        if pulsar.is_failure(task):
            err = task.trace[1]
            if isinstance(err, TaskNotAvailable):
                raise rpc.InvalidParams('Job "%s" is not available.' %
                                        err.task_name)
        if isinstance(task, (list, tuple)):
            task = [task_to_json(t) for t in task]
        elif isinstance(task, Task):
            task = task.tojson()
    return task
예제 #11
0
파일: case.py 프로젝트: cyberj/pulsar
 def add_failure(self, test, runner, failure):
     if is_failure(failure):
         for trace in failure:
             e = trace[1]
             try:
                 raise e
             except test.failureException:
                 runner.addFailure(test, trace)
             except:
                 runner.addError(test, trace)
             return True
     else:
         return False
예제 #12
0
파일: deferred.py 프로젝트: cyberj/pulsar
 def testNestedhandle(self):
     handle = lambda value : reduce(lambda x,y: x+y, value)\
                  if isinstance(value, list) else value 
     d = MultiDeferred(handle_value=handle)
     d.append((a for a in range(1,11)))
     r = maybe_async(d.lock())
     self.assertFalse(is_async(r))
     self.assertEqual(r, [55])
     handle = lambda value: 'c'*value
     d = MultiDeferred(handle_value=handle)
     d.append((a for a in range(1,11)))
     r = maybe_async(d.lock())
     self.assertFalse(is_async(r))
     self.assertTrue(is_failure(r[0]))
예제 #13
0
파일: __init__.py 프로젝트: ilmiacs/pulsar
 def as_frame(self, connection, body):
     '''Build a websocket server frame from body.'''
     body = maybe_async(body)
     if is_async(body):
         return body.addBoth(lambda b: self.as_frame(connection, b))
     if is_failure(body):
         # We have a failure. shut down connection
         body.log()
         body = Frame.close('Server error')
     elif not isinstance(body, Frame):
         # If the body is not a frame, build a final frame from it.
         body = Frame(body or '', version=connection.protocol.version,
                      final=True)
     return body.msg
예제 #14
0
 def process_request(self, request):
     from django.http import HttpResponse
     data = AttributeDictionary(request.__dict__)
     environ = data.pop('environ')
     environ['django.cache'] = data
     response = self._web_socket(environ, None)
     if response is not None:
         # Convert to django response
         if is_failure(response):
             response.throw()
         resp = HttpResponse(status=response.status_code,
                             content_type=response.content_type)
         for header, value in response.headers:
             resp[header] = value
         return resp
예제 #15
0
파일: views.py 프로젝트: xmnlab/minilab
 def process_request(self, request):
     from django.http import HttpResponse
     data = AttributeDictionary(request.__dict__)
     environ = data.pop('environ')
     environ['django.cache'] = data
     response = self._web_socket(environ, None)
     if response is not None:
         # Convert to django response
         if is_failure(response):
             response.throw()
         resp = HttpResponse(status=response.status_code,
                             content_type=response.content_type)
         for header, value in response.headers:
             resp[header] = value
         return resp
예제 #16
0
파일: manage.py 프로젝트: cyberj/pulsar
 def response_generator(self, response, wsgi_response):
     response = maybe_async(response)
     while is_async(response):
         yield b''
         response = maybe_async(response)
     stream_content = None
     if is_failure(response):
         wsgi_response.status_code = 500
     else:
         wsgi_response.status_code = response.status_code
         wsgi_response.headers.update(response.headers)
         stream_content = response.stream()
     wsgi_response.start()
     if stream_content:
         for content in stream_content:
             yield content
예제 #17
0
파일: task.py 프로젝트: zenweasel/pulsar
    def finish(self, worker, result):
        '''Called when the task has finished and a ``result`` is ready.
It sets the :attr:`time_end` attribute if not already set
(in case the :class:`Task` was revoked) and
determined if it was succesful or a failure. Once done it invokes
the :ref:`task callback <tasks-callbacks>` :meth:`on_finish`.'''
        if not self.time_end:
            self.time_end = datetime.now()
            if is_failure(result):
                result.log()
                exception = result.trace[1]
                self.status = getattr(exception, 'status', FAILURE)
                self.result = str(result) if result else str(exception)
            # If the status is STARTED this is a succesful task
            elif self.status == STARTED:
                self.status = SUCCESS
                self.result = result
        return self.on_finish(worker)
예제 #18
0
파일: wsgi.py 프로젝트: ilmiacs/pulsar
def wsgi_iterator(gen, encoding=None):
    encoding = encoding or "utf-8"
    for data in gen:
        data = maybe_async(data)
        while is_async(data):
            yield b""
            data = maybe_async(data)
        if data is NOT_DONE:
            yield b""
        elif data is None:
            continue
        elif is_failure(data):
            log_failure(data)
        else:
            if isinstance(data, bytes):
                yield data
            elif isinstance(data, string_type):
                yield data.encode(encoding)
            else:
                for b in wsgi_iterator(data, encoding):
                    yield b
예제 #19
0
파일: utils.py 프로젝트: cyberj/pulsar
 def _():
     if is_failure(value):
         value.raise_all()
예제 #20
0
파일: me.py 프로젝트: japaks/pulsar
 def test_unknown_send_target(self):
     # The target does not exists
     future = pulsar.send('vcghdvchdgcvshcd',
                          'ping').add_both(lambda r: [r])
     yield future
     self.assertTrue(is_failure(future.result[0]))
예제 #21
0
파일: iostream.py 프로젝트: cyberj/pulsar
        r = client.execute(msg)
        yield r
        self.assertEqual(r.result, msg)
        self.assertTrue(client.closed)
    
    def testConnectionClose(self):
        client = self.client(timeout=0, read_timeout=3)
        self.assertTrue(client.async)
        self.assertEqual(client.read_timeout, 3)
        future = client.execute(b'ciao')
        yield future
        self.assertEqual(future.result, b'ciao')
        future = client.execute(b'quit')
        yield future
        self.assertEqual(future.result, b'bye')
        try:
            future = client.execute(b'ciao')
        except IOError:
            pass
        else:
            future.add_errback(lambda res: (res,))
            yield future
            result = future.result[0]
            self.assertTrue(is_failure(result))
        
        
@dont_run_with_thread
class TestPulsarStreamsProcess(TestPulsarStreams):
    impl = 'process'