Esempio n. 1
0
 def get_addresses(self):
     launcher = SqsLauncher(self.service_discovery_address)
     launcher.launch_message({
         'command': 'hello',
         'node_name': self.node_id,
         'node_address': self.node_id
     })
Esempio n. 2
0
 def _send_read_response(self, client_id, key, value):
     launcher = SqsLauncher(client_id)
     launcher.launch_message({
         'command': 'read_response',
         'key': key,
         'value': value
     })
Esempio n. 3
0
 def _send_response(self, id):
     launcher = SqsLauncher(id['client_id'])
     launcher.launch_message({
         'command': 'write_response',
         'response': 'accepted',
         'id': id['time']
     })
Esempio n. 4
0
 def _send_proposal_accepted(self, key):
     launcher = SqsLauncher(self.coordinator_address)
     launcher.launch_message({
         'command': 'accept_to_coordinator',
         'key': key,
         'id': self.accepted[key]['proposed_id']
     })
Esempio n. 5
0
    def _start_listening(self):
        # TODO consider incorporating output processing from here: https://github.com/debrouwere/sqs-antenna/blob/master/antenna/__init__.py
        while True:
            messages = self._client.receive_message(
                QueueUrl=self._queue_url,
                MessageAttributeNames=self._message_attribute_names,
                AttributeNames=self._attribute_names,
            )
            if 'Messages' in messages:
                sqs_logger.info(
                    str(len(messages['Messages'])) + " messages received")
                for m in messages['Messages']:
                    receipt_handle = m['ReceiptHandle']
                    m_body = m['Body']
                    message_attribs = None
                    attribs = None

                    # catch problems with malformed JSON, usually a result of someone writing poor JSON directly in the AWS console
                    try:
                        params_dict = json.loads(m_body)
                    except:
                        sqs_logger.warning(
                            "Unable to parse message - JSON is not formatted properly"
                        )
                        continue
                    if 'MessageAttributes' in m:
                        message_attribs = m['MessageAttributes']
                    if 'Attributes' in m:
                        attribs = m['Attributes']
                    try:
                        if self._force_delete:
                            self._client.delete_message(
                                QueueUrl=self._queue_url,
                                ReceiptHandle=receipt_handle)
                            self.handle_message(params_dict, message_attribs,
                                                attribs, receipt_handle)
                        else:
                            self.handle_message(params_dict, message_attribs,
                                                attribs, receipt_handle)
                            self._client.delete_message(
                                QueueUrl=self._queue_url,
                                ReceiptHandle=receipt_handle)
                    except Exception as ex:
                        # need exception logtype to log stack trace
                        sqs_logger.exception(ex)
                        if self._error_queue_name:
                            exc_type, exc_obj, exc_tb = sys.exc_info()

                            sqs_logger.info("Pushing exception to error queue")
                            error_launcher = SqsLauncher(
                                self._error_queue_name, True)
                            error_launcher.launch_message({
                                'exception_type':
                                str(exc_type),
                                'error_message':
                                str(ex.args)
                            })

            else:
                time.sleep(self._poll_interval)
Esempio n. 6
0
 def _send_accepted(self, quorum):
     for node_address in self.nodes_addresses:
         launcher = SqsLauncher(node_address)
         launcher.launch_message({
             'command': 'accepted',
             'key': quorum['key'],
             'id': quorum['id']
         })
Esempio n. 7
0
 def _send_alive_msg(self):
     launcher = SqsLauncher(self.service_discovery_address)
     launcher.launch_message(({
         'command':
         'alive',
         'node_id':
         os.environ.get('NODE_ID', 'iosrFastPaxos_node1'),
         'coordinator':
         self.is_coordinator
     }))
Esempio n. 8
0
 def _handle_read(self, msg_body):
     key = msg_body['key']
     if key in self.database:
         launcher = SqsLauncher(self.coordinator_address)
         launcher.launch_message({
             'command': 'read_result',
             'id': msg_body['id'],
             'key': key,
             'value': self.database[key]
         })
Esempio n. 9
0
    def _start_listening(self):
        # TODO consider incorporating output processing from here: https://github.com/debrouwere/sqs-antenna/blob/master/antenna/__init__.py
        while True:
            messages = self._client.receive_message(
                QueueUrl=self._queue_url
            )
            if 'Messages' in messages:
                print str(len(messages['Messages'])) + " messages received"
                for m in messages['Messages']:
                    receipt_handle = m['ReceiptHandle']
                    m_body = m['Body']
                    message_attribs = None
                    attribs = None

                    # catch problems with malformed JSON, usually a result of someone writing poor JSON directly in the AWS console
                    try:
                        params_dict = json.loads(m_body)
                    except:
                        continue
                    if 'MessageAttributes' in m:
                        message_attribs = m['MessageAttributes']
                    if 'Attributes' in m:
                        attribs = m['Attributes']
                    try:
                        if self._force_delete:
                            self._client.delete_message(
                                QueueUrl=self._queue_url,
                                ReceiptHandle=receipt_handle
                            )
                            self.handle_message(params_dict, message_attribs, attribs)
                        else:
                            self.handle_message(params_dict, message_attribs, attribs)
                            self._client.delete_message(
                                QueueUrl=self._queue_url,
                                ReceiptHandle=receipt_handle
                            )
                    except Exception, ex:
                        print repr(ex)
                        if self._error_queue_name:
                            exc_type, exc_obj, exc_tb = sys.exc_info()

                            print "Pushing exception to error queue"
                            error_launcher = SqsLauncher(self._error_queue_name, True)
                            error_launcher.launch_message(
                                {
                                    'exception_type': str(exc_type),
                                    'error_message': str(ex.args)
                                }
                            )

            else:
                time.sleep(self._poll_interval)
Esempio n. 10
0
def get_sqs(node):
    return SqsLauncher(node)