Skip to content

Commit

Permalink
Use ThreadPoolExecuter
Browse files Browse the repository at this point in the history
  • Loading branch information
lwesterhof committed Nov 4, 2024
1 parent 06c8dba commit a4dd0df
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 13 deletions.
4 changes: 4 additions & 0 deletions app.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@

import json
import threading
from concurrent.futures import ThreadPoolExecutor
from os import path
from typing import Any, Dict, Optional

Expand All @@ -30,6 +31,9 @@
from util import get_validated_static_path, log_error
from vault.vault import vault_bp

# Create a global ThreadPoolExecutor.
executor = ThreadPoolExecutor(max_workers=2)

app = Flask(__name__, static_folder='assets')
app.json.sort_keys = False

Expand Down
26 changes: 20 additions & 6 deletions cache_config.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
__copyright__ = 'Copyright (c) 2024, Utrecht University'
__license__ = 'GPLv3, see LICENSE'

import hashlib
import json
from typing import List, Optional

from flask import current_app as app, g, request, session
Expand Down Expand Up @@ -126,13 +128,25 @@ def clear_api_cache_keys(fn: str) -> None:
log_error(f"Error deleting cache keys: {keys_to_delete}", True)


def populate_api_cache() -> None:
def populate_api_cache(user, irods, session_id) -> None:
"""Function to prepopulate the API cache."""
for fn in API_CACHE_TIMEOUTS:
try:
api.call(fn)
except Exception as e:
log_error(f"Error prepopulating cache {fn}: {e}", True)
# Set and session context.
g.user = user
g.irods = irods
session.sid = session_id

data = {}
params = json.dumps(data)
encoded_params = hashlib.md5(params.encode('utf-8')).hexdigest()

if authenticated():
for fn in API_CACHE_TIMEOUTS:
cache_key = make_key(f"{fn}-{encoded_params}")
if cache.get(cache_key) is None:
try:
api.call(fn)
except Exception as e:
log_error(f"Error prepopulating cache {fn}: {e}", True)


# Create a Cache instance.
Expand Down
2 changes: 1 addition & 1 deletion setup.cfg
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import-order-style=smarkets
strictness=short
docstring_style=sphinx
max-line-length=127
application-import-names=admin,api,cache_config,connman,errors,fileviewer,general,group_manager,monitor,research,search,open_search,stats,user,vault,deposit,intake,datarequest,util
application-import-names=admin,api,app,cache_config,connman,errors,fileviewer,general,group_manager,monitor,research,search,open_search,stats,user,vault,deposit,intake,datarequest,util
exclude=venv

[mypy]
Expand Down
16 changes: 10 additions & 6 deletions user/user.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@

import json
import secrets
import threading
from datetime import datetime
from typing import List
from uuid import uuid4
Expand All @@ -14,6 +13,7 @@
import requests
from flask import (
Blueprint,
copy_current_request_context,
flash,
g,
redirect,
Expand All @@ -34,6 +34,7 @@

import api
import connman
from app import executor
from cache_config import cache, make_key, populate_api_cache
from util import is_email_in_domains, is_relative_url, log_error

Expand Down Expand Up @@ -397,11 +398,6 @@ class UserinfoEmailMismatchError(Exception):
if exception_occurred:
return redirect(url_for('user_bp.gate')) # noqa: B012

# Start the background task to populate API cache.
thread = threading.Thread(target=populate_api_cache)
thread.daemon = True
thread.start()

return redirect(original_destination())


Expand Down Expand Up @@ -498,6 +494,14 @@ def prepare_user() -> None:
session['admin'] = response['data']
g.admin = session.get('admin')

@copy_current_request_context
def populate_api_cache_thread(user, irods, session_id):
with app.app_context():
populate_api_cache(user, irods, session_id)

# Submit the task to the global thread executor.
executor.submit(populate_api_cache_thread, g.user, g.irods, session.sid)

except PAM_AUTH_PASSWORD_FAILED:
# Password is not valid any more (probably OIDC access token).
connman.clean(session.sid)
Expand Down

0 comments on commit a4dd0df

Please sign in to comment.