From 886e4f2beb285d0a97755fe322e7ebe1f41bdbda Mon Sep 17 00:00:00 2001 From: Bob Mottram Date: Fri, 18 Dec 2020 11:48:00 +0000 Subject: [PATCH] Adjustable timeout for dormant post threads --- daemon.py | 14 ++++++++++---- epicyon.py | 13 ++++++++++++- tests.py | 12 +++++++++--- threads.py | 8 +++++--- 4 files changed, 36 insertions(+), 11 deletions(-) diff --git a/daemon.py b/daemon.py index 6c76b5a2e..20dc14b85 100644 --- a/daemon.py +++ b/daemon.py @@ -12980,12 +12980,13 @@ class EpicyonServer(ThreadingHTTPServer): return HTTPServer.handle_error(self, request, client_address) -def runPostsQueue(baseDir: str, sendThreads: [], debug: bool) -> None: +def runPostsQueue(baseDir: str, sendThreads: [], debug: bool, + timeoutMins: int) -> None: """Manages the threads used to send posts """ while True: time.sleep(1) - removeDormantThreads(baseDir, sendThreads, debug) + removeDormantThreads(baseDir, sendThreads, debug, timeoutMins) def runSharesExpire(versionNumber: str, baseDir: str) -> None: @@ -13048,7 +13049,8 @@ def loadTokens(baseDir: str, tokensDict: {}, tokensLookup: {}) -> None: break -def runDaemon(dormantMonths: int, +def runDaemon(sendThreadsTimeoutMins: int, + dormantMonths: int, maxNewswirePosts: int, allowLocalNetworkAccess: bool, maxFeedItemSizeKb: int, @@ -13343,10 +13345,14 @@ def runDaemon(dormantMonths: int, httpd.maxPostsInBox), daemon=True) httpd.thrCache.start() + # number of mins after which sending posts or updates will expire + httpd.sendThreadsTimeoutMins = sendThreadsTimeoutMins + print('Creating posts queue') httpd.thrPostsQueue = \ threadWithTrace(target=runPostsQueue, - args=(baseDir, httpd.sendThreads, debug), daemon=True) + args=(baseDir, httpd.sendThreads, debug, + httpd.sendThreadsTimeoutMins), daemon=True) if not unitTest: httpd.thrPostsWatchdog = \ threadWithTrace(target=runPostsWatchdog, diff --git a/epicyon.py b/epicyon.py index ba8eb64b3..8b243c66e 100644 --- a/epicyon.py +++ b/epicyon.py @@ -122,6 +122,11 @@ parser.add_argument('--dormantMonths', default=3, help='How many months does a followed account need to ' + 'be unseen for before being considered dormant') +parser.add_argument('--sendThreadsTimeoutMins', + dest='sendThreadsTimeoutMins', type=int, + default=30, + help='How many minutes before a thread to send out ' + + 'posts expires') parser.add_argument('--maxNewswirePosts', dest='maxNewswirePosts', type=int, default=20, @@ -2035,6 +2040,11 @@ dormantMonths = \ if dormantMonths is not None: args.dormantMonths = int(dormantMonths) +sendThreadsTimeoutMins = \ + getConfigParam(baseDir, 'sendThreadsTimeoutMins') +if sendThreadsTimeoutMins is not None: + args.sendThreadsTimeoutMins = int(sendThreadsTimeoutMins) + allowNewsFollowers = \ getConfigParam(baseDir, 'allowNewsFollowers') if allowNewsFollowers is not None: @@ -2083,7 +2093,8 @@ if setTheme(baseDir, themeName, domain, args.allowLocalNetworkAccess): print('Theme set to ' + themeName) if __name__ == "__main__": - runDaemon(args.dormantMonths, + runDaemon(args.sendThreadsTimeoutMins, + args.dormantMonths, args.maxNewswirePosts, args.allowLocalNetworkAccess, args.maxFeedItemSizeKb, diff --git a/tests.py b/tests.py index 654f977be..4b41b1fc4 100644 --- a/tests.py +++ b/tests.py @@ -296,8 +296,10 @@ def createServerAlice(path: str, domain: str, port: int, allowLocalNetworkAccess = True maxNewswirePosts = 20 dormantMonths = 3 + sendThreadsTimeoutMins = 30 print('Server running: Alice') - runDaemon(dormantMonths, maxNewswirePosts, + runDaemon(sendThreadsTimeoutMins, + dormantMonths, maxNewswirePosts, allowLocalNetworkAccess, 2048, False, True, False, False, True, 10, False, 0, 100, 1024, 5, False, @@ -366,8 +368,10 @@ def createServerBob(path: str, domain: str, port: int, allowLocalNetworkAccess = True maxNewswirePosts = 20 dormantMonths = 3 + sendThreadsTimeoutMins = 30 print('Server running: Bob') - runDaemon(dormantMonths, maxNewswirePosts, + runDaemon(sendThreadsTimeoutMins, + dormantMonths, maxNewswirePosts, allowLocalNetworkAccess, 2048, False, True, False, False, True, 10, False, 0, 100, 1024, 5, False, 0, @@ -410,8 +414,10 @@ def createServerEve(path: str, domain: str, port: int, federationList: [], allowLocalNetworkAccess = True maxNewswirePosts = 20 dormantMonths = 3 + sendThreadsTimeoutMins = 30 print('Server running: Eve') - runDaemon(dormantMonths, maxNewswirePosts, + runDaemon(sendThreadsTimeoutMins, + dormantMonths, maxNewswirePosts, allowLocalNetworkAccess, 2048, False, True, False, False, True, 10, False, 0, 100, 1024, 5, False, 0, diff --git a/threads.py b/threads.py index cdb3cb95d..89a43a1eb 100644 --- a/threads.py +++ b/threads.py @@ -69,12 +69,14 @@ class threadWithTrace(threading.Thread): daemon=True) -def removeDormantThreads(baseDir: str, threadsList: [], debug: bool) -> None: +def removeDormantThreads(baseDir: str, threadsList: [], debug: bool, + timeoutMins=30) -> None: """Removes threads whose execution has completed """ if len(threadsList) == 0: return + timeoutSecs = int(timeoutMins * 60) dormantThreads = [] currTime = datetime.datetime.utcnow() changed = False @@ -92,13 +94,13 @@ def removeDormantThreads(baseDir: str, threadsList: [], debug: bool) -> None: 'thread is not alive ten seconds after start') removeThread = True # timeout for started threads - if (currTime - th.startTime).total_seconds() > 600: + if (currTime - th.startTime).total_seconds() > timeoutSecs: if debug: print('DEBUG: started thread timed out') removeThread = True else: # timeout for threads which havn't been started - if (currTime - th.startTime).total_seconds() > 600: + if (currTime - th.startTime).total_seconds() > timeoutSecs: if debug: print('DEBUG: unstarted thread timed out') removeThread = True