From 8551e1e3a70f204050d146a4c88374624481a9e4 Mon Sep 17 00:00:00 2001 From: Bob Mottram Date: Tue, 20 Aug 2019 12:51:29 +0100 Subject: [PATCH] Archive daily --- cache.py | 8 -------- daemon.py | 13 +++++++++++-- posts.py | 10 ++++++++++ 3 files changed, 21 insertions(+), 10 deletions(-) diff --git a/cache.py b/cache.py index 20bed888..835ce4eb 100644 --- a/cache.py +++ b/cache.py @@ -66,14 +66,6 @@ def expirePersonCache(personCache: {}): del personCache[personUrl] print(str(len(removals))+' actors were expired from the cache') -def expireCache(baseDir: str,personCache: {}): - """Cache expiry thread - """ - while True: - # once per day - time.sleep(60*60*24) - expirePersonCache(basedir,personCache) - def storeWebfingerInCache(handle: str,wf,cachedWebfingers: {}) -> None: """Store a webfinger endpoint in the cache """ diff --git a/daemon.py b/daemon.py index e4379885..7dca7ab2 100644 --- a/daemon.py +++ b/daemon.py @@ -43,6 +43,7 @@ from posts import createFollowersOnlyPost from posts import createDirectMessagePost from posts import populateRepliesJson from posts import addToField +from posts import expireCache from inbox import inboxPermittedMessage from inbox import inboxMessageHasParams from inbox import runInboxQueue @@ -109,7 +110,6 @@ from announce import createAnnounce from announce import outboxAnnounce from content import addHtmlTags from media import removeMetaData -from cache import expireCache import os import sys @@ -2892,6 +2892,7 @@ def runDaemon(projectVersion, \ # max POST size of 10M httpd.projectVersion=projectVersion httpd.maxPostLength=1024*1024*10 + httpd.maxPostsInBox=256 httpd.domain=domain httpd.port=port httpd.domainFull=domain @@ -2949,10 +2950,18 @@ def runDaemon(projectVersion, \ print('Creating actors cache') os.mkdir(baseDir+'/cache/actors') + archiveDir=baseDir+'/archive' + if not os.path.isdir(archiveDir): + print('Creating archive') + os.mkdir(archiveDir) + print('Creating cache expiry thread') httpd.thrCache= \ threadWithTrace(target=expireCache, \ - args=(baseDir,httpd.personCache),daemon=True) + args=(baseDir,httpd.personCache, \ + httpd.httpPrefix, \ + archiveDir, \ + httpdmaxPostsInBox),daemon=True) httpd.thrCache.start() print('Creating inbox queue') diff --git a/posts.py b/posts.py index e3874b28..4acfb46e 100644 --- a/posts.py +++ b/posts.py @@ -21,6 +21,7 @@ from collections import OrderedDict from threads import threadWithTrace from cache import storePersonInCache from cache import getPersonFromCache +from cache import expirePersonCache from pprint import pprint from random import randint from session import createSession @@ -1706,6 +1707,15 @@ def createBoxBase(baseDir: str,boxname: str, \ return boxHeader return boxItems +def expireCache(baseDir: str,personCache: {},httpPrefix: str,archiveDir: str,maxPostsInBox=256): + """Thread used to expire actors from the cache and archive old posts + """ + while True: + # once per day + time.sleep(60*60*24) + expirePersonCache(basedir,personCache) + archivePosts(baseDir,httpPrefix,archiveDir,maxPostsInBox) + def archivePosts(baseDir: str,httpPrefix: str,archiveDir: str,maxPostsInBox=256) -> None: """Archives posts for all accounts """