retry for fga plugin
This commit is contained in:
parent
05f83d818b
commit
c6a995caee
1 changed files with 16 additions and 1 deletions
|
@ -1,21 +1,36 @@
|
||||||
# TODO: tidy up the fucking code
|
# TODO: tidy up the fucking code
|
||||||
import plugins
|
import plugins
|
||||||
import requests
|
import requests
|
||||||
|
import time
|
||||||
|
|
||||||
|
|
||||||
URI = 'http://fucking-great-advice.ru/api/random'
|
URI = 'http://fucking-great-advice.ru/api/random'
|
||||||
class PluginThread(plugins.PluginThreadCommon):
|
class PluginThread(plugins.PluginThreadCommon):
|
||||||
def __init__(self, config):
|
def __init__(self, config):
|
||||||
defaults = {'freq': 120}
|
defaults = {'freq': 120, 'retry': 3}
|
||||||
super(PluginThread, self).__init__(config, defaults)
|
super(PluginThread, self).__init__(config, defaults)
|
||||||
|
self.retry = False
|
||||||
|
|
||||||
|
|
||||||
def main(self):
|
def main(self):
|
||||||
try:
|
try:
|
||||||
req = requests.get(URI, timeout=2)
|
req = requests.get(URI, timeout=2)
|
||||||
advice = req.json()['text'] if req.status_code == 200 else 'N/A'
|
advice = req.json()['text'] if req.status_code == 200 else 'N/A'
|
||||||
|
self.retry = True
|
||||||
except requests.exceptions.Timeout:
|
except requests.exceptions.Timeout:
|
||||||
advice = 'N/A (timeout)'
|
advice = 'N/A (timeout)'
|
||||||
|
self.retry = False
|
||||||
except requests.exceptions.ConnectionError:
|
except requests.exceptions.ConnectionError:
|
||||||
advice = 'N/A (offline)'
|
advice = 'N/A (offline)'
|
||||||
|
self.retry = False
|
||||||
self.status['full_text'] = advice
|
self.status['full_text'] = advice
|
||||||
|
|
||||||
|
|
||||||
|
def run(self):
|
||||||
|
while True:
|
||||||
|
self.main()
|
||||||
|
if self.retry:
|
||||||
|
sleep_time = self.conf['retry']
|
||||||
|
else:
|
||||||
|
sleep_time = self.conf['freq']
|
||||||
|
time.sleep(sleep_time)
|
||||||
|
|
Loading…
Reference in a new issue