From 7abf819572c5e9a7f9011130e51ee7d999c98cc5 Mon Sep 17 00:00:00 2001 From: Bob Mottram Date: Fri, 9 Feb 2024 21:00:12 +0000 Subject: [PATCH] Create a daemon to handle federated blocks --- blocking.py | 37 +++++++++++++++++++++++++++++++------ daemon.py | 17 +++++------------ tests.py | 1 + 3 files changed, 37 insertions(+), 18 deletions(-) diff --git a/blocking.py b/blocking.py index b137e7083..f1a253c17 100644 --- a/blocking.py +++ b/blocking.py @@ -11,6 +11,7 @@ import os import json import time from session import get_json_valid +from session import create_session from utils import date_from_string_format from utils import date_utcnow from utils import remove_eol @@ -1833,12 +1834,12 @@ def load_federated_blocks_endpoints(base_dir: str) -> []: return block_federated_endpoints -def update_federated_blocks(session, base_dir: str, - http_prefix: str, - domain: str, domain_full: str, - debug: bool, version: str, - signing_priv_key_pem: str, - max_api_blocks: int) -> []: +def _update_federated_blocks(session, base_dir: str, + http_prefix: str, + domain: str, domain_full: str, + debug: bool, version: str, + signing_priv_key_pem: str, + max_api_blocks: int) -> []: """Creates block_api.txt """ block_federated = [] @@ -1939,3 +1940,27 @@ def save_block_federated_endpoints(base_dir: str, except OSError: print('EX: unable to write block_api_endpoints.txt') return result + + +def run_federated_blocks_daemon(base_dir: str, httpd, debug: bool) -> None: + """Runs the daemon used to update federated blocks + """ + seconds_per_hour = 60 * 60 + time.sleep(60) + + session = None + while True: + if httpd.session: + session = httpd.session + else: + session = create_session(httpd.proxy_type) + + if session: + httpd.block_federated = \ + _update_federated_blocks(httpd.session, base_dir, + httpd.http_prefix, + httpd.domain, httpd.domain_full, + debug, httpd.project_version, + httpd.signing_priv_key_pem, + httpd.max_api_blocks) + time.sleep(seconds_per_hour * 6) diff --git a/daemon.py b/daemon.py index 4f010671b..4fc6431ee 100644 --- a/daemon.py +++ b/daemon.py @@ -160,9 +160,9 @@ from media import path_is_transcript from media import path_is_audio from cwlists import get_cw_list_variable from cwlists import load_cw_lists +from blocking import run_federated_blocks_daemon from blocking import save_block_federated_endpoints from blocking import load_federated_blocks_endpoints -from blocking import update_federated_blocks from blocking import contains_military_domain from blocking import load_blocked_military from blocking import save_blocked_military @@ -25283,17 +25283,10 @@ def run_daemon(no_of_books: int, # this is the instance actor private key httpd.signing_priv_key_pem = get_instance_actor_key(base_dir, domain) - # load federated blocklists - if not unit_test: - curr_session = create_session(proxy_type) - if curr_session: - httpd.block_federated = \ - update_federated_blocks(curr_session, base_dir, - http_prefix, - domain, httpd.domain_full, - debug, project_version, - httpd.signing_priv_key_pem, - httpd.max_api_blocks) + print('THREAD: Creating federated blocks thread') + httpd.thrFederatedBlocksDaemon = \ + thread_with_trace(target=run_federated_blocks_daemon, + args=(base_dir, httpd, debug), daemon=True) # threads used for checking for actor changes when clicking on # avatar icon / person options diff --git a/tests.py b/tests.py index 3db486be6..1ee2edd0d 100644 --- a/tests.py +++ b/tests.py @@ -5673,6 +5673,7 @@ def _test_functions(): 'run_post_schedule', 'run_post_schedule_watchdog', 'str2bool', + 'run_federated_blocks_daemon', 'run_newswire_daemon', 'run_newswire_watchdog', 'run_federated_shares_watchdog',