Skip to content

Commit

Permalink
refactor: Iterate through available games
Browse files Browse the repository at this point in the history
Instead of needing to setup utilities to run per game, we can iterate through. There is
likely scope for this to be improved further, but this will bring ksp2 on reasonably
quickly.
  • Loading branch information
techman83 committed Apr 7, 2023
1 parent 1a7694e commit 4a28f69
Show file tree
Hide file tree
Showing 5 changed files with 59 additions and 25 deletions.
13 changes: 13 additions & 0 deletions netkan/netkan/cli/common.py
Original file line number Diff line number Diff line change
Expand Up @@ -182,6 +182,7 @@ class SharedArgs:
user: str
_debug: bool
_ssh_key: str
_games: List[str]

def __init__(self) -> None:
self._environment_data = None
Expand Down Expand Up @@ -227,6 +228,18 @@ def game(self, game: str) -> Game:
setattr(self, f'_game_{game_id}', Game(game_id, self))
return getattr(self, f'_game_{game_id}')

@property
def games(self) -> List[str]:
if getattr(self, '_games', None) is None:
games = set()
for arg in ['ckanmeta_remotes', 'netkan_remotes', 'ia_collections', 'repos']:
if arg not in vars(self):
continue
games.update([x.split('=', maxsplit=1)[0]
for x in getattr(self, arg)])
self._games = sorted(games) # sorts + casts to list type
return self._games


pass_state = click.make_pass_decorator(
SharedArgs, ensure=True) # pylint: disable=invalid-name
Expand Down
19 changes: 10 additions & 9 deletions netkan/netkan/cli/services.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,15 +44,16 @@ def scheduler(
min_cpu: int,
min_io: int
) -> None:
game = common.game(common.game_id)
sched = NetkanScheduler(
common, game.inflation_queue, common.token, game.name,
nonhooks_group=(group in ('all', 'nonhooks')),
webhooks_group=(group in ('all', 'webhooks')),
)
if sched.can_schedule(max_queued, common.dev, min_cpu, min_io):
sched.schedule_all_netkans()
logging.info("NetKANs submitted to %s", game.inflation_queue)
for game_id in common.games:
game = common.game(game_id)
sched = NetkanScheduler(
common, game.inflation_queue, common.token, game.name,
nonhooks_group=(group in ('all', 'nonhooks')),
webhooks_group=(group in ('all', 'webhooks')),
)
if sched.can_schedule(max_queued, common.dev, min_cpu, min_io):
sched.schedule_all_netkans()
logging.info("NetKANs submitted to %s", game.inflation_queue)


@click.command()
Expand Down
26 changes: 14 additions & 12 deletions netkan/netkan/cli/utilities.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,24 +29,26 @@
@common_options
@pass_state
def auto_freezer(common: SharedArgs, days_limit: int, days_till_ignore: int) -> None:
afr = AutoFreezer(
common.game(common.game_id).netkan_repo,
common.game(common.game_id).github_pr,
)
afr.freeze_idle_mods(days_limit, days_till_ignore)
afr.mark_frozen_mods()
for game_id in common.games:
afr = AutoFreezer(
common.game(game_id).netkan_repo,
common.game(game_id).github_pr,
)
afr.freeze_idle_mods(days_limit, days_till_ignore)
afr.mark_frozen_mods()


@click.command()
@common_options
@pass_state
def download_counter(common: SharedArgs) -> None:
logging.info('Starting Download Count Calculation...')
DownloadCounter(
common.game(common.game_id).ckanmeta_repo,
common.token
).update_counts()
logging.info('Download Counter completed!')
for game_id in common.games:
logging.info('Starting Download Count Calculation (%s)...', game_id)
DownloadCounter(
common.game(game_id).ckanmeta_repo,
common.token
).update_counts()
logging.info('Download Counter completed! (%s)', game_id)


@click.command()
Expand Down
22 changes: 22 additions & 0 deletions netkan/tests/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,28 @@ def test_shared_unset_arg_exits(self):
shared.ckanmeta_remote # pylint: disable=pointless-statement
self.assertEqual(error.exception.code, 1)

def test_shared_games_none(self):
shared = SharedArgs()
self.assertEqual(shared.games, [])

def test_shared_games_ksp(self):
shared = SharedArgs()
shared.ckanmeta_remotes = ('ksp=ckan_url',)
shared.repos = ('ksp=ckan',)
self.assertEqual(shared.games, ['ksp'])

def test_shared_games_ksp2(self):
shared = SharedArgs()
shared.ckanmeta_remotes = ('ksp2=ckan_url',)
shared.repos = ('ksp2=ckan',)
self.assertEqual(shared.games, ['ksp2'])

def test_shared_games_multi(self):
shared = SharedArgs()
shared.ckanmeta_remotes = ('ksp2=ckan_url', 'ksp=ckan_url')
shared.repos = ('ksp2=ckan', 'ksp2=ckan')
self.assertEqual(shared.games, ['ksp', 'ksp2'])


class TestGame(SharedArgsHarness):

Expand Down
4 changes: 0 additions & 4 deletions prod-stack.py
Original file line number Diff line number Diff line change
Expand Up @@ -709,7 +709,6 @@
'memory': '156',
'secrets': ['SSH_KEY', 'GH_Token'],
'env': [
('GAME_ID', 'ksp'),
('INFLATION_QUEUES', INFLATION_QUEUES),
('NETKAN_REMOTES', NETKAN_REMOTES),
('CKANMETA_REMOTES', CKANMETA_REMOTES),
Expand All @@ -728,7 +727,6 @@
'memory': '156',
'secrets': ['SSH_KEY', 'GH_Token'],
'env': [
('GAME_ID', 'ksp'),
('INFLATION_QUEUES', INFLATION_QUEUES),
('NETKAN_REMOTES', NETKAN_REMOTES),
('CKANMETA_REMOTES', CKANMETA_REMOTES),
Expand Down Expand Up @@ -806,7 +804,6 @@
'SSH_KEY', 'GH_Token',
],
'env': [
('GAME_ID', 'ksp'),
('NETKAN_REMOTES', NETKAN_REMOTES),
('CKANMETA_REMOTES', CKANMETA_REMOTES),
],
Expand Down Expand Up @@ -850,7 +847,6 @@
'name': 'AutoFreezer',
'command': 'auto-freezer',
'env': [
('GAME_ID', 'ksp'),
('NETKAN_REMOTES', NETKAN_REMOTES),
('CKAN_USER', NETKAN_USER),
('CKAN_REPOS', NETKAN_REPOS),
Expand Down

0 comments on commit 4a28f69

Please sign in to comment.