From a4469bd727c005d1ae5f673ebbd3c58c2dfda9ca Mon Sep 17 00:00:00 2001 From: Bob Mottram Date: Sun, 4 Oct 2020 21:21:50 +0100 Subject: [PATCH] Watchdog for updating rss feeds --- daemon.py | 14 ++++++++++++++ newswire.py | 49 +++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 63 insertions(+) diff --git a/daemon.py b/daemon.py index dabaefb5..9d3cb6be 100644 --- a/daemon.py +++ b/daemon.py @@ -204,6 +204,8 @@ from devices import E2EEdevicesCollection from devices import E2EEvalidDevice from devices import E2EEaddDevice from newswire import getRSSfromDict +from newswire import runNewswireWatchdog +from newswire import runNewswireDaemon import os @@ -11251,11 +11253,17 @@ def runDaemon(blogsInstance: bool, mediaInstance: bool, allowDeletion, debug, maxMentions, maxEmoji, httpd.translate, unitTest, httpd.YTReplacementDomain), daemon=True) + print('Creating scheduled post thread') httpd.thrPostSchedule = \ threadWithTrace(target=runPostSchedule, args=(baseDir, httpd, 20), daemon=True) + print('Creating newswire thread') + httpd.thrNewswireDaemon = \ + threadWithTrace(target=runNewswireDaemon, + args=(baseDir, httpd), daemon=True) + # flags used when restarting the inbox queue httpd.restartInboxQueueInProgress = False httpd.restartInboxQueue = False @@ -11272,6 +11280,12 @@ def runDaemon(blogsInstance: bool, mediaInstance: bool, threadWithTrace(target=runPostScheduleWatchdog, args=(projectVersion, httpd), daemon=True) httpd.thrWatchdogSchedule.start() + + print('Creating newswire watchdog') + httpd.thrNewswireWatchdog = \ + threadWithTrace(target=runNewswireWatchdog, + args=(projectVersion, httpd), daemon=True) + httpd.thrNewswireWatchdog.start() else: httpd.thrInboxQueue.start() httpd.thrPostSchedule.start() diff --git a/newswire.py b/newswire.py index d295b127..3a1476e7 100644 --- a/newswire.py +++ b/newswire.py @@ -7,6 +7,7 @@ __email__ = "bob@freedombone.net" __status__ = "Production" import os +import time import requests from socket import error as SocketError import errno @@ -175,3 +176,51 @@ def getDictFromNewswire(session, baseDir: str) -> {}: result = dict(result.items() + getRSS(session, url).items()) sortedResult = OrderedDict(sorted(result.items(), reverse=False)) return sortedResult + + +def runNewswireDaemon(baseDir: str, httpd): + """Periodically updates RSS feeds + """ + # initial sleep to allow the system to start up + time.sleep(100) + while True: + # has the session been created yet? + if not httpd.session: + print('Newswire daemon waiting for session') + time.sleep(60) + continue + + # try to update the feeds + newNewswire = None + loaded = False + try: + newNewswire = getDictFromNewswire(httpd.session, baseDir) + loaded = True + except BaseException: + print('WARN: unable to update newswire') + pass + + if loaded: + httpd.newswire = newNewswire + print('Newswire updated') + # wait a while before the next feeds update + time.sleep(1200) + else: + time.sleep(120) + + +def runNewswireWatchdog(projectVersion: str, httpd) -> None: + """This tries to keep the newswire update thread running even if it dies + """ + print('Starting newswire watchdog') + newswireOriginal = \ + httpd.thrPostSchedule.clone(runNewswireDaemon) + httpd.thrNewswireDaemon.start() + while True: + time.sleep(50) + if not httpd.thrNewswireDaemon.isAlive(): + httpd.thrNewswireDaemon.kill() + httpd.thrNewswireDaemon = \ + newswireOriginal.clone(runNewswireDaemon) + httpd.thrNewswireDaemon.start() + print('Restarting newswire daemon...')