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