diff --git a/daemon.py b/daemon.py index 39e72ddc1..062852a14 100644 --- a/daemon.py +++ b/daemon.py @@ -347,6 +347,7 @@ from context import getIndividualPostContext from speaker import getSSMLbox from city import getSpoofedCity from fitnessFunctions import fitnessPerformance +from fitnessFunctions import fitnessThread import os @@ -16917,7 +16918,10 @@ def runDaemon(defaultReplyIntervalHours: int, assert not scanThemesForScripts(baseDir) # fitness metrics + fitnessFilename = baseDir + '/accounts/fitness.json' httpd.fitness = {} + if os.path.isfile(fitnessFilename): + httpd.fitness = loadJson(fitnessFilename) # initialize authorized fetch key httpd.signingPrivateKeyPem = None @@ -17223,6 +17227,12 @@ def runDaemon(defaultReplyIntervalHours: int, print('Creating shared item files directory') os.mkdir(baseDir + '/sharefiles') + print('Creating fitness thread') + httpd.thrFitness = \ + threadWithTrace(target=fitnessThread, + args=(baseDir, httpd.fitness), daemon=True) + httpd.thrFitness.start() + print('Creating cache expiry thread') httpd.thrCache = \ threadWithTrace(target=expireCache, diff --git a/fitnessFunctions.py b/fitnessFunctions.py index 25a2f1b2b..43028b135 100644 --- a/fitnessFunctions.py +++ b/fitnessFunctions.py @@ -8,6 +8,7 @@ __status__ = "Production" __module_group__ = "Core" import time +from utils import saveJson def fitnessPerformance(startTime, fitnessState: {}, @@ -37,3 +38,13 @@ def fitnessPerformance(startTime, fitnessState: {}, print('FITNESS: performance/' + fitnessId + '/' + watchPoint + '/' + str(fitnessState['performance'][fitnessId][watchPoint])) + + +def fitnessThread(baseDir: str, fitness: {}): + """Thread used to save fitness function scores + """ + fitnessFilename = baseDir + '/accounts/fitness.json' + while True: + # every 10 mins + time.sleep(60 * 10) + saveJson(fitness, fitnessFilename) diff --git a/tests.py b/tests.py index f63a37c35..417a58845 100644 --- a/tests.py +++ b/tests.py @@ -4530,6 +4530,7 @@ def _testFunctions(): 'runNewswireWatchdog', 'runFederatedSharesWatchdog', 'runFederatedSharesDaemon', + 'fitnessThread', 'threadSendPost', 'sendToFollowers', 'expireCache',