Exemple #1
0
    def _execute(self, job, timeout=None, extract=False):

        if not isinstance(job, Message):
            job = Message(**job)

        if timeout is None:
            timeout = self.timeout_max_overflow

        with self.lock:
            send(self.master, job.serialize())

            while True:
                try:
                    socks = dict(self.poller.poll(timeout))
                    break
                except zmq.ZMQError as e:
                    if e.errno != errno.EINTR:
                        raise

        if socks.get(self.master) == zmq.POLLIN:
            if extract:
                return extract_result(recv(self.master))
            else:
                return recv(self.master)

        raise TimeoutError(timeout / 1000)
Exemple #2
0
    def test_message(self):
        data = {'1': 2}
        msg = Message(**data)
        self.assertEquals(msg.serialize(), json.dumps(data))

        msg = Message.load_from_string(json.dumps(data))
        self.assertEquals(msg.serialize(), json.dumps(data))
Exemple #3
0
    def test_message(self):
        data = {'1': 2}
        msg = Message(**data)
        self.assertEquals(msg.serialize(), json.dumps(data))

        msg = Message.load_from_string(json.dumps(data))
        self.assertEquals(msg.serialize(), json.dumps(data))
Exemple #4
0
    def _handle_recv_back(self, msg):
        # do the message and send the result
        if self.debug:
            #logger.debug('Message received from the broker')
            target = timed()(self._handle_commands)
        else:
            target = self._handle_commands

        duration = -1

        try:
            res = target(Message.load_from_string(msg[0]))
            if self.debug:
                duration, res = res

            res = json.dumps(res)
            # we're working with strings
            if isinstance(res, unicode):
                res = res.encode('utf8')

        except Exception, e:
            exc_type, exc_value, exc_traceback = sys.exc_info()
            exc = traceback.format_tb(exc_traceback)
            exc.insert(0, str(e))
            res = {'error': {'agent_id': self.pid, 'error': '\n'.join(exc)}}
            logger.error(res)
Exemple #5
0
    def _handle_recv_back(self, msg):
        # do the message and send the result
        if self.debug:
            logger.debug('Message received')
            target = timed()(self.target)
        else:
            target = self.target

        duration = -1

        # results are sent with a PID:OK: or a PID:ERROR prefix
        try:
            with self.timer.run_message():
                res = target(Message.load_from_string(msg[0]))

            # did we timout ?
            if self.timer.timed_out:
                # let's dump the last
                for line in self.timer.last_dump:
                    logger.error(line)

            if self.debug:
                duration, res = res

            # we're working with strings
            if isinstance(res, unicode):
                res = res.encode('utf8')

            res = '%d:OK:%s' % (self.pid, res)
        except Exception, e:
            exc_type, exc_value, exc_traceback = sys.exc_info()
            exc = traceback.format_tb(exc_traceback)
            exc.insert(0, str(e))
            res = '%d:ERROR:%s' % (self.pid, '\n'.join(exc))
            logger.error(res)
Exemple #6
0
    def _handle_recv_back(self, msg):
        # do the message and send the result
        if self.debug:
            target = timed()(self._handle_commands)
        else:
            target = self._handle_commands

        duration = -1
        broker_id = msg[2]

        if len(msg) == 7:
            client_id = msg[4]
        else:
            client_id = None

        data = msg[-1]
        try:
            res = target(Message.load_from_string(data))
            if self.debug:
                duration, res = res

            res['hostname'] = get_hostname()
            res = json.dumps(res)
            # we're working with strings
            if isinstance(res, unicode):
                res = res.encode('utf8')

        except Exception, e:
            exc_type, exc_value, exc_traceback = sys.exc_info()
            exc = traceback.format_tb(exc_traceback)
            exc.insert(0, str(e))
            res = {'error': {'agent_id': self.pid, 'error': '\n'.join(exc)}}
            logger.error(res)
Exemple #7
0
    def _handle_recv_back(self, msg):
        # do the message and send the result
        if self.debug:
            #logger.debug('Message received from the broker')
            target = timed()(self._handle_commands)
        else:
            target = self._handle_commands

        duration = -1

        try:
            res = target(Message.load_from_string(msg[0]))
            if self.debug:
                duration, res = res

            res = json.dumps(res)
            # we're working with strings
            if isinstance(res, unicode):
                res = res.encode('utf8')

        except Exception, e:
            exc_type, exc_value, exc_traceback = sys.exc_info()
            exc = traceback.format_tb(exc_traceback)
            exc.insert(0, str(e))
            res = {'error': {'agent_id': self.pid, 'error': '\n'.join(exc)}}
            logger.error(res)
Exemple #8
0
    def _handle_recv_back(self, msg):
        # do the message and send the result
        if self.debug:
            target = timed()(self._handle_commands)
        else:
            target = self._handle_commands

        duration = -1
        broker_id = msg[2]

        if len(msg) == 7:
            client_id = msg[4]
        else:
            client_id = None

        data = msg[-1]
        try:
            res = target(Message.load_from_string(data))
            if self.debug:
                duration, res = res

            res['hostname'] = get_hostname()
            res = json.dumps(res)
            # we're working with strings
            if isinstance(res, unicode):
                res = res.encode('utf8')

        except Exception, e:
            exc_type, exc_value, exc_traceback = sys.exc_info()
            exc = traceback.format_tb(exc_traceback)
            exc.insert(0, str(e))
            res = {'error': {'agent_id': self.pid, 'error': '\n'.join(exc)}}
            logger.error(res)
Exemple #9
0
    def _execute(self, job, timeout=None):

        if not isinstance(job, Message):
            job = Message(**job)

        if timeout is None:
            timeout = self.timeout_max_overflow

        with self.lock:
            send(self.master, job.serialize())

            while True:
                try:
                    socks = dict(self.poller.poll(timeout))
                    break
                except zmq.ZMQError as e:
                    if e.errno != errno.EINTR:
                        raise

        if socks.get(self.master) == zmq.POLLIN:
            return json.loads(recv(self.master))

        raise TimeoutError(timeout)
Exemple #10
0
    def _execute(self, job, timeout=None):

        if not isinstance(job, Message):
            job = Message(**job)

        if timeout is None:
            timeout = self.timeout_max_overflow

        with self.lock:
            send(self.master, job.serialize())

            while True:
                try:
                    socks = dict(self.poller.poll(timeout))
                    break
                except zmq.ZMQError as e:
                    if e.errno != errno.EINTR:
                        raise

        if socks.get(self.master) == zmq.POLLIN:
            data = recv(self.master)
            return json.loads(data)

        raise TimeoutError(timeout)