forked from platypush/platypush
Always define msg so the function doesn't fail on exception
Reverted the previous retry logic for backend - it didn't really work This reverts commit4e0e4863a0
. This reverts commit964c7b5cf0
. This reverts commit6ce348365f
.
This commit is contained in:
parent
4e0e4863a0
commit
284e0638f8
28 changed files with 267 additions and 273 deletions
|
@ -7,7 +7,6 @@ import importlib
|
|||
import logging
|
||||
import sys
|
||||
import threading
|
||||
import time
|
||||
|
||||
from threading import Thread
|
||||
|
||||
|
@ -35,7 +34,6 @@ class Backend(Thread):
|
|||
"""
|
||||
|
||||
_default_response_timeout = 5
|
||||
_backend_reload_timeout = 10
|
||||
|
||||
def __init__(self, bus=None, **kwargs):
|
||||
"""
|
||||
|
@ -213,38 +211,9 @@ class Backend(Thread):
|
|||
redis.send_message(msg, queue_name=queue_name)
|
||||
|
||||
|
||||
def exec(self):
|
||||
""" Backend thread logic. To be implemented in the derived classes """
|
||||
raise RuntimeError('Backend class {} does not implement the exec() method'.
|
||||
format(self.__class__.__name__))
|
||||
|
||||
def run(self):
|
||||
""" Thread runner. It wraps the exec method in a retry block """
|
||||
|
||||
super().run()
|
||||
""" Starts the backend thread. To be implemented in the derived classes """
|
||||
self.thread_id = threading.get_ident()
|
||||
error = None
|
||||
|
||||
while not self.should_stop():
|
||||
try:
|
||||
self.exec()
|
||||
except Exception as e:
|
||||
error = e
|
||||
|
||||
if not self.should_stop():
|
||||
if error:
|
||||
self.logger.error(('Backend {} terminated with an exception, ' +
|
||||
'reloading in {} seconds').format(
|
||||
self.__class__.__name__,
|
||||
self._backend_reload_timeout))
|
||||
self.logger.exception(error)
|
||||
else:
|
||||
self.logger.warning(('Backend {} unexpectedly terminated, ' +
|
||||
'reloading in {} seconds').format(
|
||||
self.__class__.__name__,
|
||||
self._backend_reload_timeout))
|
||||
|
||||
time.sleep(self._backend_reload_timeout)
|
||||
|
||||
def on_stop(self):
|
||||
""" Callback invoked when the process stops """
|
||||
|
|
|
@ -110,7 +110,8 @@ class AssistantGoogleBackend(Backend):
|
|||
self.assistant.stop_conversation()
|
||||
|
||||
|
||||
def exec(self):
|
||||
def run(self):
|
||||
super().run()
|
||||
|
||||
with Assistant(self.credentials, self.device_model_id) as assistant:
|
||||
self.assistant = assistant
|
||||
|
|
|
@ -176,8 +176,9 @@ class AssistantGooglePushtotalkBackend(Backend):
|
|||
""" Speech recognized handler """
|
||||
self.bus.post(SpeechRecognizedEvent(phrase=speech))
|
||||
|
||||
def exec(self):
|
||||
def run(self):
|
||||
""" Backend executor """
|
||||
super().run()
|
||||
|
||||
with SampleAssistant(self.lang, self.device_model_id, self.device_id,
|
||||
self.conversation_stream,
|
||||
|
|
|
@ -72,7 +72,8 @@ class AssistantSnowboyBackend(Backend):
|
|||
self.bus.post(HotwordDetectedEvent(hotword=self.hotword))
|
||||
return callback
|
||||
|
||||
def exec(self):
|
||||
def run(self):
|
||||
super().run()
|
||||
self.detector.start(self.hotword_detected())
|
||||
|
||||
|
||||
|
|
|
@ -112,7 +112,8 @@ class ButtonFlicBackend(Backend):
|
|||
|
||||
return _f
|
||||
|
||||
def exec(self):
|
||||
def run(self):
|
||||
super().run()
|
||||
|
||||
self.client.handle_events()
|
||||
|
||||
|
|
|
@ -160,7 +160,8 @@ class CameraPiBackend(Backend):
|
|||
self.logger.warning('Failed to stop recording')
|
||||
self.logger.exception(e)
|
||||
|
||||
def exec(self):
|
||||
def run(self):
|
||||
super().run()
|
||||
|
||||
if not self.redis:
|
||||
self.redis = get_backend('redis')
|
||||
|
|
|
@ -377,7 +377,8 @@ class HttpBackend(Backend):
|
|||
loop.run_forever()
|
||||
|
||||
|
||||
def exec(self):
|
||||
def run(self):
|
||||
super().run()
|
||||
os.putenv('FLASK_APP', 'platypush')
|
||||
os.putenv('FLASK_ENV', 'production')
|
||||
self.logger.info('Initialized HTTP backend on port {}'.format(self.port))
|
||||
|
|
|
@ -72,7 +72,8 @@ class HttpPollBackend(Backend):
|
|||
self.requests.append(request)
|
||||
|
||||
|
||||
def exec(self):
|
||||
def run(self):
|
||||
super().run()
|
||||
|
||||
while not self.should_stop():
|
||||
for request in self.requests:
|
||||
|
|
|
@ -48,7 +48,8 @@ class InotifyBackend(Backend):
|
|||
|
||||
self.inotify_watch = None
|
||||
|
||||
def exec(self):
|
||||
def run(self):
|
||||
super().run()
|
||||
|
||||
self.inotify_watch = inotify.adapters.Inotify()
|
||||
for path in self.watch_paths:
|
||||
|
|
|
@ -29,7 +29,8 @@ class JoystickBackend(Backend):
|
|||
|
||||
self.device = device
|
||||
|
||||
def exec(self):
|
||||
def run(self):
|
||||
super().run()
|
||||
self.logger.info('Initialized joystick backend on device {}'.format(self.device))
|
||||
|
||||
while not self.should_stop():
|
||||
|
|
|
@ -81,7 +81,8 @@ class KafkaBackend(Backend):
|
|||
self.logger.warning('Exception occurred while closing Kafka connection')
|
||||
self.logger.exception(e)
|
||||
|
||||
def exec(self):
|
||||
def run(self):
|
||||
super().run()
|
||||
|
||||
self.consumer = KafkaConsumer(self.topic, bootstrap_servers=self.server)
|
||||
self.logger.info('Initialized kafka backend - server: {}, topic: {}'
|
||||
|
|
|
@ -61,7 +61,8 @@ class LocalBackend(Backend):
|
|||
return Message.build(msg) if len(msg) else None
|
||||
|
||||
|
||||
def exec(self):
|
||||
def run(self):
|
||||
super().run()
|
||||
self.logger.info('Initialized local backend on {} and {}'.
|
||||
format(self.request_fifo, self.response_fifo))
|
||||
|
||||
|
|
|
@ -102,7 +102,8 @@ class MidiBackend(Backend):
|
|||
return callback
|
||||
|
||||
|
||||
def exec(self):
|
||||
def run(self):
|
||||
super().run()
|
||||
|
||||
self.midi.open_port(self.port_number)
|
||||
self.logger.info('Initialized MIDI backend, listening for events on device {}'.
|
||||
|
|
|
@ -45,7 +45,7 @@ class MqttBackend(Backend):
|
|||
publisher.single(self.topic, str(msg), hostname=self.host, port=self.port)
|
||||
|
||||
|
||||
def exec(self):
|
||||
def run(self):
|
||||
def on_connect(client, userdata, flags, rc):
|
||||
client.subscribe(self.topic)
|
||||
|
||||
|
@ -57,6 +57,7 @@ class MqttBackend(Backend):
|
|||
self.logger.info('Received message on the MQTT backend: {}'.format(msg))
|
||||
self.on_message(msg)
|
||||
|
||||
super().run()
|
||||
client = mqtt.Client()
|
||||
client.on_connect = on_connect
|
||||
client.on_message = on_message
|
||||
|
|
|
@ -39,7 +39,8 @@ class MusicMpdBackend(Backend):
|
|||
self.poll_seconds = poll_seconds
|
||||
|
||||
|
||||
def exec(self):
|
||||
def run(self):
|
||||
super().run()
|
||||
|
||||
last_status = {}
|
||||
last_state = None
|
||||
|
|
|
@ -175,7 +175,8 @@ class PushbulletBackend(Backend):
|
|||
def on_stop(self):
|
||||
self.ws.close()
|
||||
|
||||
def exec(self):
|
||||
def run(self):
|
||||
super().run()
|
||||
|
||||
self._init_socket()
|
||||
self.logger.info('Initialized Pushbullet backend - device_id: {}'
|
||||
|
|
|
@ -31,21 +31,19 @@ class RedisBackend(Backend):
|
|||
|
||||
self.queue = queue
|
||||
self.redis_args = redis_args
|
||||
self.redis = None
|
||||
self.redis = Redis(**self.redis_args)
|
||||
|
||||
def _get_redis(self):
|
||||
return Redis(**self.redis_args)
|
||||
|
||||
def send_message(self, msg, queue_name=None):
|
||||
if queue_name:
|
||||
self._get_redis().rpush(queue_name, msg)
|
||||
self.redis.rpush(queue_name, msg)
|
||||
else:
|
||||
self._get_redis().rpush(self.queue, msg)
|
||||
self.redis.rpush(self.queue, msg)
|
||||
|
||||
|
||||
def get_message(self, queue_name=None):
|
||||
queue = queue_name or self.queue
|
||||
msg = self._get_redis().blpop(queue)[1].decode('utf-8')
|
||||
msg = self.redis.blpop(queue)[1].decode('utf-8')
|
||||
|
||||
try:
|
||||
msg = Message.build(json.loads(msg))
|
||||
|
@ -62,7 +60,8 @@ class RedisBackend(Backend):
|
|||
return msg
|
||||
|
||||
|
||||
def exec(self):
|
||||
def run(self):
|
||||
super().run()
|
||||
|
||||
self.logger.info('Initialized Redis backend on queue {} with arguments {}'.
|
||||
format(self.queue, self.redis_args))
|
||||
|
|
|
@ -50,7 +50,8 @@ class ScardBackend(Backend):
|
|||
self.cardtype = AnyCardType()
|
||||
|
||||
|
||||
def exec(self):
|
||||
def run(self):
|
||||
super().run()
|
||||
|
||||
self.logger.info('Initialized smart card reader backend - ATR filter: {}'.
|
||||
format(self.ATRs))
|
||||
|
|
|
@ -40,7 +40,8 @@ class SensorBackend(Backend):
|
|||
""" To be implemented in the derived classes """
|
||||
raise NotImplementedError('To be implemented in a derived class')
|
||||
|
||||
def exec(self):
|
||||
def run(self):
|
||||
super().run()
|
||||
self.logger.info('Initialized {} sensor backend'.format(self.__class__.__name__))
|
||||
|
||||
while not self.should_stop():
|
||||
|
|
|
@ -32,7 +32,9 @@ class SensorIrZeroborgBackend(Backend):
|
|||
self.logger.info('Initialized Zeroborg infrared sensor backend')
|
||||
|
||||
|
||||
def exec(self):
|
||||
def run(self):
|
||||
super().run()
|
||||
|
||||
while True:
|
||||
try:
|
||||
self.zb.GetIrMessage()
|
||||
|
|
|
@ -61,7 +61,9 @@ class SensorLeapBackend(Backend):
|
|||
self.position_tolerance = position_tolerance
|
||||
|
||||
|
||||
def exec(self):
|
||||
def run(self):
|
||||
super().run()
|
||||
|
||||
listener = LeapListener(position_ranges=self.position_ranges,
|
||||
position_tolerance=self.position_tolerance)
|
||||
|
||||
|
|
|
@ -90,7 +90,9 @@ class TcpBackend(Backend):
|
|||
|
||||
threading.Thread(target=_f_wrapper).run()
|
||||
|
||||
def exec(self):
|
||||
def run(self):
|
||||
super().run()
|
||||
|
||||
serv_sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
|
||||
serv_sock.bind((self.bind_address, self.port))
|
||||
|
||||
|
|
|
@ -27,7 +27,8 @@ class WeatherForecastBackend(Backend):
|
|||
def send_message(self, msg):
|
||||
pass
|
||||
|
||||
def exec(self):
|
||||
def run(self):
|
||||
super().run()
|
||||
weather = get_plugin('weather.forecast')
|
||||
self.logger.info('Initialized weather forecast backend')
|
||||
|
||||
|
|
|
@ -24,6 +24,8 @@ class RedisBus(Bus):
|
|||
|
||||
def get(self):
|
||||
""" Reads one message from the Redis queue """
|
||||
msg = None
|
||||
|
||||
try:
|
||||
msg = self.redis.blpop(self.redis_queue)
|
||||
if msg and msg[1]:
|
||||
|
|
|
@ -75,7 +75,7 @@ class AdafruitIoPlugin(Plugin):
|
|||
def _get_redis(self):
|
||||
from redis import Redis
|
||||
|
||||
redis_args = get_backend('redis').redis_args.copy()
|
||||
redis_args = get_backend('redis').redis_args
|
||||
redis_args['socket_timeout'] = 1
|
||||
return Redis(**redis_args)
|
||||
|
||||
|
|
|
@ -706,7 +706,7 @@ class LightHuePlugin(LightPlugin):
|
|||
self.animation_thread = None
|
||||
self.redis = None
|
||||
|
||||
redis_args = get_backend('redis').redis_args.copy()
|
||||
redis_args = get_backend('redis').redis_args
|
||||
redis_args['socket_timeout'] = transition_seconds
|
||||
self.redis = Redis(**redis_args)
|
||||
|
||||
|
|
Loading…
Reference in a new issue