Skip to content

Commit

Permalink
prepare initial API to adjust worker amount from outside
Browse files Browse the repository at this point in the history
  • Loading branch information
GB609 authored Mar 7, 2025
1 parent 558f2cc commit dab0f15
Showing 1 changed file with 28 additions and 1 deletion.
29 changes: 28 additions & 1 deletion minigalaxy/download_manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -239,6 +239,31 @@ def put_in_proper_queue(self, download):
# Add other items to the UI queue
self.__ui_queue.put(QueuedDownloadItem(download, 0))

def adjust_game_workers(self, new_amount, stop_active=False):
'''
This method allows to dynamically change the number of download threads used by the game queue.
The new number is compared against the current value set in config, afterwards the config value is updated.
1. When greater, new threads are spawned for this queue.
2. When smaller, idle threads will orderly terminate until len(workers[game_queue]) == new_amount (TBD)
2a. Threads which are currently busy will only be actively stopped when stop_active=True is given. (TBD)
In that case, the download with the least amount of progress is stopped and requeued afterwards
'''

difference = new_amount - self.config.max_parallel_game_downloads
if difference == 0 or new_amount < 1:
return

self.config.max_parallel_game_downloads = new_amount
if difference > 0:
self.__initialize_workers(self.__game_queue, difference)
return

if not stop_active:
return

# TODO:
# Find a way to fetch and count active downloads from game queue

def download_now(self, download):
"""
Download an item with a higher priority
Expand Down Expand Up @@ -422,7 +447,9 @@ def __download_file(self, download, download_queue):
last_error = str(e) # FIXME: need a way to remove token from error
download_attempt += 1
# TODO: maybe add an incrementally growing sleep time instead
time.sleep(10) # don't immediately use up all retries
if download_attempt < download_max_attempts:
# only sleep when there are retries left
time.sleep(10) # don't immediately use up all retries

if download_attempt == download_max_attempts:
result = DownloadState.FAILED
Expand Down

0 comments on commit dab0f15

Please sign in to comment.