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

Run version worker delayed #5361

Merged
merged 2 commits into from
Sep 29, 2019
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
2 changes: 1 addition & 1 deletion src/main/java/org/jabref/JabRefGUI.java
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ public JabRefGUI(Stage mainStage, List<ParserResult> argsDatabases, boolean isBl

openWindow(mainStage);
new VersionWorker(Globals.BUILD_INFO.getVersion(), Globals.prefs.getVersionPreferences().getIgnoredVersion(), mainFrame.getDialogService(), Globals.TASK_EXECUTOR)
.checkForNewVersionAsync(false);
.checkForNewVersionDelayed();
}

private void openWindow(Stage mainStage) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,6 @@ public SearchForUpdateAction(BuildInfo buildInfo, VersionPreferences versionPref
@Override
public void execute() {
new VersionWorker(buildInfo.getVersion(), versionPreferences.getIgnoredVersion(), dialogService, taskExecutor)
.checkForNewVersionAsync(true);
.checkForNewVersionAsync();
}
}
17 changes: 11 additions & 6 deletions src/main/java/org/jabref/gui/help/VersionWorker.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import java.util.List;
import java.util.Objects;
import java.util.Optional;
import java.util.concurrent.TimeUnit;

import org.jabref.gui.DialogService;
import org.jabref.gui.util.BackgroundTask;
Expand Down Expand Up @@ -54,16 +55,20 @@ private Optional<Version> getNewVersion() throws IOException {
return installedVersion.shouldBeUpdatedTo(availableVersions);
}

/**
* @param manualExecution if this versions check is executed automatically (eg. on startup) or manually by the user
*/
public void checkForNewVersionAsync(boolean manualExecution) {
public void checkForNewVersionAsync() {
BackgroundTask.wrap(this::getNewVersion)
.onSuccess(version -> showUpdateInfo(version, manualExecution))
.onFailure(exception -> showConnectionError(exception, manualExecution))
.onSuccess(version -> showUpdateInfo(version, true))
.onFailure(exception -> showConnectionError(exception, true))
.executeWith(taskExecutor);
}

public void checkForNewVersionDelayed() {
BackgroundTask.wrap(this::getNewVersion)
.onSuccess(version -> showUpdateInfo(version, false))
.onFailure(exception -> showConnectionError(exception, false))
.scheduleWith(taskExecutor, 30, TimeUnit.SECONDS);
}

/**
* Prints the connection problem to the status bar and shows a dialog if it was executed manually
*/
Expand Down
5 changes: 5 additions & 0 deletions src/main/java/org/jabref/gui/util/BackgroundTask.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import java.util.concurrent.Callable;
import java.util.concurrent.Future;
import java.util.concurrent.TimeUnit;
import java.util.function.Consumer;
import java.util.function.Function;

Expand Down Expand Up @@ -149,6 +150,10 @@ public Future<?> executeWith(TaskExecutor taskExecutor) {
return taskExecutor.execute(this);
}

public Future<?> scheduleWith(TaskExecutor taskExecutor, long delay, TimeUnit unit) {
return taskExecutor.schedule(this, delay, unit);
}

/**
* Sets the {@link Runnable} that is invoked after the task is finished, irrespectively if it was successful or
* failed with an error.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,11 @@ public <V> Future<V> execute(Task<V> task) {
return task;
}

@Override
public <V> Future<?> schedule(BackgroundTask<V> task, long delay, TimeUnit unit) {
return execute(task);
}

@Override
public void shutdown() {
// Nothing to do here
Expand Down
15 changes: 12 additions & 3 deletions src/main/java/org/jabref/gui/util/DefaultTaskExecutor.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@
import java.util.concurrent.Executors;
import java.util.concurrent.Future;
import java.util.concurrent.FutureTask;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;
import java.util.function.Consumer;

import javafx.application.Platform;
Expand All @@ -25,7 +27,8 @@ public class DefaultTaskExecutor implements TaskExecutor {

private static final Logger LOGGER = LoggerFactory.getLogger(DefaultTaskExecutor.class);

private static final ExecutorService EXECUTOR = Executors.newFixedThreadPool(5);
private final ExecutorService executor = Executors.newFixedThreadPool(5);
private final ScheduledExecutorService scheduledExecutor = Executors.newScheduledThreadPool(2);

/**
*
Expand Down Expand Up @@ -95,13 +98,19 @@ public <V> Future<V> execute(BackgroundTask<V> task) {

@Override
public <V> Future<V> execute(Task<V> task) {
EXECUTOR.submit(task);
executor.submit(task);
return task;
}

@Override
public <V> Future<?> schedule(BackgroundTask<V> task, long delay, TimeUnit unit) {
return scheduledExecutor.schedule(getJavaFXTask(task), delay, unit);
}

@Override
public void shutdown() {
EXECUTOR.shutdownNow();
executor.shutdownNow();
scheduledExecutor.shutdownNow();
}

private <V> Task<V> getJavaFXTask(BackgroundTask<V> task) {
Expand Down
13 changes: 13 additions & 0 deletions src/main/java/org/jabref/gui/util/TaskExecutor.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package org.jabref.gui.util;

import java.util.concurrent.Future;
import java.util.concurrent.TimeUnit;

import javafx.concurrent.Task;

Expand Down Expand Up @@ -29,6 +30,18 @@ public interface TaskExecutor {
*/
<V> Future<V> execute(Task<V> task);

/**
* Submits a one-shot task that becomes enabled after the given delay.
*
* @param task the task to execute
* @param delay the time from now to delay execution
* @param unit the time unit of the delay parameter
* @return a ScheduledFuture representing pending completion of
* the task and whose {@code get()} method will return
* {@code null} upon completion
*/
<V> Future<?> schedule(BackgroundTask<V> task, long delay, TimeUnit unit);

/**
* Shutdown the task executor.
*/
Expand Down
25 changes: 0 additions & 25 deletions src/main/resources/ApplicationInsights.xml

This file was deleted.