Adjustable timeout for dormant post threads

merge-requests/8/head
Bob Mottram 2020-12-18 11:48:00 +00:00
parent ef72b93984
commit 886e4f2beb
4 changed files with 36 additions and 11 deletions

View File

@ -12980,12 +12980,13 @@ class EpicyonServer(ThreadingHTTPServer):
return HTTPServer.handle_error(self, request, client_address) 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 """Manages the threads used to send posts
""" """
while True: while True:
time.sleep(1) time.sleep(1)
removeDormantThreads(baseDir, sendThreads, debug) removeDormantThreads(baseDir, sendThreads, debug, timeoutMins)
def runSharesExpire(versionNumber: str, baseDir: str) -> None: def runSharesExpire(versionNumber: str, baseDir: str) -> None:
@ -13048,7 +13049,8 @@ def loadTokens(baseDir: str, tokensDict: {}, tokensLookup: {}) -> None:
break break
def runDaemon(dormantMonths: int, def runDaemon(sendThreadsTimeoutMins: int,
dormantMonths: int,
maxNewswirePosts: int, maxNewswirePosts: int,
allowLocalNetworkAccess: bool, allowLocalNetworkAccess: bool,
maxFeedItemSizeKb: int, maxFeedItemSizeKb: int,
@ -13343,10 +13345,14 @@ def runDaemon(dormantMonths: int,
httpd.maxPostsInBox), daemon=True) httpd.maxPostsInBox), daemon=True)
httpd.thrCache.start() httpd.thrCache.start()
# number of mins after which sending posts or updates will expire
httpd.sendThreadsTimeoutMins = sendThreadsTimeoutMins
print('Creating posts queue') print('Creating posts queue')
httpd.thrPostsQueue = \ httpd.thrPostsQueue = \
threadWithTrace(target=runPostsQueue, threadWithTrace(target=runPostsQueue,
args=(baseDir, httpd.sendThreads, debug), daemon=True) args=(baseDir, httpd.sendThreads, debug,
httpd.sendThreadsTimeoutMins), daemon=True)
if not unitTest: if not unitTest:
httpd.thrPostsWatchdog = \ httpd.thrPostsWatchdog = \
threadWithTrace(target=runPostsWatchdog, threadWithTrace(target=runPostsWatchdog,

View File

@ -122,6 +122,11 @@ parser.add_argument('--dormantMonths',
default=3, default=3,
help='How many months does a followed account need to ' + help='How many months does a followed account need to ' +
'be unseen for before being considered dormant') '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', parser.add_argument('--maxNewswirePosts',
dest='maxNewswirePosts', type=int, dest='maxNewswirePosts', type=int,
default=20, default=20,
@ -2035,6 +2040,11 @@ dormantMonths = \
if dormantMonths is not None: if dormantMonths is not None:
args.dormantMonths = int(dormantMonths) args.dormantMonths = int(dormantMonths)
sendThreadsTimeoutMins = \
getConfigParam(baseDir, 'sendThreadsTimeoutMins')
if sendThreadsTimeoutMins is not None:
args.sendThreadsTimeoutMins = int(sendThreadsTimeoutMins)
allowNewsFollowers = \ allowNewsFollowers = \
getConfigParam(baseDir, 'allowNewsFollowers') getConfigParam(baseDir, 'allowNewsFollowers')
if allowNewsFollowers is not None: if allowNewsFollowers is not None:
@ -2083,7 +2093,8 @@ if setTheme(baseDir, themeName, domain, args.allowLocalNetworkAccess):
print('Theme set to ' + themeName) print('Theme set to ' + themeName)
if __name__ == "__main__": if __name__ == "__main__":
runDaemon(args.dormantMonths, runDaemon(args.sendThreadsTimeoutMins,
args.dormantMonths,
args.maxNewswirePosts, args.maxNewswirePosts,
args.allowLocalNetworkAccess, args.allowLocalNetworkAccess,
args.maxFeedItemSizeKb, args.maxFeedItemSizeKb,

View File

@ -296,8 +296,10 @@ def createServerAlice(path: str, domain: str, port: int,
allowLocalNetworkAccess = True allowLocalNetworkAccess = True
maxNewswirePosts = 20 maxNewswirePosts = 20
dormantMonths = 3 dormantMonths = 3
sendThreadsTimeoutMins = 30
print('Server running: Alice') print('Server running: Alice')
runDaemon(dormantMonths, maxNewswirePosts, runDaemon(sendThreadsTimeoutMins,
dormantMonths, maxNewswirePosts,
allowLocalNetworkAccess, allowLocalNetworkAccess,
2048, False, True, False, False, True, 10, False, 2048, False, True, False, False, True, 10, False,
0, 100, 1024, 5, False, 0, 100, 1024, 5, False,
@ -366,8 +368,10 @@ def createServerBob(path: str, domain: str, port: int,
allowLocalNetworkAccess = True allowLocalNetworkAccess = True
maxNewswirePosts = 20 maxNewswirePosts = 20
dormantMonths = 3 dormantMonths = 3
sendThreadsTimeoutMins = 30
print('Server running: Bob') print('Server running: Bob')
runDaemon(dormantMonths, maxNewswirePosts, runDaemon(sendThreadsTimeoutMins,
dormantMonths, maxNewswirePosts,
allowLocalNetworkAccess, allowLocalNetworkAccess,
2048, False, True, False, False, True, 10, False, 2048, False, True, False, False, True, 10, False,
0, 100, 1024, 5, False, 0, 0, 100, 1024, 5, False, 0,
@ -410,8 +414,10 @@ def createServerEve(path: str, domain: str, port: int, federationList: [],
allowLocalNetworkAccess = True allowLocalNetworkAccess = True
maxNewswirePosts = 20 maxNewswirePosts = 20
dormantMonths = 3 dormantMonths = 3
sendThreadsTimeoutMins = 30
print('Server running: Eve') print('Server running: Eve')
runDaemon(dormantMonths, maxNewswirePosts, runDaemon(sendThreadsTimeoutMins,
dormantMonths, maxNewswirePosts,
allowLocalNetworkAccess, allowLocalNetworkAccess,
2048, False, True, False, False, True, 10, False, 2048, False, True, False, False, True, 10, False,
0, 100, 1024, 5, False, 0, 0, 100, 1024, 5, False, 0,

View File

@ -69,12 +69,14 @@ class threadWithTrace(threading.Thread):
daemon=True) 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 """Removes threads whose execution has completed
""" """
if len(threadsList) == 0: if len(threadsList) == 0:
return return
timeoutSecs = int(timeoutMins * 60)
dormantThreads = [] dormantThreads = []
currTime = datetime.datetime.utcnow() currTime = datetime.datetime.utcnow()
changed = False changed = False
@ -92,13 +94,13 @@ def removeDormantThreads(baseDir: str, threadsList: [], debug: bool) -> None:
'thread is not alive ten seconds after start') 'thread is not alive ten seconds after start')
removeThread = True removeThread = True
# timeout for started threads # timeout for started threads
if (currTime - th.startTime).total_seconds() > 600: if (currTime - th.startTime).total_seconds() > timeoutSecs:
if debug: if debug:
print('DEBUG: started thread timed out') print('DEBUG: started thread timed out')
removeThread = True removeThread = True
else: else:
# timeout for threads which havn't been started # timeout for threads which havn't been started
if (currTime - th.startTime).total_seconds() > 600: if (currTime - th.startTime).total_seconds() > timeoutSecs:
if debug: if debug:
print('DEBUG: unstarted thread timed out') print('DEBUG: unstarted thread timed out')
removeThread = True removeThread = True