rearrange start to use asyncio

Create an asyncio loop then start confservers, mqtt, and mqtthelper in loop.

TODO: Re-work xmpp for asyncio
This commit is contained in:
Brian Martin 2019-03-21 21:19:29 -04:00
parent d5449961f3
commit a296f038a7
3 changed files with 48 additions and 13 deletions

View file

@ -13,6 +13,7 @@ import os
import logging
from base64 import b64decode, b64encode
from tinydb import TinyDB, Query
import json
from tinydb.storages import MemoryStorage
bumper_users_var = contextvars.ContextVar("bumper_users", default=[])
@ -62,16 +63,24 @@ def os_db_path():
def db_get():
# Will create the database if it doesn't exist
db = TinyDB(db_file())
try:
# Will create the database if it doesn't exist
db = TinyDB(db_file())
# Will create the tables if they don't exist
db.table("users", cache_size=0)
db.table("clients", cache_size=0)
db.table("bots", cache_size=0)
db.table("tokens", cache_size=0)
# Will create the tables if they don't exist
db.table("users", cache_size=0)
db.table("clients", cache_size=0)
db.table("bots", cache_size=0)
db.table("tokens", cache_size=0)
return db
return db
except json.decoder.JSONDecodeError as jerr:
bumperlog.error("JsonErr: {} - Doc: {}".format(jerr.msg, jerr.doc))
except Exception as ex:
bumperlog.error(ex)
class BumperUser(object):

View file

@ -73,6 +73,10 @@ class MQTTHelperBot:
async def start_helper_bot(self):
try:
self.Client = MQTTClient(
client_id=self.client_id, config={"check_hostname": False}
)
await self.Client.connect(
"mqtts://{}:{}/".format(self.address[0], self.address[1]),
cafile=bumper.ca_cert,
@ -84,6 +88,8 @@ class MQTTHelperBot:
]
)
asyncio.ensure_future(self.get_msg())
except Exception as e:
helperbotlog.exception("{}".format(e))

View file

@ -5,6 +5,7 @@ import bumper
import sys, socket
import time
import platform
import asyncio
def main():
@ -59,22 +60,41 @@ def main():
# users.append(user1)
# bumper.bumper_users_var.set(users)
try:
loop = asyncio.get_event_loop()
except:
loop = asyncio.new_event_loop()
# Start web servers
conf_server.confserver_app()
conf_server_2.confserver_app()
asyncio.ensure_future(conf_server.start_server(),loop=loop)
asyncio.ensure_future(conf_server_2.start_server(),loop=loop)
# Start MQTT Server
asyncio.ensure_future(mqtt_server.broker_coro())
# Start MQTT Helperbot
asyncio.ensure_future(mqtt_helperbot.start_helper_bot())
loop.run_forever()
# start xmpp server on port 5223 (sync)
xmpp_server.run(run_async=True) # Start in new thread
# start mqtt server on port 8883 (async)
mqtt_server.run(run_async=True) # Start in new thread
#mqtt_server.run(run_async=True) # Start in new thread
time.sleep(1.5) # Wait for broker startup
#time.sleep(1.5) # Wait for broker startup
# start mqtt_helperbot (async)
mqtt_helperbot.run(run_async=True) # Start in new thread
#mqtt_helperbot.run(run_async=True) # Start in new thread
# start conf server on port 443 (async) - Used for most https calls
conf_server.run(run_async=True) # Start in new thread
#conf_server.run(run_async=True) # Start in new thread
# start conf server on port 8007 (async) - Used for a load balancer request
conf_server_2.run(run_async=True) # Start in new thread
#conf_server_2.run(run_async=True) # Start in new thread
while True:
try: