diff --git a/daemon.py b/daemon.py index a5f321c9..189a197b 100644 --- a/daemon.py +++ b/daemon.py @@ -118,6 +118,7 @@ from shares import outboxShareUpload from shares import outboxUndoShareUpload from shares import addShare from shares import removeShare +from shares import expireShares from utils import getNicknameFromActor from utils import getDomainFromActor from utils import getStatusNumber @@ -4623,6 +4624,13 @@ def runPostsQueue(baseDir: str,sendThreads: [],debug: bool) -> None: time.sleep(1) 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: """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() 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, \ instanceId,clientToServer: bool, \ baseDir: str,domain: str, \ @@ -4789,6 +4811,18 @@ def runDaemon(projectVersion, \ else: 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') httpd.thrInboxQueue= \ threadWithTrace(target=runInboxQueue, \ diff --git a/shares.py b/shares.py index 231a8443..86964d3a 100644 --- a/shares.py +++ b/shares.py @@ -172,7 +172,18 @@ def addShare(baseDir: str, \ time.sleep(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 """ handleDomain=domain @@ -189,7 +200,7 @@ def expireShares(baseDir: str,nickname: str,domain: str) -> None: sharesJson=commentjson.load(fp) break except Exception as e: - print('WARN: commentjson exception expireShares - '+e) + print('WARN: commentjson exception expireSharesForAccount - '+e) time.sleep(1) tries+=1 if sharesJson: @@ -216,7 +227,7 @@ def expireShares(baseDir: str,nickname: str,domain: str) -> None: commentjson.dump(sharesJson, fp, indent=4, sort_keys=False) break except Exception as e: - print(e) + print('WARN: commentjson exception expireSharesForAccount 2 - '+e) time.sleep(1) tries+=1