From ec3d7df81b915d29caa52bd7862683b7cbacd358 Mon Sep 17 00:00:00 2001 From: Matheus Mota Date: Tue, 12 Mar 2019 23:22:30 -0300 Subject: [PATCH 01/11] starting relationship between the relayer and broker MQTT --- docker-compose.yml | 81 +++++++++++++++++++++++++ modules/relayer_module/Dockerfile | 13 ++++ modules/relayer_module/requirements.txt | 2 + modules/relayer_module/src/server.py | 34 +++++++++++ 4 files changed, 130 insertions(+) create mode 100644 docker-compose.yml create mode 100644 modules/relayer_module/Dockerfile create mode 100644 modules/relayer_module/requirements.txt create mode 100644 modules/relayer_module/src/server.py diff --git a/docker-compose.yml b/docker-compose.yml new file mode 100644 index 0000000..ab03823 --- /dev/null +++ b/docker-compose.yml @@ -0,0 +1,81 @@ +version: '3' + +services: + + + broker: + image: eclipse-mosquitto + ports: + - 1883:1883 +# - 9001:9001 +# volumes: +# - broker_config:/mosquitto/config/ + restart: always + + + relayer: + build: ./modules/relayer_module + ports: + - 80:8080 + depends_on: + - broker + restart: always + + + + + # database: + # image: mysql:5.7 + # ports: + # - "3306:3306" + # volumes: + # - database:/var/lib/mysql + # environment: # will be replaced to .env vars, as in ${DB_PASSWORD} + # - MYSQL_DATABASE=jacinto-casemanager + # - MYSQL_USER=jacinto-casemanager + # - MYSQL_ALLOW_EMPTY_PASSWORD=yes + # - MYSQL_PASSWORD=jacinto + # - MYSQL_ROOT_PASSWORD=root + # restart: always + + + # database-ui: + # image: phpmyadmin/phpmyadmin:latest + # links: + # - database + # ports: + # - 80:80 + # environment: + # - PMA_ARBITRARY=0 # connection to any server + # - PMA_HOST=database + # - PMA_PORT=3306 + # depends_on: + # - database + + + # jacinto-casemanager: + # image: datasci4health/jacinto-casemanager + # ports: + # - "3333:3333" + # environment: + # - HOST=0.0.0.0 + # - PORT=3333 + # - NODE_ENV=production + # - APP_NAME=jacinto-casemanager + # - APP_URL=http://${HOST}:${PORT} + # - CACHE_VIEWS=false + # - APP_KEY=Xw8Ctuhsasew77qDx4GSnUlyTY3a6x + # - DB_CONNECTION=mysql + # - DB_HOST=127.0.0.1 + # - DB_PORT=3306 + # - DB_DATABASE=jacinto-casemanager + # - DB_USER=jacinto-casemanager + # - DB_PASSWORD=jacinto + # - HASH_DRIVER=bcrypt + # depends_on: + # - database + # restart: always + +# volumes: +# database: +# driver: "local" \ No newline at end of file diff --git a/modules/relayer_module/Dockerfile b/modules/relayer_module/Dockerfile new file mode 100644 index 0000000..aea4884 --- /dev/null +++ b/modules/relayer_module/Dockerfile @@ -0,0 +1,13 @@ +FROM ubuntu:18.04 + +RUN apt update +RUN apt install -y python3 python3-pip htop + +WORKDIR /usr/app + +ADD . . + +RUN pip3 install -r requirements.txt + + +CMD ["python3", "src/server.py"] \ No newline at end of file diff --git a/modules/relayer_module/requirements.txt b/modules/relayer_module/requirements.txt new file mode 100644 index 0000000..ced1bfe --- /dev/null +++ b/modules/relayer_module/requirements.txt @@ -0,0 +1,2 @@ +flask +paho-mqtt \ No newline at end of file diff --git a/modules/relayer_module/src/server.py b/modules/relayer_module/src/server.py new file mode 100644 index 0000000..fd723f0 --- /dev/null +++ b/modules/relayer_module/src/server.py @@ -0,0 +1,34 @@ +from flask import Flask, request +import paho.mqtt.client as paho +import random + +app = Flask(__name__) + +broker_host = "broker" +broker_port = 1883 + +broker = paho.Client("publisher{0}".format(random.randint(0,99999999)) ) +broker.connect(broker_host,broker_port) + +@app.route('/') +def index(): + return 'Harena Logger Relayer' + +@app.route('/publish', methods=['POST']) +def publish(): + + # payload = request.args.get('payload', '') + + message = request.get_json() + print(message) + topic = message['topic'] + payload = message['payload'] + + + broker.publish(topic,payload) + + return 'Message published successfully' + + +if __name__ == '__main__': + app.run(host="0.0.0.0", port=8080, debug=True) \ No newline at end of file From e7b00bf70575c7aa1eef499f117c626b1ce44b6c Mon Sep 17 00:00:00 2001 From: Marcos Mota Date: Wed, 13 Mar 2019 19:22:20 -0300 Subject: [PATCH 02/11] adding mongo image --- .gitignore | 3 +++ docker-compose.yml | 26 ++++++++++++------- modules/relayer/Dockerfile | 9 +++++++ modules/relayer/requirements.txt | 6 +++++ .../{relayer_module => relayer}/src/server.py | 0 modules/relayer_module/Dockerfile | 13 ---------- modules/relayer_module/requirements.txt | 2 -- 7 files changed, 35 insertions(+), 24 deletions(-) create mode 100644 modules/relayer/Dockerfile create mode 100644 modules/relayer/requirements.txt rename modules/{relayer_module => relayer}/src/server.py (100%) delete mode 100644 modules/relayer_module/Dockerfile delete mode 100644 modules/relayer_module/requirements.txt diff --git a/.gitignore b/.gitignore index 894a44c..6751d4d 100644 --- a/.gitignore +++ b/.gitignore @@ -102,3 +102,6 @@ venv.bak/ # mypy .mypy_cache/ + +#mongodb volume +data/ diff --git a/docker-compose.yml b/docker-compose.yml index ab03823..f28af72 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -2,27 +2,35 @@ version: '3' services: - broker: image: eclipse-mosquitto ports: - 1883:1883 # - 9001:9001 # volumes: -# - broker_config:/mosquitto/config/ +# - broker_config:/mosquitto/config/ restart: always - relayer: - build: ./modules/relayer_module + build: ./modules/relayer ports: - - 80:8080 + - 80:8080 depends_on: - broker + - mongodb restart: always - - + mongodb: + image: mongo:latest + container_name: "mongodb" + environment: + - MONGO_DATA_DIR=/data/db + - MONGO_LOG_DIR=/dev/null + volumes: + - ./data/db:/data/db + ports: + - 27017:27017 + command: mongod --smallfiles --logpath=/dev/null # --quiet # database: # image: mysql:5.7 @@ -46,7 +54,7 @@ services: # ports: # - 80:80 # environment: - # - PMA_ARBITRARY=0 # connection to any server + # - PMA_ARBITRARY=0 # connection to any server # - PMA_HOST=database # - PMA_PORT=3306 # depends_on: @@ -78,4 +86,4 @@ services: # volumes: # database: -# driver: "local" \ No newline at end of file +# driver: "local" diff --git a/modules/relayer/Dockerfile b/modules/relayer/Dockerfile new file mode 100644 index 0000000..d06d727 --- /dev/null +++ b/modules/relayer/Dockerfile @@ -0,0 +1,9 @@ +FROM python:3.7-alpine + +WORKDIR /app + +ADD . . + +RUN pip3 install -r requirements.txt + +CMD ["python3", "src/server.py"] diff --git a/modules/relayer/requirements.txt b/modules/relayer/requirements.txt new file mode 100644 index 0000000..231327e --- /dev/null +++ b/modules/relayer/requirements.txt @@ -0,0 +1,6 @@ +python-dotenv +flask +flask_restful +Flask-MongoAlchemy +flask-migrate +paho-mqtt diff --git a/modules/relayer_module/src/server.py b/modules/relayer/src/server.py similarity index 100% rename from modules/relayer_module/src/server.py rename to modules/relayer/src/server.py diff --git a/modules/relayer_module/Dockerfile b/modules/relayer_module/Dockerfile deleted file mode 100644 index aea4884..0000000 --- a/modules/relayer_module/Dockerfile +++ /dev/null @@ -1,13 +0,0 @@ -FROM ubuntu:18.04 - -RUN apt update -RUN apt install -y python3 python3-pip htop - -WORKDIR /usr/app - -ADD . . - -RUN pip3 install -r requirements.txt - - -CMD ["python3", "src/server.py"] \ No newline at end of file diff --git a/modules/relayer_module/requirements.txt b/modules/relayer_module/requirements.txt deleted file mode 100644 index ced1bfe..0000000 --- a/modules/relayer_module/requirements.txt +++ /dev/null @@ -1,2 +0,0 @@ -flask -paho-mqtt \ No newline at end of file From ba3dd4a07171b9301b47b1dca7a5e5df18d83c6b Mon Sep 17 00:00:00 2001 From: Marcos Mota Date: Wed, 13 Mar 2019 20:19:31 -0300 Subject: [PATCH 03/11] Inital script using flask-restful --- modules/relayer/src/server.py | 25 +++++++++++++------------ 1 file changed, 13 insertions(+), 12 deletions(-) diff --git a/modules/relayer/src/server.py b/modules/relayer/src/server.py index fd723f0..13c1fed 100644 --- a/modules/relayer/src/server.py +++ b/modules/relayer/src/server.py @@ -1,11 +1,13 @@ from flask import Flask, request +from flask_restful import Resource, Api import paho.mqtt.client as paho import random app = Flask(__name__) +api = Api(app) broker_host = "broker" -broker_port = 1883 +broker_port = 1883 broker = paho.Client("publisher{0}".format(random.randint(0,99999999)) ) broker.connect(broker_host,broker_port) @@ -14,21 +16,20 @@ def index(): return 'Harena Logger Relayer' -@app.route('/publish', methods=['POST']) -def publish(): +class Publisher(Resource): # payload = request.args.get('payload', '') + def post(self): + message = request.get_json() + print(message) + topic = message['topic'] + payload = message['payload'] - message = request.get_json() - print(message) - topic = message['topic'] - payload = message['payload'] + broker.publish(topic,payload) + return 'Message published successfully' - broker.publish(topic,payload) - - return 'Message published successfully' - +api.add_resource(Publisher, '/publish') if __name__ == '__main__': - app.run(host="0.0.0.0", port=8080, debug=True) \ No newline at end of file + app.run(host="0.0.0.0", port=8080, debug=True) From cf48d9f10ea4ba22a7b01c0c23a6b743b78a4cab Mon Sep 17 00:00:00 2001 From: Marcos Mota Date: Wed, 13 Mar 2019 21:05:12 -0300 Subject: [PATCH 04/11] debug flag off --- modules/relayer/src/server.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/modules/relayer/src/server.py b/modules/relayer/src/server.py index fd723f0..d7e523a 100644 --- a/modules/relayer/src/server.py +++ b/modules/relayer/src/server.py @@ -5,7 +5,7 @@ app = Flask(__name__) broker_host = "broker" -broker_port = 1883 +broker_port = 1883 broker = paho.Client("publisher{0}".format(random.randint(0,99999999)) ) broker.connect(broker_host,broker_port) @@ -25,10 +25,10 @@ def publish(): payload = message['payload'] - broker.publish(topic,payload) + broker.publish(topic,payload) return 'Message published successfully' if __name__ == '__main__': - app.run(host="0.0.0.0", port=8080, debug=True) \ No newline at end of file + app.run(host="0.0.0.0", port=8080) From fc22c3f88d99b67176231b9d0445f5c2f7c5bd30 Mon Sep 17 00:00:00 2001 From: Matheus Mota Date: Sun, 17 Mar 2019 20:28:36 -0300 Subject: [PATCH 05/11] adding support to anv var --- modules/relayer/src/server.py | 54 +++++++++++++++++++++++++---------- 1 file changed, 39 insertions(+), 15 deletions(-) diff --git a/modules/relayer/src/server.py b/modules/relayer/src/server.py index 9a65f10..7b26b01 100644 --- a/modules/relayer/src/server.py +++ b/modules/relayer/src/server.py @@ -1,3 +1,5 @@ +import os +import json from flask import Flask, request from flask_restful import Resource, Api import paho.mqtt.client as paho @@ -5,36 +7,58 @@ -broker_host = "broker" -broker_port = 1883 -broker = paho.Client("publisher{0}".format(random.randint(0,99999999)) ) -broker.connect(broker_host,broker_port) -@app.route('/') -def index(): - return 'Harena Logger Relayer' +class IndexResource(Resource): + + def __init__(self,broker): + print(broker) + self.broker = broker + + def get(self): + message = {"message": "Harena Logger Relayer", + "broker" : broker.__repr__() + } + return message + + class HarenaMessageResource(Resource): + def __init__(self,broker): + print(broker) + self.broker = broker + + def post(self): message = request.get_json() - print(message) + print(json.dumps(message)) topic = message['topic'] payload = message['payload'] - broker.publish(topic,payload) + self.broker.publish(topic,payload) return 'Message published successfully' if __name__ == '__main__': - web_app = Flask(__name__) + web_app = Flask(__name__) + api = Api(web_app) - api = Api(web_app) - - api.add_resource(HarenaMessageResource, '/message') - - web_app.run(host="0.0.0.0", port=8080) + config = {} + config['broker_host'] = os.environ.get('HARENA_LOGGER_BROKER_HOST', 'localhost') + config['broker_port'] = os.environ.get('HARENA_LOGGER_BROKER_PORT', 1883) + config['flask_host'] = os.environ.get('HARENA_LOGGER_FLASK_HOST', '0.0.0.0') + config['flask_port'] = os.environ.get('HARENA_LOGGER_FLASK_PORT', 5000) + config['flask_debug'] = bool(os.environ.get('HARENA_LOGGER_FLASK_DEBUG', 'False')) + + + broker = paho.Client("publisher{0}".format(random.randint(0,99999999)) ) + broker.connect(config['broker_host'],config['broker_port']) + + api.add_resource(IndexResource, '/', resource_class_args={'broker': broker}) + api.add_resource(HarenaMessageResource, '/message',resource_class_args={'broker': broker}) + + web_app.run(host=config['flask_host'], port=config['flask_port'],debug=config['flask_debug']) From 7dac5cabf38b183054834960f45f7c03ab47b486 Mon Sep 17 00:00:00 2001 From: Matheus Mota Date: Sun, 17 Mar 2019 22:32:24 -0300 Subject: [PATCH 06/11] adding aditional step for faster building (pip packages in exclusive fs layer) --- docker-compose.yml | 20 +++++++-- modules/relayer/Dockerfile | 4 +- modules/relayer/requirements.txt | 7 ++-- modules/relayer/src/server.py | 71 ++++++++++++++++++++++---------- 4 files changed, 74 insertions(+), 28 deletions(-) diff --git a/docker-compose.yml b/docker-compose.yml index f28af72..32f6bf2 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -13,8 +13,18 @@ services: relayer: build: ./modules/relayer + environment: + - HARENA_LOGGER_BROKER_HOST=broker + - HARENA_LOGGER_BROKER_PORT=1883 + - HARENA_LOGGER_FLASK_HOST=0.0.0.0 + - HARENA_LOGGER_FLASK_PORT=5000 + - HARENA_LOGGER_FLASK_DEBUG=False + - HARENA_LOGGER_MONGODB_HOST=mongodb + - HARENA_LOGGER_MONGODB_PORT=27017 + - HARENA_LOGGER_MONGODB_DB=harena_logger + - HARENA_LOGGER_MONGODB_COLLECTION=executions ports: - - 80:8080 + - 5000:5000 depends_on: - broker - mongodb @@ -27,10 +37,14 @@ services: - MONGO_DATA_DIR=/data/db - MONGO_LOG_DIR=/dev/null volumes: - - ./data/db:/data/db + - harena_logger_mongodb:/data/db ports: - 27017:27017 - command: mongod --smallfiles --logpath=/dev/null # --quiet + # command: mongod --smallfiles --logpath=/dev/null # --quiet + +volumes: + harena_logger_mongodb: + # database: # image: mysql:5.7 diff --git a/modules/relayer/Dockerfile b/modules/relayer/Dockerfile index d06d727..3da05b9 100644 --- a/modules/relayer/Dockerfile +++ b/modules/relayer/Dockerfile @@ -2,8 +2,10 @@ FROM python:3.7-alpine WORKDIR /app -ADD . . +COPY requirements.txt . RUN pip3 install -r requirements.txt +ADD . . + CMD ["python3", "src/server.py"] diff --git a/modules/relayer/requirements.txt b/modules/relayer/requirements.txt index 231327e..3f47e1b 100644 --- a/modules/relayer/requirements.txt +++ b/modules/relayer/requirements.txt @@ -1,6 +1,7 @@ -python-dotenv +#python-dotenv flask flask_restful -Flask-MongoAlchemy -flask-migrate +#Flask-MongoAlchemy +#flask-migrate paho-mqtt +pymongo \ No newline at end of file diff --git a/modules/relayer/src/server.py b/modules/relayer/src/server.py index 7b26b01..a2d2e67 100644 --- a/modules/relayer/src/server.py +++ b/modules/relayer/src/server.py @@ -1,19 +1,16 @@ import os import json -from flask import Flask, request +from flask import Flask, request, jsonify from flask_restful import Resource, Api import paho.mqtt.client as paho import random - - - - +import pymongo +import time class IndexResource(Resource): def __init__(self,broker): - print(broker) self.broker = broker def get(self): @@ -26,21 +23,44 @@ def get(self): class HarenaMessageResource(Resource): - def __init__(self,broker): - print(broker) + def __init__(self, broker, mongodb_collection): self.broker = broker + self.mongodb_collection = mongodb_collection def post(self): - message = request.get_json() - print(json.dumps(message)) - topic = message['topic'] - payload = message['payload'] + message = request.get_json() + print(json.dumps(message)) + topic = message['topic'] + payload = message['payload'] - self.broker.publish(topic,payload) + message['timestamp'] = "{}".format(int(round(time.time() * 1000))) - return 'Message published successfully' + broker_publishing_flag = self.broker.publish(topic,json.dumps(payload)) + mongodb_insertion_flag = self.mongodb_collection.insert(message) + + print("broker_publishing_flag: {}, mongodb_insertion_flag: {}".format(broker_publishing_flag,mongodb_insertion_flag)) + + return 'Message published successfully',201 + def get(self): + docs = self.mongodb_collection.find().sort([("timestamp", pymongo.DESCENDING)]) + + items=[] + + for doc in docs: + doc['_id'] = str(doc['_id']) + items.append(doc) + + return jsonify({'execution_stream':items}) + + def delete(self): + self.mongodb_collection.delete_many({}) + + return 'Messages in the execution stream deleted successfully' + + + if __name__ == '__main__': @@ -48,17 +68,26 @@ def post(self): api = Api(web_app) config = {} - config['broker_host'] = os.environ.get('HARENA_LOGGER_BROKER_HOST', 'localhost') - config['broker_port'] = os.environ.get('HARENA_LOGGER_BROKER_PORT', 1883) - config['flask_host'] = os.environ.get('HARENA_LOGGER_FLASK_HOST', '0.0.0.0') - config['flask_port'] = os.environ.get('HARENA_LOGGER_FLASK_PORT', 5000) - config['flask_debug'] = bool(os.environ.get('HARENA_LOGGER_FLASK_DEBUG', 'False')) + config['broker_host'] = os.environ.get('HARENA_LOGGER_BROKER_HOST', 'localhost') + config['broker_port'] = int(os.environ.get('HARENA_LOGGER_BROKER_PORT', 1883)) + + config['flask_host'] = os.environ.get('HARENA_LOGGER_FLASK_HOST', '0.0.0.0') + config['flask_port'] = int(os.environ.get('HARENA_LOGGER_FLASK_PORT', 5000)) + config['flask_debug'] = bool(os.environ.get('HARENA_LOGGER_FLASK_DEBUG', False)) + + config['mongodb_host'] = os.environ.get('HARENA_LOGGER_MONGODB_HOST', 'localhost') + config['mongodb_port'] = int(os.environ.get('HARENA_LOGGER_MONGODB_PORT', 27017)) + config['mongodb_db'] = os.environ.get('HARENA_LOGGER_MONGODB_DB', 'harena_logger') + config['mongodb_collection'] = os.environ.get('HARENA_LOGGER_MONGODB_COLLECTION', 'executions') + mongodb_client = pymongo.MongoClient("mongodb://{0}:{1}/".format(config['mongodb_host'],config['mongodb_port'])) + mongodb_db = mongodb_client[config['mongodb_db']] + mongodb_collection = mongodb_db[ config['mongodb_collection']] broker = paho.Client("publisher{0}".format(random.randint(0,99999999)) ) broker.connect(config['broker_host'],config['broker_port']) - api.add_resource(IndexResource, '/', resource_class_args={'broker': broker}) - api.add_resource(HarenaMessageResource, '/message',resource_class_args={'broker': broker}) + api.add_resource(IndexResource, '/', resource_class_args=[broker]) + api.add_resource(HarenaMessageResource, '/message',resource_class_args=[broker,mongodb_collection]) web_app.run(host=config['flask_host'], port=config['flask_port'],debug=config['flask_debug']) From f365c74bb9ae0f6371215bcb22339bfac68e80f0 Mon Sep 17 00:00:00 2001 From: Matheus Mota Date: Sun, 17 Mar 2019 22:35:31 -0300 Subject: [PATCH 07/11] adding route for collection delete --- modules/relayer/src/server.py | 2 ++ 1 file changed, 2 insertions(+) diff --git a/modules/relayer/src/server.py b/modules/relayer/src/server.py index a2d2e67..76af910 100644 --- a/modules/relayer/src/server.py +++ b/modules/relayer/src/server.py @@ -43,6 +43,7 @@ def post(self): return 'Message published successfully',201 + def get(self): docs = self.mongodb_collection.find().sort([("timestamp", pymongo.DESCENDING)]) @@ -54,6 +55,7 @@ def get(self): return jsonify({'execution_stream':items}) + def delete(self): self.mongodb_collection.delete_many({}) From f5da2a07336ef343111a826a322bb9f799895744 Mon Sep 17 00:00:00 2001 From: Matheus Mota Date: Sun, 17 Mar 2019 22:36:25 -0300 Subject: [PATCH 08/11] adding route for listing execution stream --- modules/relayer/src/server.py | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/modules/relayer/src/server.py b/modules/relayer/src/server.py index 76af910..4d45bff 100644 --- a/modules/relayer/src/server.py +++ b/modules/relayer/src/server.py @@ -39,15 +39,13 @@ def post(self): broker_publishing_flag = self.broker.publish(topic,json.dumps(payload)) mongodb_insertion_flag = self.mongodb_collection.insert(message) - print("broker_publishing_flag: {}, mongodb_insertion_flag: {}".format(broker_publishing_flag,mongodb_insertion_flag)) - return 'Message published successfully',201 - + def get(self): docs = self.mongodb_collection.find().sort([("timestamp", pymongo.DESCENDING)]) - items=[] + items = [] for doc in docs: doc['_id'] = str(doc['_id']) From d6396ffac189072dfad97d95fbe0eb96d55839b7 Mon Sep 17 00:00:00 2001 From: Matheus Mota Date: Sun, 17 Mar 2019 23:20:41 -0300 Subject: [PATCH 09/11] adding broker reconnect policy --- modules/relayer/src/server.py | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/modules/relayer/src/server.py b/modules/relayer/src/server.py index 4d45bff..c714d6f 100644 --- a/modules/relayer/src/server.py +++ b/modules/relayer/src/server.py @@ -10,12 +10,14 @@ class IndexResource(Resource): - def __init__(self,broker): + def __init__(self,broker,): self.broker = broker def get(self): - message = {"message": "Harena Logger Relayer", - "broker" : broker.__repr__() + message = {"message": "Welcome to the Harena Logger module", + "broker_type" : "MQTT", + "broker_status" : broker.__repr__() + } return message @@ -27,7 +29,6 @@ def __init__(self, broker, mongodb_collection): self.broker = broker self.mongodb_collection = mongodb_collection - def post(self): message = request.get_json() print(json.dumps(message)) @@ -40,7 +41,6 @@ def post(self): mongodb_insertion_flag = self.mongodb_collection.insert(message) return 'Message published successfully',201 - def get(self): docs = self.mongodb_collection.find().sort([("timestamp", pymongo.DESCENDING)]) @@ -53,7 +53,6 @@ def get(self): return jsonify({'execution_stream':items}) - def delete(self): self.mongodb_collection.delete_many({}) @@ -86,8 +85,9 @@ def delete(self): broker = paho.Client("publisher{0}".format(random.randint(0,99999999)) ) broker.connect(config['broker_host'],config['broker_port']) + broker.reconnect_delay_set(min_delay=1, max_delay=20) - api.add_resource(IndexResource, '/', resource_class_args=[broker]) + api.add_resource(IndexResource, '/', resource_class_args=[broker,mongodb_collection]) api.add_resource(HarenaMessageResource, '/message',resource_class_args=[broker,mongodb_collection]) web_app.run(host=config['flask_host'], port=config['flask_port'],debug=config['flask_debug']) From ca94b53706b35790a2a440bb1171015f2cef640e Mon Sep 17 00:00:00 2001 From: Matheus Mota Date: Sun, 17 Mar 2019 23:28:32 -0300 Subject: [PATCH 10/11] fixing index resource --- modules/relayer/src/server.py | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/modules/relayer/src/server.py b/modules/relayer/src/server.py index c714d6f..7393dff 100644 --- a/modules/relayer/src/server.py +++ b/modules/relayer/src/server.py @@ -10,13 +10,13 @@ class IndexResource(Resource): - def __init__(self,broker,): + def __init__(self,broker,mongodb_client): self.broker = broker def get(self): message = {"message": "Welcome to the Harena Logger module", - "broker_type" : "MQTT", - "broker_status" : broker.__repr__() + "broker_status" : broker.__repr__(), + "database_status":mongodb_client.server_info() } return message @@ -87,7 +87,7 @@ def delete(self): broker.connect(config['broker_host'],config['broker_port']) broker.reconnect_delay_set(min_delay=1, max_delay=20) - api.add_resource(IndexResource, '/', resource_class_args=[broker,mongodb_collection]) + api.add_resource(IndexResource, '/', resource_class_args=[broker,mongodb_client]) api.add_resource(HarenaMessageResource, '/message',resource_class_args=[broker,mongodb_collection]) web_app.run(host=config['flask_host'], port=config['flask_port'],debug=config['flask_debug']) From c21fca2426b851fb45579af50b89a77f9b708865 Mon Sep 17 00:00:00 2001 From: Matheus Mota Date: Sun, 17 Mar 2019 23:30:54 -0300 Subject: [PATCH 11/11] fixing database status indicator --- modules/relayer/src/server.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/modules/relayer/src/server.py b/modules/relayer/src/server.py index 7393dff..0a5679a 100644 --- a/modules/relayer/src/server.py +++ b/modules/relayer/src/server.py @@ -16,7 +16,7 @@ def __init__(self,broker,mongodb_client): def get(self): message = {"message": "Welcome to the Harena Logger module", "broker_status" : broker.__repr__(), - "database_status":mongodb_client.server_info() + "database_status":mongodb_client.server_info()['ok'] } return message