Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add threads flag to use threads #132

Merged
merged 4 commits into from
Apr 2, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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)

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
Loading