Network initialization for the Pushbullet backend should be done in the run method.

Network errors in the constructor cause the whole application to fail.
Better to do the initialization in the run method with retries.
This commit is contained in:
Fabio Manganiello 2020-02-17 00:46:33 +01:00
parent b83f6539a5
commit 30e51ee299

View file

@ -1,5 +1,6 @@
import json import json
import time import time
from typing import Optional
from platypush.backend import Backend from platypush.backend import Backend
from platypush.message.event.pushbullet import PushbulletEvent from platypush.message.event.pushbullet import PushbulletEvent
@ -24,34 +25,30 @@ class PushbulletBackend(Backend):
* **pushbullet.py** (``pip install git+https://github.com/rbrcsk/pushbullet.py``) * **pushbullet.py** (``pip install git+https://github.com/rbrcsk/pushbullet.py``)
""" """
def __init__(self, token, device='Platypush', proxy_host=None, def __init__(self, token: str, device: str = 'Platypush', proxy_host: Optional[str] = None,
proxy_port=None, **kwargs): proxy_port: Optional[int] = None, **kwargs):
""" """
:param token: Your Pushbullet API token, see :param token: Your Pushbullet API token, see https://docs.pushbullet.com/#authentication
https://docs.pushbullet.com/#authentication
:type token: str
:param device: Name of the virtual device for Platypush (default: Platypush) :param device: Name of the virtual device for Platypush (default: Platypush)
:type device: str
:param proxy_host: HTTP proxy host (default: None) :param proxy_host: HTTP proxy host (default: None)
:type proxy_host: str
:param proxy_port: HTTP proxy port (default: None) :param proxy_port: HTTP proxy port (default: None)
:type proxy_port: int
""" """
# noinspection PyPackageRequirements
from pushbullet import Pushbullet
super().__init__(**kwargs) super().__init__(**kwargs)
self.token = token self.token = token
self.device_name = device self.device_name = device
self.proxy_host = proxy_host self.proxy_host = proxy_host
self.proxy_port = proxy_port self.proxy_port = proxy_port
self.pb = Pushbullet(token) self.device = None
self.pb_device_id = None
self.pb = None
self.listener = None self.listener = None
def _initialize(self):
# noinspection PyPackageRequirements
from pushbullet import Pushbullet
self.pb = Pushbullet(self.token)
# noinspection PyBroadException # noinspection PyBroadException
try: try:
self.device = self.pb.get_device(self.device_name) self.device = self.pb.get_device(self.device_name)
@ -135,6 +132,16 @@ class PushbulletBackend(Backend):
# noinspection PyPackageRequirements # noinspection PyPackageRequirements
from pushbullet import Listener from pushbullet import Listener
super().run() super().run()
initialized = False
while not initialized:
try:
self._initialize()
initialized = True
except Exception as e:
self.logger.exception(e)
self.logger.error('Pushbullet initialization error: {}'.format(str(e)))
time.sleep(30)
self.logger.info('Initialized Pushbullet backend - device_id: {}' self.logger.info('Initialized Pushbullet backend - device_id: {}'
.format(self.device_name)) .format(self.device_name))