How to use the mitogen.core.CallError function in mitogen

To help you get started, we’ve selected a few mitogen examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github dw / mitogen / ansible_mitogen / helpers.py View on Github external
def _async_main(job_id, runner_name, kwargs):
    """
    Implementation for the thread that implements asynchronous module
    execution.
    """
    try:
        rc = run_module(runner_name, kwargs)
    except Exception, e:
        rc = mitogen.core.CallError(e)

    _result_by_job_id[job_id] = rc
github dw / mitogen / mitogen / core.py View on Github external
unpickler = _Unpickler(fp, **self.UNPICKLER_KWARGS)
            unpickler.find_global = self._find_global
            try:
                # Must occur off the broker thread.
                try:
                    obj = unpickler.load()
                except:
                    LOG.error('raw pickle was: %r', self.data)
                    raise
                self._unpickled = obj
            except (TypeError, ValueError):
                e = sys.exc_info()[1]
                raise StreamError('invalid message: %s', e)

        if throw:
            if isinstance(obj, CallError):
                raise obj

        return obj
github dw / mitogen / mitogen / debug.py View on Github external
def _handle_debug_msg(self, msg):
        try:
            method, args, kwargs = msg.unpickle()
            msg.reply(getattr(self, method)(*args, **kwargs))
        except Exception:
            e = sys.exc_info()[1]
            msg.reply(mitogen.core.CallError(e))
github dw / mitogen / mitogen / service.py View on Github external
msg.reply(mitogen.core.CallError(
                Error(self.context_mismatch_msg)
            ))
            return

        LOG.debug('Serving %r', path)

        # Response must arrive first so requestee can begin receive loop,
        # otherwise first ack won't arrive until all pending chunks were
        # delivered. In that case max BDP would always be 128KiB, aka. max
        # ~10Mbit/sec over a 100ms link.
        try:
            fp = open(path, 'rb', self.IO_SIZE)
            msg.reply(self._generate_stat(path))
        except IOError:
            msg.reply(mitogen.core.CallError(
                sys.exc_info()[1]
            ))
            return

        stream = self.router.stream_by_id(sender.context.context_id)
        state = self._state_by_stream.setdefault(stream, FileStreamState())
        state.lock.acquire()
        try:
            state.jobs.append((sender, fp))
            self._schedule_pending_unlocked(state)
        finally:
            state.lock.release()
github dw / mitogen / examples / select_loop.py View on Github external
msg = both_sel.get(timeout=60.0)
            except mitogen.core.TimeoutError:
                print("No update in 60 seconds, something's broke")
                break

            hostname = hostname_by_context_id[msg.src_id]

            if msg.receiver is status_recv:   # https://mitogen.readthedocs.io/en/stable/api.html#mitogen.core.Message.receiver
                # handle a status update
                print('Got status update from %s: %s' % (hostname, msg.unpickle()))
            elif msg.receiver is calls_sel:  # subselect
                # handle a function call result.
                try:
                    assert None == msg.unpickle()
                    print('Task succeeded on %s' % (hostname,))
                except mitogen.core.CallError as e:
                    print('Task failed on host %s: %s' % (hostname, e))

        if calls_sel:
            print('Some tasks did not complete.')
        else:
            print('All tasks completed.')
github dw / mitogen / mitogen / service.py View on Github external
def _validate(self, method_name, kwargs, msg):
        method = getattr(self.service, method_name, None)
        if method is None:
            raise mitogen.core.CallError('No such method: %r', method_name)

        policies = getattr(method, 'mitogen_service__policies', None)
        if not policies:
            raise mitogen.core.CallError('Method has no policies set.')

        if msg is not None:
            if not all(p.is_authorized(self.service, msg) for p in policies):
                raise mitogen.core.CallError(
                    self.unauthorized_msg,
                    method_name,
                    self.service.name()
                )

        required = getattr(method, 'mitogen_service__arg_spec', {})
        validate_arg_spec(required, kwargs)
github dw / mitogen / mitogen / error.py View on Github external
klass = type('CallError_' + type_name, bases, {
        'type_name': type_name,
    })
    _error_by_cls[cls] = klass
    print [klass, klass.__bases__]
    return klass



def for_type_name(type_name):
    cls = find_class_by_qualname(type_name)
    if cls:
        return for_type(cls)
    return mitogen.core.CallError

mitogen.core.CallError.for_type_name = staticmethod(for_type_name)


def match(target_cls):
    """
    Return a magic for use in :keyword:`except` statements that matches any
    :class:`mitogen.core.CallError` whose original exception type was
    `target_cls` or one of its base classes::

        try:
            context.call(func_raising_some_exc)
        except mitogen.error.match(ValueError) as e:
            # handle ValueError.
            pass

    :param type target_cls:
        Target class to match.
github dw / mitogen / mitogen / service.py View on Github external
def _validate(self, msg):
        tup = msg.unpickle(throw=False)
        if not (isinstance(tup, tuple) and
                len(tup) == 3 and
                isinstance(tup[0], mitogen.core.AnyTextType) and
                isinstance(tup[1], mitogen.core.AnyTextType) and
                isinstance(tup[2], dict)):
            raise mitogen.core.CallError('Invalid message format.')