Skip to content

Commit ab0e0c1

Browse files
committed
fix PeriodicTimer usage
fixes #4066
1 parent 05f9a7f commit ab0e0c1

File tree

2 files changed

+28
-21
lines changed

2 files changed

+28
-21
lines changed

docker/periodic_timer.py

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -8,23 +8,24 @@
88

99
class PeriodicTimer:
1010
"""
11-
Helper class to facilitate waiting for periodic events.
12-
Requires the start() function to be called first.
11+
Helper class to facilitate waiting for (periodic) events.
12+
For periodic events, start() function has to be called first.
1313
"""
1414

1515
def __init__(self, interval):
1616
"""
17-
:param interval: interval in seconds
17+
:param interval: interval in seconds, can be 0
1818
"""
1919
self._interval = interval
2020
self._flag = 0
2121
self._cv = threading.Condition()
2222

2323
def start(self):
2424
"""
25-
Start the notification thread.
25+
Start the periodic notification thread if the configured interval is positive.
2626
"""
27-
threading.Thread(target=self.run, daemon=True).start()
27+
if self._interval > 0:
28+
threading.Thread(target=self.run, daemon=True).start()
2829

2930
def run(self):
3031
"""
@@ -34,9 +35,10 @@ def run(self):
3435
time.sleep(self._interval)
3536
self.notify_all()
3637

37-
def wait_for_tick(self):
38+
def wait_for_event(self):
3839
"""
39-
Wait for the next tick of the timer
40+
Wait for the wakeup event. This can be either tick of the timer from run(),
41+
or notification via notify_all().
4042
"""
4143
with self._cv:
4244
last_flag = self._flag

docker/start.py

Lines changed: 19 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -81,11 +81,6 @@
8181
REINDEX_POINT = '/reindex'
8282

8383

84-
def trigger_reindex():
85-
# Signal the sync/indexer thread.
86-
periodic_timer.notify_all()
87-
88-
8984
@auth.verify_token
9085
def verify_token(token):
9186
if expected_token is None:
@@ -98,9 +93,16 @@ def verify_token(token):
9893
@app.route(REINDEX_POINT)
9994
@auth.login_required
10095
def index():
101-
periodic_timer.notify_all()
96+
global periodic_timer
97+
98+
if periodic_timer:
99+
logger = logging.getLogger(__name__)
100+
logger.debug("Triggering reindex based on API call")
102101

103-
return "Reindex triggered"
102+
periodic_timer.notify_all()
103+
return "Reindex triggered\n"
104+
105+
return "Reindex not triggered - the timer is not initialized yet\n"
104106

105107

106108
def rest_function(logger, rest_port):
@@ -297,7 +299,8 @@ def indexer_no_projects(logger, uri, config_path, extra_indexer_options):
297299
indexer.execute()
298300

299301
logger.info("Waiting for reindex to be triggered")
300-
periodic_timer.wait_for_tick()
302+
global periodic_timer
303+
periodic_timer.wait_for_event()
301304

302305

303306
def project_syncer(logger, loglevel, uri, config_path, numworkers, env):
@@ -346,7 +349,8 @@ def project_syncer(logger, loglevel, uri, config_path, numworkers, env):
346349
save_config(logger, uri, config_path)
347350

348351
logger.info("Waiting for reindex to be triggered")
349-
periodic_timer.wait_for_tick()
352+
global periodic_timer
353+
periodic_timer.wait_for_event()
350354

351355

352356
def create_bare_config(logger, use_projects, extra_indexer_options=None):
@@ -553,6 +557,12 @@ def main():
553557
extra_indexer_options)
554558

555559
if sync_enabled:
560+
period_seconds = sync_period_mins * 60
561+
logger.debug(f"Creating and starting periodic timer (period {period_seconds} seconds)")
562+
global periodic_timer
563+
periodic_timer = PeriodicTimer(period_seconds)
564+
periodic_timer.start()
565+
556566
logger.debug("Starting sync thread")
557567
sync_thread = threading.Thread(target=worker_function,
558568
name="Sync thread",
@@ -561,11 +571,6 @@ def main():
561571

562572
start_rest_thread(logger)
563573

564-
if sync_period_mins > 0:
565-
global periodic_timer
566-
periodic_timer = PeriodicTimer(sync_period_mins * 60)
567-
periodic_timer.start()
568-
569574
# Start Tomcat last. It will be the foreground process.
570575
logger.info("Starting Tomcat")
571576
global tomcat_popen

0 commit comments

Comments
 (0)