1 ##############################################################################
2 # Copyright (c) 2017 ZTE Corporation and others.
4 # All rights reserved. This program and the accompanying materials
5 # are made available under the terms of the Apache License, Version 2.0
6 # which accompanies this distribution, and is available at
7 # http://www.apache.org/licenses/LICENSE-2.0
8 ##############################################################################
9 from flask import Flask
10 from flask import request
13 from threading import Thread
16 from doctor_tests.consumer.base import BaseConsumer
19 class SampleConsumer(BaseConsumer):
21 def __init__(self, conf, log):
22 super(SampleConsumer, self).__init__(conf, log)
26 self.log.info('sample consumer start......')
27 self.app = ConsumerApp(self.conf.consumer.port, self, self.log)
31 self.log.info('sample consumer stop......')
35 'Content-Type': 'application/json',
36 'Accept': 'application/json',
38 url = 'http://%s:%d/shutdown'\
39 % (self.conf.consumer.ip,
40 self.conf.consumer.port)
41 requests.post(url, data='', headers=headers)
44 class ConsumerApp(Thread):
46 def __init__(self, port, consumer, log):
49 self.consumer = consumer
53 app = Flask('consumer')
55 @app.route('/failure', methods=['POST'])
57 notified_time = time.time()
58 self.log.info('doctor consumer notified at %s' % notified_time)
59 self.consumer.notified_time = notified_time
60 data = json.loads(request.data.decode('utf8'))
61 self.log.info('sample consumer received data = %s' % data)
64 @app.route('/shutdown', methods=['POST'])
66 self.log.info('shutdown consumer app server at %s' % time.time())
67 func = request.environ.get('werkzeug.server.shutdown')
69 raise RuntimeError('Not running with the Werkzeug Server')
71 return 'consumer app shutting down...'
73 app.run(host="0.0.0.0", port=self.port)