Skip to content

Commit 4fe126c

Browse files
committed
move PeriodicTimer class to separate file
1 parent 8c01c67 commit 4fe126c

File tree

2 files changed

+53
-45
lines changed

2 files changed

+53
-45
lines changed

docker/periodic_timer.py

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
"""
2+
Helper class for start.py
3+
"""
4+
5+
import threading
6+
import time
7+
8+
9+
class PeriodicTimer:
10+
"""
11+
Helper class to facilitate waiting for periodic events.
12+
Requires the start() function to be called first.
13+
"""
14+
15+
def __init__(self, interval):
16+
"""
17+
:param interval: interval in seconds
18+
"""
19+
self._interval = interval
20+
self._flag = 0
21+
self._cv = threading.Condition()
22+
23+
def start(self):
24+
"""
25+
Start the notification thread.
26+
"""
27+
threading.Thread(target=self.run, daemon=True).start()
28+
29+
def run(self):
30+
"""
31+
Run the timer and notify waiting threads after each interval
32+
"""
33+
while True:
34+
time.sleep(self._interval)
35+
self.notify_all()
36+
37+
def wait_for_tick(self):
38+
"""
39+
Wait for the next tick of the timer
40+
"""
41+
with self._cv:
42+
last_flag = self._flag
43+
while last_flag == self._flag:
44+
self._cv.wait()
45+
46+
def notify_all(self):
47+
"""
48+
Notify all listeners, possibly out of band.
49+
"""
50+
with self._cv:
51+
self._flag ^= 1
52+
self._cv.notify_all()

docker/start.py

Lines changed: 1 addition & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@
4949
from opengrok_tools.utils.exitvals import SUCCESS_EXITVAL
5050
from opengrok_tools.utils.mirror import check_configuration
5151
from opengrok_tools.mirror import OPENGROK_NO_MIRROR_ENV
52-
52+
from periodic_timer import PeriodicTimer
5353

5454
fs_root = os.path.abspath('.').split(os.path.sep)[0] + os.path.sep
5555
if os.environ.get('OPENGROK_TOMCAT_ROOT'): # debug only
@@ -75,50 +75,6 @@
7575
NOMIRROR_ENV_NAME = 'NOMIRROR'
7676

7777
expected_token = None
78-
79-
80-
class PeriodicTimer:
81-
"""
82-
Helper class to facilitate waiting for periodic events.
83-
Requires the start() function to be called first.
84-
"""
85-
def __init__(self, interval):
86-
"""
87-
:param interval: interval in seconds
88-
"""
89-
self._interval = interval
90-
self._flag = 0
91-
self._cv = threading.Condition()
92-
93-
def start(self):
94-
threading.Thread(target=self.run, daemon=True).start()
95-
96-
def run(self):
97-
"""
98-
Run the timer and notify waiting threads after each interval
99-
"""
100-
while True:
101-
time.sleep(self._interval)
102-
self.notify_all()
103-
104-
def wait_for_tick(self):
105-
"""
106-
Wait for the next tick of the timer
107-
"""
108-
with self._cv:
109-
last_flag = self._flag
110-
while last_flag == self._flag:
111-
self._cv.wait()
112-
113-
def notify_all(self):
114-
"""
115-
Notify all listeners, possibly out of band.
116-
"""
117-
with self._cv:
118-
self._flag ^= 1
119-
self._cv.notify_all()
120-
121-
12278
periodic_timer = None
12379
app = Flask(__name__)
12480
auth = HTTPTokenAuth(scheme='Bearer')

0 commit comments

Comments
 (0)