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 import logging
from base64 import b64decode, b64encode from base64 import b64decode, b64encode
from tinydb import TinyDB, Query from tinydb import TinyDB, Query
import json
from tinydb.storages import MemoryStorage from tinydb.storages import MemoryStorage
bumper_users_var = contextvars.ContextVar("bumper_users", default=[]) bumper_users_var = contextvars.ContextVar("bumper_users", default=[])
@ -62,16 +63,24 @@ def os_db_path():
def db_get(): def db_get():
# Will create the database if it doesn't exist try:
db = TinyDB(db_file()) # Will create the database if it doesn't exist
db = TinyDB(db_file())
# Will create the tables if they don't exist # Will create the tables if they don't exist
db.table("users", cache_size=0) db.table("users", cache_size=0)
db.table("clients", cache_size=0) db.table("clients", cache_size=0)
db.table("bots", cache_size=0) db.table("bots", cache_size=0)
db.table("tokens", 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): class BumperUser(object):

View file

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

View file

@ -5,6 +5,7 @@ import bumper
import sys, socket import sys, socket
import time import time
import platform import platform
import asyncio
def main(): def main():
@ -59,22 +60,41 @@ def main():
# users.append(user1) # users.append(user1)
# bumper.bumper_users_var.set(users) # 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) # start xmpp server on port 5223 (sync)
xmpp_server.run(run_async=True) # Start in new thread xmpp_server.run(run_async=True) # Start in new thread
# start mqtt server on port 8883 (async) # 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) # 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 # 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 # 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: while True:
try: try: