summaryrefslogtreecommitdiffstats
path: root/doctor_tests/consumer/sample.py
blob: c7dcf4f9917f415693d66b6803446b8296ca00c9 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
##############################################################################
# Copyright (c) 2017 ZTE Corporation and others.
#
# All rights reserved. This program and the accompanying materials
# are made available under the terms of the Apache License, Version 2.0
# which accompanies this distribution, and is available at
# http://www.apache.org/licenses/LICENSE-2.0
##############################################################################
from flask import Flask
from flask import request
import json
import time
from threading import Thread
import requests

from doctor_tests.consumer.base import BaseConsumer


class SampleConsumer(BaseConsumer):

    def __init__(self, conf, log):
        super(SampleConsumer, self).__init__(conf, log)
        self.app = None

    def start(self):
        self.log.info('sample consumer start......')
        self.app = ConsumerApp(self.conf.consumer.port, self, self.log)
        self.app.start()

    def stop(self):
        self.log.info('sample consumer stop......')
        if not self.app:
            return
        headers = {
            'Content-Type': 'application/json',
            'Accept': 'application/json',
        }
        url = 'http://%s:%d/shutdown'\
              % (self.conf.consumer.ip,
                 self.conf.consumer.port)
        requests.post(url, data='', headers=headers)


class ConsumerApp(Thread):

    def __init__(self, port, consumer, log):
        Thread.__init__(self)
        self.port = port
        self.consumer = consumer
        self.log = log

    def run(self):
        app = Flask('consumer')

        @app.route('/failure', methods=['POST'])
        def event_posted():
            notified_time = time.time()
            self.log.info('doctor consumer notified at %s' % notified_time)
            self.consumer.notified_time = notified_time
            data = json.loads(request.data.decode('utf8'))
            self.log.info('sample consumer received data = %s' % data)
            return 'OK'

        @app.route('/shutdown', methods=['POST'])
        def shutdown():
            self.log.info('shutdown consumer app server at %s' % time.time())
            func = request.environ.get('werkzeug.server.shutdown')
            if func is None:
                raise RuntimeError('Not running with the Werkzeug Server')
            func()
            return 'consumer app shutting down...'

        app.run(host="0.0.0.0", port=self.port)