Skip to content

Commit

Permalink
fix: use thread pool to update manually
Browse files Browse the repository at this point in the history
  • Loading branch information
hizumiaoba committed Dec 29, 2021
1 parent c0bf391 commit 055b2ed
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 15 deletions.
18 changes: 16 additions & 2 deletions generated/database.json
Original file line number Diff line number Diff line change
Expand Up @@ -14100,10 +14100,10 @@
}, {
"attribute" : "クール",
"name" : "Drastic Melody",
"difficulty" : "MASTER+",
"difficulty" : "ⓁMASTER+",
"level" : 29,
"notes" : 926,
"albumType" : "Not-implemented"
"albumType" : "ALBUM B"
}, {
"attribute" : "キュート",
"name" : "いとしーさー♥",
Expand Down Expand Up @@ -14580,5 +14580,19 @@
"level" : 16,
"notes" : 405,
"albumType" : "Not-Implemented"
}, {
"attribute" : "クール",
"name" : "Drastic Melody",
"difficulty" : "MASTER+",
"level" : 29,
"notes" : 934,
"albumType" : "ALBUM B"
}, {
"attribute" : "全タイプ",
"name" : "夕映えプレゼント",
"difficulty" : "MASTER+",
"level" : 29,
"notes" : 887,
"albumType" : "ALBUM B"
} ]
}
17 changes: 10 additions & 7 deletions src/com/ranfa/lib/ManualUpdateThreadImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

import java.util.ArrayList;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.Executor;
import java.util.concurrent.Executors;
import java.util.function.BiConsumer;
Expand Down Expand Up @@ -45,12 +44,16 @@ public void run() {
this.logger.info("Checking database updates...");
CompletableFuture<ArrayList<Song>> webData = CompletableFuture.supplyAsync(Scraping::getWholeData, this.executor);
CompletableFuture<ArrayList<Song>> localData = CompletableFuture.supplyAsync(Scraping::getFromJson, this.executor);
try {
this.updateConsumer.accept(webData.get(), localData.get());
} catch (InterruptedException | ExecutionException e) {
this.logger.warn("Update failed.", e);
}
flag = true;
CompletableFuture<Void> afterUpdateFuture = webData.thenAcceptBothAsync(localData, this.updateConsumer, this.executor);
afterUpdateFuture.whenCompleteAsync((ret, e) -> {
if(e != null) {
this.logger.warn("Manual update process has been ended with exception.", e);
} else {
this.logger.info("Manual update process has been ended successfully.");
flag = true;
}

}, this.executor);
}

public boolean getFlag() {
Expand Down
11 changes: 5 additions & 6 deletions src/com/ranfa/main/DelesteRandomSelector.java
Original file line number Diff line number Diff line change
Expand Up @@ -154,7 +154,7 @@ public DelesteRandomSelector() {
};
Runnable setEnabled = () -> {
try {
Thread.sleep(1000);
Thread.sleep(3 * 1000L);
} catch (InterruptedException e1) {
this.logger.error("Thread has been interrupted during waiting cooldown.", e1);
}
Expand Down Expand Up @@ -368,18 +368,17 @@ public DelesteRandomSelector() {
this.btnManualUpdate = new JButton(Messages.MSGManualUpdate.toString());
this.btnManualUpdate.addActionListener(e -> {
this.impl = new ManualUpdateThreadImpl();
this.manualUpdateThread = new Thread(this.impl);
this.manualUpdateThread.setName("ManualUpdate-thread");
this.manualUpdateThread.setDaemon(false);
this.manualUpdateThread.start();
es.submit(this.impl);
});
this.panelEast.add(this.btnManualUpdate, "1, 9");
this.panelEast.add(this.btnTwitterIntegration, "1, 11");

this.btnExit = new JButton(Messages.MSGTerminate.toString());
this.btnExit.addActionListener(e -> {
if(DelesteRandomSelector.this.softwareUpdateFuture.isDone() || DelesteRandomSelector.this.albumTypeEstimateFuture.isDone() || !this.impl.getFlag()) {
DelesteRandomSelector.this.logger.info("Requested Exit by Button");
DelesteRandomSelector.this.logger.info("Requested Exit by Button.");
this.logger.info("Shut down thread pool.");
es.shutdown();
System.exit(0);
} else {
JOptionPane.showMessageDialog(null, Messages.MSGInternalYpdateNotDoneYet.toString());
Expand Down

0 comments on commit 055b2ed

Please sign in to comment.