Skip to content

Commit 7f441ee

Browse files
authored
Merge branch 'master' into patch-240
2 parents 7980748 + c3c4e13 commit 7f441ee

File tree

2 files changed

+48
-11
lines changed

2 files changed

+48
-11
lines changed

src/app.py

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,11 +8,13 @@
88
from io import StringIO
99
import logging
1010
import os
11+
import random
1112
import re
1213
import sys
1314
import traceback
1415
import urllib.parse
1516
import requests
17+
import threading
1618
from wtforms import ValidationError
1719
from flask_wtf.csrf import CSRFProtect
1820

@@ -116,6 +118,35 @@ def _verify_config(cfg):
116118
csrf = CSRFProtect()
117119
csrf.init_app(app)
118120

121+
#############################################################################
122+
# Background update thread
123+
# Run when the topology cache is 2/3 to expiration
124+
bg_update_freq = max(global_data.topology.cache_lifetime*2/3, 60) # seconds
125+
bg_update_thread = threading.Thread()
126+
127+
def bg_update_run():
128+
'''Background update task'''
129+
app.logger.debug('Background update started')
130+
global_data.update_topology()
131+
132+
# Add +/- 10% random offset to avoid thundering herds
133+
delay = bg_update_freq
134+
delay *= random.uniform(0.9, 1.1)
135+
136+
# Set next run
137+
global bg_update_thread
138+
bg_update_thread = threading.Timer(delay, bg_update_run, ())
139+
bg_update_thread.daemon = True
140+
bg_update_thread.start()
141+
app.logger.info('Background update complete')
142+
143+
# Start background update thread
144+
bg_update_thread = threading.Timer(bg_update_freq, bg_update_run, ())
145+
# Make it a daemon thread, so interpreter won't wait on it when exiting
146+
bg_update_thread.daemon = True
147+
bg_update_thread.start()
148+
#############################################################################
149+
119150

120151
def _fix_unicode(text):
121152
"""Convert a partial unicode string to full unicode"""

src/webapp/models.py

Lines changed: 17 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -288,20 +288,26 @@ def get_topology(self) -> Optional[Topology]:
288288
"""
289289
if self.topology.should_update():
290290
with topology_update_summary.time():
291-
ok = self._update_topology_repo()
292-
if ok:
293-
try:
294-
self.topology.update(rg_reader.get_topology(self.topology_dir, self.get_contacts_data(), strict=self.strict))
295-
except Exception:
296-
if self.strict:
297-
raise
298-
log.exception("Failed to update topology")
299-
self.topology.try_again()
300-
else:
301-
self.topology.try_again()
291+
self.update_topology()
302292

303293
return self.topology.data
304294

295+
def update_topology(self):
296+
"""
297+
Update topology data
298+
"""
299+
ok = self._update_topology_repo()
300+
if ok:
301+
try:
302+
self.topology.update(rg_reader.get_topology(self.topology_dir, self.get_contacts_data(), strict=self.strict))
303+
except Exception:
304+
if self.strict:
305+
raise
306+
log.exception("Failed to update topology")
307+
self.topology.try_again()
308+
else:
309+
self.topology.try_again()
310+
305311
def get_vos_data(self) -> Optional[VOsData]:
306312
"""
307313
Get VO Data.

0 commit comments

Comments
 (0)