Skip to content

Commit ac85e34

Browse files
committed
Refactor: Replaced the check_timeout loop by a per-state Timer.
This allows the timeout be configured to values less than 1 hour.
1 parent b996b9d commit ac85e34

File tree

1 file changed

+32
-40
lines changed

1 file changed

+32
-40
lines changed

homu/main.py

Lines changed: 32 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
from . import utils
88
from .utils import lazy_debug
99
import logging
10-
from threading import Thread, Lock
10+
from threading import Thread, Lock, Timer
1111
import time
1212
import traceback
1313
import sqlite3
@@ -128,7 +128,8 @@ def __init__(self, num, head_sha, status, db, repo_label, mergeable_que,
128128
self.owner = owner
129129
self.name = name
130130
self.repos = repos
131-
self.test_started = time.time() # FIXME: Save in the local database
131+
self.timeout_timer = None
132+
self.test_started = time.time()
132133

133134
def head_advanced(self, head_sha, *, use_db=True):
134135
self.head_sha = head_sha
@@ -179,6 +180,9 @@ def add_comment(self, text):
179180

180181
def set_status(self, status):
181182
self.status = status
183+
if self.timeout_timer:
184+
self.timeout_timer.cancel()
185+
self.timeout_timer = None
182186

183187
db_query(
184188
self.db,
@@ -320,6 +324,30 @@ def blocked_by_closed_tree(self):
320324
treeclosed = self.repos[self.repo_label].treeclosed
321325
return treeclosed if self.priority < treeclosed else None
322326

327+
def start_testing(self, timeout):
328+
self.test_started = time.time() # FIXME: Save in the local database
329+
self.set_status('pending')
330+
timer = Timer(timeout, self.timed_out)
331+
timer.start()
332+
self.timeout_timer = timer
333+
334+
def timed_out(self):
335+
print('* Test timed out: {}'.format(self))
336+
337+
self.merge_sha = ''
338+
self.save()
339+
self.set_status('failure')
340+
341+
desc = 'Test timed out'
342+
utils.github_create_status(
343+
self.get_repo(),
344+
self.head_sha,
345+
'failure',
346+
'',
347+
desc,
348+
context='homu')
349+
self.add_comment(':boom: {}'.format(desc))
350+
323351

324352
def sha_cmp(short, full):
325353
return len(short) >= 4 and short == full[:len(short)]
@@ -1140,8 +1168,7 @@ def start_build(state, repo_cfgs, buildbot_slots, logger, db, git_cfg):
11401168
branch,
11411169
state.merge_sha))
11421170

1143-
state.test_started = time.time()
1144-
state.set_status('pending')
1171+
state.start_testing(TEST_TIMEOUT)
11451172

11461173
desc = '{} commit {} with merge {}...'.format(
11471174
'Trying' if state.try_ else 'Testing',
@@ -1217,8 +1244,7 @@ def start_rebuild(state, repo_cfgs):
12171244
state.add_comment(':bomb: Failed to start rebuilding: `{}`'.format(err)) # noqa
12181245
return False
12191246

1220-
state.test_started = time.time()
1221-
state.set_status('pending')
1247+
state.start_testing(TEST_TIMEOUT)
12221248

12231249
msg_1 = 'Previous build results'
12241250
msg_2 = ' for {}'.format(', '.join('[{}]({})'.format(builder, url) for builder, url in succ_builders)) # noqa
@@ -1326,39 +1352,6 @@ def fetch_mergeability(mergeable_que):
13261352
mergeable_que.task_done()
13271353

13281354

1329-
def check_timeout(states, queue_handler):
1330-
while True:
1331-
try:
1332-
for repo_label, repo_states in states.items():
1333-
for num, state in repo_states.items():
1334-
_timout = time.time() - state.test_started >= TEST_TIMEOUT
1335-
if state.status == 'pending' and _timout:
1336-
print('* Test timed out: {}'.format(state))
1337-
1338-
state.merge_sha = ''
1339-
state.save()
1340-
state.set_status('failure')
1341-
1342-
desc = 'Test timed out'
1343-
utils.github_create_status(
1344-
state.get_repo(),
1345-
state.head_sha,
1346-
'failure',
1347-
'',
1348-
desc,
1349-
context='homu')
1350-
state.add_comment(':boom: {}'.format(desc))
1351-
1352-
queue_handler()
1353-
1354-
except Exception:
1355-
print('* Error while checking timeout')
1356-
traceback.print_exc()
1357-
1358-
finally:
1359-
time.sleep(3600)
1360-
1361-
13621355
def synchronize(repo_label, repo_cfg, logger, gh, states, repos, db, mergeable_que, my_username, repo_labels): # noqa
13631356
logger.info('Synchronizing {}...'.format(repo_label))
13641357

@@ -1666,7 +1659,6 @@ def queue_handler():
16661659
]).start()
16671660

16681661
Thread(target=fetch_mergeability, args=[mergeable_que]).start()
1669-
Thread(target=check_timeout, args=[states, queue_handler]).start()
16701662

16711663
queue_handler()
16721664

0 commit comments

Comments
 (0)