Exemplo n.º 1
0
 def run(self):
     logger.info('Starting DS amqp handler')
     self.oio = OIOXML()
     amqp = BlockingAMQP(on_message=self.on_message,
             host=config.amqp_host,
             user=config.amqp_user,
             password=config.amqp_password)
     try:
         amqp.start()
     except (KeyboardInterrupt, SystemExit) as ex:
         logger.exception(ex)
     except Exception as ex:
         logger.exception(ex)
         raise
Exemplo n.º 2
0
class DS(daemon.Daemon):

    def run(self):
        logger.info('Starting DS amqp handler')
        self.oio = OIOXML()
        amqp = BlockingAMQP(on_message=self.on_message,
                host=config.amqp_host,
                user=config.amqp_user,
                password=config.amqp_password)
        try:
            amqp.start()
        except (KeyboardInterrupt, SystemExit) as ex:
            logger.exception(ex)
        except Exception as ex:
            logger.exception(ex)
            raise



    def on_message(self, body):
        try:
            element = json.loads(body)
            xml_file = self.oio.create_element(element['invoice'], element['deptor'])
            if not element['dry_run']:
                self.do_ftp(xml_file, element['invoice']['key'])
            else:
                logger.info('Dry run {}'.format(element))
            xml_file.close()

            return json.dumps({ 'success': True })
        except Exception as ex:
            logger.exception(ex)
            return json.dumps({ 'success': False, 'message': str(ex) })

    #channel.basic_ack(method.delivery_tag)

    def do_ftp(self, xml_file, number):
        print config.ftp_server
        ftp = ftplib.FTP(config.ftp_server)
        ftp.login(config.ftp_user, config.ftp_password)

        ftp.cwd('invoice')
        ftp.storbinary('STOR {}.xml'.format(number), xml_file)
        logging.info('FTP uploaded file: {}'.format(number))
        ftp.close()