Skip to content

Commit

Permalink
add --worker-count (#132)
Browse files Browse the repository at this point in the history
* add threads flag to use threads and add missing readme for ignore-not-found

* add test context manager inside thread

* review

* review
  • Loading branch information
Rommmmm committed Apr 2, 2024
1 parent c894410 commit e2c4a0d
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 1 deletion.
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,10 @@ artifactory-cleanup --help
- Use CI servers or cron-like utilities to run `artifactory-cleanup` every day (or every hour). TeamCity and GitHub have
built-in support and show additional logs format
- Do not save credentials in the configuration file, use environment variables.
- Use `--ignore-not-found` flag to ignore errors when the repository is not found. It's useful when you have a
configuration for multiple repositories and some of them are not found.
- Use `--worker-count=<WORKER_NUM>` to increase the number of workers. By default, it's 1. It's useful when you have a lot of
artifacts and you want to speed up the process.

## Commands ##

Expand Down
9 changes: 8 additions & 1 deletion artifactory_cleanup/artifactorycleanup.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
from concurrent.futures import ThreadPoolExecutor
from datetime import date
from typing import List, Iterator

Expand All @@ -23,11 +24,13 @@ def __init__(
destroy: bool,
today: date,
ignore_not_found: bool,
worker_count: int,
):
self.session = session
self.policies = policies
self.destroy = destroy
self.ignore_not_found = ignore_not_found
self.worker_count = worker_count

self._init_policies(today)

Expand Down Expand Up @@ -56,10 +59,14 @@ def cleanup(self, block_ctx_mgr, test_ctx_mgr) -> Iterator[CleanupSummary]:
print(f"Found {len(artifacts_to_remove)} artifacts AFTER filtering")

# Delete artifacts
for artifact in artifacts_to_remove:
def _delete(artifact):
with test_ctx_mgr(get_name_for_ci(artifact)):
policy.delete(artifact, destroy=self.destroy, ignore_not_found=self.ignore_not_found)

with ThreadPoolExecutor(max_workers=int(self.worker_count)) as executor:
for artifact in artifacts_to_remove:
executor.submit(_delete, artifact=artifact)

# Show summary
print(f"Deleted artifacts count: {len(artifacts_to_remove)}")
try:
Expand Down
10 changes: 10 additions & 0 deletions artifactory_cleanup/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,14 @@ class ArtifactoryCleanupCLI(cli.Application):
envname="ARTIFACTORY_CLEANUP_IGNORE_NOT_FOUND",
)

_worker_count = cli.SwitchAttr(
"--worker-count",
help="Number of workers to use",
mandatory=False,
default=1,
envname="ARTIFACTORY_CLEANUP_WORKER_COUNT",
)

_days_in_future = cli.SwitchAttr(
"--days-in-future",
help="Simulate future behaviour",
Expand Down Expand Up @@ -156,12 +164,14 @@ def main(self):
session.auth = HTTPBasicAuth(user, password)

self._destroy_or_verbose()
print(f"Using {self._worker_count} workers")
cleanup = ArtifactoryCleanup(
session=session,
policies=policies,
destroy=self._destroy,
today=today,
ignore_not_found=self._ignore_not_found,
worker_count=self._worker_count,
)

# Filter policies by name
Expand Down

0 comments on commit e2c4a0d

Please sign in to comment.