Skip to content

Commit 26af57f

Browse files
authored
Refactor requesting new build tasks (#207)
* Refactor requesting new build tasks * Fix review comments
1 parent d688966 commit 26af57f

File tree

2 files changed

+44
-29
lines changed

2 files changed

+44
-29
lines changed

build_node/build_node_builder.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
import random
1212
import typing
1313
import urllib.parse
14+
from queue import Queue
1415

1516
import requests
1617
import requests.adapters
@@ -41,7 +42,7 @@ def __init__(
4142
thread_num,
4243
terminated_event,
4344
graceful_terminated_event,
44-
task_queue,
45+
task_queue: Queue,
4546
):
4647
"""
4748
Build thread initialization.
@@ -56,6 +57,8 @@ def __init__(
5657
Shows, if process got "kill -15" signal.
5758
graceful_terminated_event : threading.Event
5859
Shows, if process got "kill -10" signal.
60+
task_queue: queue.Queue
61+
Shared queue with build tasks
5962
"""
6063
super().__init__(
6164
thread_num=thread_num,
@@ -243,6 +246,7 @@ def run(self):
243246
# without root permissions
244247
rm_sudo(task_dir)
245248
self.__builder = None
249+
self.__task_queue.task_done()
246250

247251
@measure_stage("cas_notarize_artifacts")
248252
def __cas_notarize_artifacts(

build_node/build_node_supervisor.py

Lines changed: 39 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import logging
22
import traceback
33
import urllib.parse
4+
from queue import Queue
45

56
import requests
67
import requests.adapters
@@ -14,7 +15,13 @@
1415

1516
class BuilderSupervisor(BaseSupervisor):
1617

17-
def __init__(self, config, builders, terminated_event, task_queue):
18+
def __init__(
19+
self,
20+
config,
21+
builders,
22+
terminated_event,
23+
task_queue: Queue,
24+
):
1825
self.__session = None
1926
self.__task_queue = task_queue
2027
self.__cached_config = TTLCache(
@@ -44,42 +51,47 @@ def __generate_request_session(self):
4451
self.__session.mount('https://', adapter)
4552

4653
def __request_build_task(self):
47-
if not self.__task_queue.full():
48-
supported_arches = [self.config.base_arch]
49-
excluded_packages = self.get_excluded_packages()
50-
if self.config.base_arch == 'x86_64':
51-
supported_arches.append('i686')
52-
if self.config.build_src:
53-
supported_arches.append('src')
54-
full_url = urllib.parse.urljoin(
55-
self.config.master_url, 'build_node/get_task'
54+
if self.__task_queue.unfinished_tasks >= self.config.threads_count:
55+
return {}
56+
supported_arches = [self.config.base_arch]
57+
excluded_packages = self.get_excluded_packages()
58+
if self.config.base_arch == 'x86_64':
59+
supported_arches.append('i686')
60+
if self.config.build_src:
61+
supported_arches.append('src')
62+
full_url = urllib.parse.urljoin(
63+
self.config.master_url, 'build_node/get_task'
64+
)
65+
data = {
66+
'supported_arches': supported_arches,
67+
'excluded_packages': excluded_packages,
68+
}
69+
try:
70+
response = self.__session.post(
71+
full_url,
72+
json=data,
73+
timeout=self.config.request_timeout,
5674
)
57-
data = {
58-
'supported_arches': supported_arches,
59-
'excluded_packages': excluded_packages,
60-
}
61-
try:
62-
response = self.__session.post(
63-
full_url, json=data, timeout=self.config.request_timeout
64-
)
65-
response.raise_for_status()
66-
return response.json()
67-
except Exception:
68-
logging.error(
69-
"Can't report active task to master:\n%s",
70-
traceback.format_exc(),
71-
)
75+
response.raise_for_status()
76+
return response.json()
77+
except Exception:
78+
logging.exception(
79+
"Failed to request build task from master:",
80+
)
81+
return {}
7282

7383
def __report_active_tasks(self):
7484
active_tasks = self.get_active_tasks()
75-
logging.debug('Sending active tasks: {}'.format(active_tasks))
85+
logging.debug('Sending active tasks: %s', active_tasks)
7686
full_url = urllib.parse.urljoin(
7787
self.config.master_url, 'build_node/ping'
7888
)
7989
data = {'active_tasks': [int(item) for item in active_tasks]}
8090
try:
8191
self.__session.post(
82-
full_url, json=data, timeout=self.config.request_timeout
92+
full_url,
93+
json=data,
94+
timeout=self.config.request_timeout,
8395
)
8496
except Exception:
8597
logging.error(
@@ -110,7 +122,6 @@ def get_excluded_packages(self):
110122
logging.debug('Excluded packages in this node: %s', excluded_packages)
111123
return excluded_packages
112124

113-
114125
def run(self):
115126
self.__generate_request_session()
116127
while not self.terminated_event.is_set():

0 commit comments

Comments
 (0)