Skip to content

Commit 889802f

Browse files
committed
tools/update: stop script on update failure
Rework multithread part of update script to be able to stop when there is a host that cannot be updated. Signed-off-by: Olivier Hoareau <olivier.hoareau@vates.tech>
1 parent 0d467f1 commit 889802f

1 file changed

Lines changed: 28 additions & 5 deletions

File tree

lib/tools/tasks/update.py

Lines changed: 28 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
"""
55
from __future__ import annotations
66

7-
from concurrent.futures import ThreadPoolExecutor
7+
from concurrent.futures import ThreadPoolExecutor, as_completed
88

99
from lib.host import Host
1010
from lib.pool import NotAMasterHostError, Pool
@@ -45,17 +45,40 @@ def update_pools(inventory: Inventory) -> None:
4545

4646
# update master hosts
4747
with ThreadPoolExecutor() as executor:
48-
for p in pools:
49-
executor.submit(p.master.update, inventory_hosts[p.master.hostname_or_ip]["repositories"])
48+
future_masters = {executor.submit(
49+
p.master.update, inventory_hosts[p.master.hostname_or_ip]["repositories"]): p.master for p in pools}
50+
for future in as_completed(future_masters):
51+
future_master = future_masters[future]
52+
try:
53+
future.result()
54+
except Exception as exc:
55+
logger.error(f"Updating pool has failed! The master {future_master} cannot be updated.")
56+
logger.info(
57+
"*** Due to previous error, the pool updating task will stop. "
58+
"Waiting for running updates to finish if any. ***"
59+
)
60+
raise exc
5061

5162
# update other hosts
5263
with ThreadPoolExecutor() as executor:
64+
future_other_hosts = {}
5365
for p in pools:
5466
# omit first item because it is the pool's master
55-
for other_host in p.hosts[1:]:
67+
for h in p.hosts[1:]:
5668
# repos are the same as for the master host
5769
repos = inventory_hosts[p.master.hostname_or_ip]["repositories"]
58-
executor.submit(other_host.update, repos)
70+
future_other_hosts[executor.submit(h.update, repos)] = h
71+
for future in as_completed(future_other_hosts):
72+
other_host = future_other_hosts[future]
73+
try:
74+
future.result()
75+
except Exception as exc:
76+
logger.error(f"Updating pool has failed! The host {other_host} cannot be updated.")
77+
logger.info(
78+
"*** Due to previous error, the pool updating task will stop. "
79+
"Waiting for running updates to finish if any. ***"
80+
)
81+
raise exc
5982

6083
# Snapshot creation
6184
for hosting_pool, nested in nested_hosts.items():

0 commit comments

Comments
 (0)