Thread for expiring shared items

main2
Bob Mottram 2019-10-17 10:58:30 +01:00
parent f056c811be
commit 8b5376f616
2 changed files with 48 additions and 3 deletions

View File

@ -118,6 +118,7 @@ from shares import outboxShareUpload
from shares import outboxUndoShareUpload from shares import outboxUndoShareUpload
from shares import addShare from shares import addShare
from shares import removeShare from shares import removeShare
from shares import expireShares
from utils import getNicknameFromActor from utils import getNicknameFromActor
from utils import getDomainFromActor from utils import getDomainFromActor
from utils import getStatusNumber from utils import getStatusNumber
@ -4623,6 +4624,13 @@ def runPostsQueue(baseDir: str,sendThreads: [],debug: bool) -> None:
time.sleep(1) time.sleep(1)
removeDormantThreads(baseDir,sendThreads,debug) removeDormantThreads(baseDir,sendThreads,debug)
def runSharesExpire(baseDir: str) -> None:
"""Expires shares as needed
"""
while True:
time.sleep(120)
expireShares(baseDir)
def runPostsWatchdog(projectVersion: str,httpd) -> None: def runPostsWatchdog(projectVersion: str,httpd) -> None:
"""This tries to keep the posts thread running even if it dies """This tries to keep the posts thread running even if it dies
""" """
@ -4637,6 +4645,20 @@ def runPostsWatchdog(projectVersion: str,httpd) -> None:
httpd.thrPostsQueue.start() httpd.thrPostsQueue.start()
print('Restarting posts queue...') print('Restarting posts queue...')
def runSharesExpireWatchdog(projectVersion: str,httpd) -> None:
"""This tries to keep the shares expiry thread running even if it dies
"""
print('Starting shares expiry watchdog')
sharesExpireOriginal=httpd.thrSharesExpire.clone(runSharesExpire)
httpd.thrSharesExpire.start()
while True:
time.sleep(20)
if not httpd.thrSharesExpire.isAlive():
httpd.thrSharesExpire.kill()
httpd.thrSharesExpire=sharesExpireOriginal.clone(runSharesExpire)
httpd.thrSharesExpire.start()
print('Restarting shares expiry...')
def runDaemon(projectVersion, \ def runDaemon(projectVersion, \
instanceId,clientToServer: bool, \ instanceId,clientToServer: bool, \
baseDir: str,domain: str, \ baseDir: str,domain: str, \
@ -4789,6 +4811,18 @@ def runDaemon(projectVersion, \
else: else:
httpd.thrPostsQueue.start() httpd.thrPostsQueue.start()
print('Creating expire thread for shared items')
httpd.thrSharesExpire= \
threadWithTrace(target=runSharesExpire, \
args=(baseDir),daemon=True)
if not unitTest:
httpd.thrSharesExpireWatchdog= \
threadWithTrace(target=runSharesExpireWatchdog, \
args=(projectVersion,httpd),daemon=True)
httpd.thrSharesExpireWatchdog.start()
else:
httpd.thrSharesExpire.start()
print('Creating inbox queue') print('Creating inbox queue')
httpd.thrInboxQueue= \ httpd.thrInboxQueue= \
threadWithTrace(target=runInboxQueue, \ threadWithTrace(target=runInboxQueue, \

View File

@ -172,7 +172,18 @@ def addShare(baseDir: str, \
time.sleep(1) time.sleep(1)
tries+=1 tries+=1
def expireShares(baseDir: str,nickname: str,domain: str) -> None: def expireShares(baseDir: str) -> None:
"""Removes expired items from shares
"""
for subdir,dirs,files in os.walk(baseDir+'/accounts'):
for account in dirs:
if '@' not in account:
continue
nickname=account.split('@')[0]
domain=account.split('@')[1]
expireSharesForAccount(baseDir,nickname,domain)
def expireSharesForAccount(baseDir: str,nickname: str,domain: str) -> None:
"""Removes expired items from shares """Removes expired items from shares
""" """
handleDomain=domain handleDomain=domain
@ -189,7 +200,7 @@ def expireShares(baseDir: str,nickname: str,domain: str) -> None:
sharesJson=commentjson.load(fp) sharesJson=commentjson.load(fp)
break break
except Exception as e: except Exception as e:
print('WARN: commentjson exception expireShares - '+e) print('WARN: commentjson exception expireSharesForAccount - '+e)
time.sleep(1) time.sleep(1)
tries+=1 tries+=1
if sharesJson: if sharesJson:
@ -216,7 +227,7 @@ def expireShares(baseDir: str,nickname: str,domain: str) -> None:
commentjson.dump(sharesJson, fp, indent=4, sort_keys=False) commentjson.dump(sharesJson, fp, indent=4, sort_keys=False)
break break
except Exception as e: except Exception as e:
print(e) print('WARN: commentjson exception expireSharesForAccount 2 - '+e)
time.sleep(1) time.sleep(1)
tries+=1 tries+=1