Skip to content

Commit

Permalink
fix(threads): Cap updating threads
Browse files Browse the repository at this point in the history
Submit each repository update runnable to an executor service with fixed number of threads.

This allows having a maximum cap number of threads for projects with 10s or 20s repos.

Closes #55

Signed-off-by: jchrist <[email protected]>
  • Loading branch information
JChrist committed Jan 30, 2024
1 parent a18d667 commit e490487
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 6 deletions.
10 changes: 5 additions & 5 deletions src/main/java/gr/jchrist/gitextender/GitExtenderUpdateAll.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@

import java.util.List;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.Executors;
import java.util.concurrent.atomic.AtomicBoolean;
import java.util.stream.Collectors;

Expand Down Expand Up @@ -122,13 +123,12 @@ private void updateRepositories(@NotNull Project project, @NotNull List<GitRepos
GitExtenderSettingsHandler settingsHandler = new GitExtenderSettingsHandler();
final GitExtenderSettings settings = settingsHandler.loadSettings();
this.updateCountDown = new CountDownLatch(repositories.size());
final var es = Executors.newFixedThreadPool(Math.min(repositories.size(), 3));

Check warning on line 126 in src/main/java/gr/jchrist/gitextender/GitExtenderUpdateAll.java

View check run for this annotation

Codecov / codecov/patch

src/main/java/gr/jchrist/gitextender/GitExtenderUpdateAll.java#L126

Added line #L126 was not covered by tests
repositories.forEach(repo ->
new Thread(new BackgroundableRepoUpdateTask(repo,
VcsImplUtil.getShortVcsRootName(repo.getProject(), repo.getRoot()),
settings, updateCountDown))
.start());
es.submit(new BackgroundableRepoUpdateTask(repo, VcsImplUtil.getShortVcsRootName(repo.getProject(), repo.getRoot()),

Check warning on line 128 in src/main/java/gr/jchrist/gitextender/GitExtenderUpdateAll.java

View check run for this annotation

Codecov / codecov/patch

src/main/java/gr/jchrist/gitextender/GitExtenderUpdateAll.java#L128

Added line #L128 was not covered by tests
settings, updateCountDown)));
UpdateExecutingBackgroundTask ubt = new UpdateExecutingBackgroundTask(project, "GitExtender Update All Repos",
accessToken, updateCountDown, executingFlag);
accessToken, updateCountDown, es, executingFlag);
//ubt.queue();
new Thread(ubt::queue).start();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,21 +9,24 @@
import org.jetbrains.annotations.NotNull;

import java.util.concurrent.CountDownLatch;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicBoolean;

public class UpdateExecutingBackgroundTask extends Task.Backgroundable {
private static final Logger logger = Logger.getInstance(UpdateExecutingBackgroundTask.class);
protected AccessToken accessToken;
protected CountDownLatch updateCountDownLatch;
protected ExecutorService executor;
protected AtomicBoolean executingFlag;

public UpdateExecutingBackgroundTask(
Project project, String title,
AccessToken accessToken, CountDownLatch updatingLatch, AtomicBoolean executing) {
AccessToken accessToken, CountDownLatch updatingLatch, ExecutorService executor, AtomicBoolean executing) {
super(project, title, false, PerformInBackgroundOption.ALWAYS_BACKGROUND);
this.accessToken = accessToken;
this.updateCountDownLatch = updatingLatch;
this.executor = executor;

Check warning on line 29 in src/main/java/gr/jchrist/gitextender/UpdateExecutingBackgroundTask.java

View check run for this annotation

Codecov / codecov/patch

src/main/java/gr/jchrist/gitextender/UpdateExecutingBackgroundTask.java#L29

Added line #L29 was not covered by tests
this.executingFlag = executing;
}

Expand All @@ -36,6 +39,11 @@ public void run(@NotNull ProgressIndicator indicator) {
} catch (Exception e) {
logger.warn("error awaiting update latch!", e);
}
try {
executor.shutdownNow();
} catch (Exception e) {
logger.warn("error shutting down executor", e);
}

Check warning on line 46 in src/main/java/gr/jchrist/gitextender/UpdateExecutingBackgroundTask.java

View check run for this annotation

Codecov / codecov/patch

src/main/java/gr/jchrist/gitextender/UpdateExecutingBackgroundTask.java#L43-L46

Added lines #L43 - L46 were not covered by tests
logger.debug("update finished - " + Thread.currentThread().getName());
//the last task finished should clean up, release project changes and show info notification
accessToken.finish();
Expand Down

0 comments on commit e490487

Please sign in to comment.