def receipt(): """ After PayBC verifies that the payment has been approved, it submits the payment details of the invoice that was paid. We'll save the payment info to VIPS and acknowledge receipt of payment. """ if request.method == 'POST': logging.info("receipt() invoked: {} | {}".format( request.remote_addr, request.get_data())) payload = request.json # invoke middleware business logic args = helper.middle_logic(rules.save_payment(), payload=payload, config=Config, writer=RabbitMQ(Config)) return args.get('response', make_response({'status': 'INCMP'}, 400))
self.writer = rabbit_writer logging.warning('*** writer initialized ***') def main(self): # start listening for messages on the WATCH_QUEUE # when a message arrives invoke the callback() self.listener.consume(self.config.WATCH_QUEUE, self.callback) def callback(self, ch, method, properties, body): logging.info('message received; callback invoked') # convert body (in bytes) to string message_dict = decode_message(body, self.config.ENCRYPT_KEY) helper.middle_logic(helper.get_listeners(business.process_ekt_events(), message_dict['event_type']), message=message_dict, config=self.config, writer=self.writer) # Regardless of whether the write above is successful we need to # acknowledge receipt of the message to RabbitMQ. This acknowledgement # deletes it from the queue so the logic above must have saved or # handled the message before we get here. ch.basic_ack(delivery_tag=method.delivery_tag) if __name__ == "__main__": Listener(Config(), RabbitMQ(Config()), RabbitMQ(Config())).main()
def before_request_function(): g.writer = RabbitMQ(Config())
from python.ingestor.config import Config from python.common.rabbitmq import RabbitMQ from flask import request, jsonify, Response, json from flask_api import FlaskAPI import logging application = FlaskAPI(__name__) logging.basicConfig(level=Config.LOG_LEVEL) logging.warning('*** ingestor initialized ***') rabbit_mq = RabbitMQ( Config.INGEST_USER, Config.INGEST_PASS, Config.RABBITMQ_URL, Config.LOG_LEVEL, Config.MAX_CONNECTION_RETRIES, Config.RETRY_DELAY) @application.route('/v1/publish/event', methods=["POST"]) def create(): if rabbit_mq.publish(Config.WRITE_QUEUE, json.dumps(request.json)): return jsonify(request.json), 200 else: return Response('Unavailable', 500, mimetype='application/json') if __name__ == "__main__": application.run(host='0.0.0.0')
:return: """ self.listener.consume(self.config.WATCH_QUEUE, self.callback) def callback(self, ch, method, properties, body): logging.info('message received; callback invoked') message_dict = decode_message(body, self.config.ENCRYPT_KEY) result = self.validator.validate(message_dict) logging.info("write to: " + result['queue']) if result['isSuccess']: # Validation SUCCESSFUL if self.writer.publish( result['queue'], encode_message(message_dict, self.config.ENCRYPT_KEY)): ch.basic_ack(delivery_tag=method.delivery_tag) else: # Validation FAILED message_with_errors = add_error_to_message(message_dict, result) if self.writer.publish( result['queue'], encode_message(message_with_errors, self.config.ENCRYPT_KEY)): ch.basic_ack(delivery_tag=method.delivery_tag) if __name__ == "__main__": Listener(Config(), Validate(Config()), RabbitMQ(Config()), RabbitMQ(Config())).main()
if result['isSuccessful']: # acknowledge that the message was written to the database # remove the message from RabbitMQs WRITE_WATCH_QUEUE ch.basic_ack(delivery_tag=method.delivery_tag) else: message_with_errors_appended = Helper.add_error_to_message(message_dict, result) if self.writer.publish(self.config.FAIL_QUEUE, json.dumps(message_with_errors_appended)): ch.basic_ack(delivery_tag=method.delivery_tag) if __name__ == "__main__": Listener( Config(), MsSQL(Config()), Mapper(Config()), RabbitMQ( Config.MQ_WRITER_USER, Config.MQ_WRITER_PASS, Config.RABBITMQ_URL, Config.LOG_LEVEL, Config.MAX_CONNECTION_RETRIES, Config.RETRY_DELAY), RabbitMQ( Config.MQ_WRITER_USER, Config.MQ_WRITER_PASS, Config.RABBITMQ_URL, Config.LOG_LEVEL, Config.MAX_CONNECTION_RETRIES, Config.RETRY_DELAY) ).main()
# convert body (in bytes) to string message = body.decode(self.config.RABBITMQ_MESSAGE_ENCODE) message_dict = json.loads(message) validation_result = self.validator.validate(message_dict) if validation_result['isSuccess']: # Validation was SUCCESSFUL logging.info("write to: " + self.config.VALID_QUEUE) if self.writer.publish(self.config.VALID_QUEUE, message): ch.basic_ack(delivery_tag=method.delivery_tag) else: # Validation FAILED message_with_errors_appended = Helper.add_error_to_message( message_dict, validation_result['description']) logging.info("write to: " + self.config.FAIL_QUEUE) if self.writer.publish(self.config.FAIL_QUEUE, json.dumps(message_with_errors_appended)): ch.basic_ack(delivery_tag=method.delivery_tag) if __name__ == "__main__": Listener( Config(), Validate(Config()), RabbitMQ(Config.VALIDATOR_USER, Config.VALIDATOR_PASS, Config.RABBITMQ_URL, Config.LOG_LEVEL, Config.MAX_CONNECTION_RETRIES, Config.RETRY_DELAY), RabbitMQ(Config.VALIDATOR_USER, Config.VALIDATOR_PASS, Config.RABBITMQ_URL, Config.LOG_LEVEL, Config.MAX_CONNECTION_RETRIES, Config.RETRY_DELAY)).main()