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 2 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
25 changes: 17 additions & 8 deletions 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 @@ -17,17 +18,19 @@ class CleanupSummary:

class ArtifactoryCleanup:
def __init__(
self,
session: Session,
policies: List[CleanupPolicy],
destroy: bool,
today: date,
ignore_not_found: bool,
self,
session: Session,
policies: List[CleanupPolicy],
destroy: bool,
today: date,
ignore_not_found: bool,
worker_count: int,
Rommmmm marked this conversation as resolved.
Show resolved Hide resolved
):
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,9 +59,15 @@ 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_with_context(artifact, policy, test_ctx_mgr, get_name_for_ci, destroy, ignore_not_found):
Copy link
Member

@allburov allburov Feb 20, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

only artifact argument is required here as I can see, so the signature should be something like
All other methods are imported, flags we can get from self as well.

def _delete(artifact):
    with test_ctx_mgr(get_name_for_ci(artifact)):
        policy.delete(artifact, destroy=self.destroy)

executor.submit(self._delete, artifact=artifact)

Also we could switch to https://docs.python.org/3/library/concurrent.futures.html#concurrent.futures.Executor.map

executor.map(self._delete, artifacts_to_remove)

P.S.
Well, it'll create mess in output anyway, because all thread will print in the same output... But it's the useful case for people who doesn't care about CI, agree.
The task is not so easy if we think about that, especially in case where you use it in CI.

with test_ctx_mgr(get_name_for_ci(artifact)):
policy.delete(artifact, destroy=self.destroy)
return policy.delete(artifact, destroy=destroy, ignore_not_found=ignore_not_found)

with ThreadPoolExecutor(max_workers=int(self.worker_count)) as executor:
for artifact in artifacts_to_remove:
executor.submit(
_delete_with_context, artifact, policy, test_ctx_mgr, get_name_for_ci, self.destroy, self.ignore_not_found
)

# Show summary
print(f"Deleted artifacts count: {len(artifacts_to_remove)}")
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