fix: plugin events dispatch is now asynchronous (fixes #592)

This commit is contained in:
Simone Margaritelli 2019-11-13 00:11:50 +01:00
parent a5f9b9b2ee
commit 8fcfd4cafd
2 changed files with 7 additions and 6 deletions

View File

@ -12,9 +12,9 @@ API_ADDRESS = "http://127.0.0.1:8666/api/v1"
def is_connected(): def is_connected():
try: try:
socket.create_connection(("www.google.com", 80)) socket.create_connection(("api.pwnagotchi.ai", 443), timeout=30)
return True return True
except OSError: except:
pass pass
return False return False
@ -22,11 +22,11 @@ def is_connected():
def call(path, obj=None): def call(path, obj=None):
url = '%s%s' % (API_ADDRESS, path) url = '%s%s' % (API_ADDRESS, path)
if obj is None: if obj is None:
r = requests.get(url, headers=None) r = requests.get(url, headers=None, timeout=(30.0, 60.0))
elif isinstance(obj, dict): elif isinstance(obj, dict):
r = requests.post(url, headers=None, json=obj) r = requests.post(url, headers=None, json=obj, timeout=(30.0, 60.0))
else: else:
r = requests.post(url, headers=None, data=obj) r = requests.post(url, headers=None, data=obj, timeout=(30.0, 60.0))
if r.status_code != 200: if r.status_code != 200:
raise Exception("(status %d) %s" % (r.status_code, r.text)) raise Exception("(status %d) %s" % (r.status_code, r.text))

View File

@ -1,5 +1,6 @@
import os import os
import glob import glob
import _thread
import importlib, importlib.util import importlib, importlib.util
import logging import logging
@ -31,7 +32,7 @@ def one(plugin_name, event_name, *args, **kwargs):
callback = getattr(plugin, cb_name, None) callback = getattr(plugin, cb_name, None)
if callback is not None and callable(callback): if callback is not None and callable(callback):
try: try:
callback(*args, **kwargs) _thread.start_new_thread(callback, (*args, *kwargs))
except Exception as e: except Exception as e:
logging.error("error while running %s.%s : %s" % (plugin_name, cb_name, e)) logging.error("error while running %s.%s : %s" % (plugin_name, cb_name, e))
logging.error(e, exc_info=True) logging.error(e, exc_info=True)