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 commit 4e0e4863a0.
This reverts commit 964c7b5cf0.
This reverts commit 6ce348365f.
This commit is contained in:
Fabio Manganiello 2018-10-25 20:45:58 +02:00
parent 4e0e4863a0
commit 284e0638f8
28 changed files with 267 additions and 273 deletions

View file

@ -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 """

View file

@ -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

View file

@ -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,

View file

@ -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())

View file

@ -112,7 +112,8 @@ class ButtonFlicBackend(Backend):
return _f
def exec(self):
def run(self):
super().run()
self.client.handle_events()

View file

@ -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')

View file

@ -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))

View file

@ -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:

View file

@ -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:

View file

@ -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():

View file

@ -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: {}'

View file

@ -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))

View file

@ -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 {}'.

View file

@ -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

View file

@ -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

View file

@ -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: {}'

View file

@ -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))

View file

@ -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))

View file

@ -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():

View file

@ -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()

View file

@ -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)

View file

@ -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))

View file

@ -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')

View file

@ -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]:

View file

@ -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)

View file

@ -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)