|
4 | 4 | """ |
5 | 5 | from __future__ import annotations |
6 | 6 |
|
7 | | -from concurrent.futures import ThreadPoolExecutor |
| 7 | +from concurrent.futures import ThreadPoolExecutor, as_completed |
8 | 8 |
|
9 | 9 | from lib.host import Host |
10 | 10 | from lib.pool import NotAMasterHostError, Pool |
@@ -45,17 +45,40 @@ def update_pools(inventory: Inventory) -> None: |
45 | 45 |
|
46 | 46 | # update master hosts |
47 | 47 | 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 |
50 | 61 |
|
51 | 62 | # update other hosts |
52 | 63 | with ThreadPoolExecutor() as executor: |
| 64 | + future_other_hosts = {} |
53 | 65 | for p in pools: |
54 | 66 | # 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:]: |
56 | 68 | # repos are the same as for the master host |
57 | 69 | 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 |
59 | 82 |
|
60 | 83 | # Snapshot creation |
61 | 84 | for hosting_pool, nested in nested_hosts.items(): |
|
0 commit comments