Do not schedule task if preExecute and postExecute are not overrided.

This commit is contained in:
huanghongxun
2019-01-22 16:46:11 +08:00
parent 78fcc69d29
commit 28d002f6d3
5 changed files with 46 additions and 14 deletions

View File

@@ -104,6 +104,11 @@ public class LibraryDownloadTask extends Task {
}
}
@Override
public boolean doPreExecute() {
return true;
}
@Override
public void preExecute() throws Exception {
Optional<Path> libPath = cacheRepository.getLibrary(originalLibrary);
@@ -135,6 +140,11 @@ public class LibraryDownloadTask extends Task {
}
}
@Override
public boolean doPostExecute() {
return true;
}
@Override
public void postExecute() throws Exception {
if (!cached)

View File

@@ -64,6 +64,11 @@ public class ModpackUpdateTask extends Task {
FileUtils.copyDirectory(repository.getVersionRoot(id).toPath(), backupFolder);
}
@Override
public boolean doPostExecute() {
return true;
}
@Override
public void postExecute() throws Exception {
if (isDependenciesSucceeded()) {

View File

@@ -96,6 +96,11 @@ public final class MultiMCModpackInstallTask extends Task {
return dependencies;
}
@Override
public boolean doPreExecute() {
return true;
}
@Override
public void preExecute() throws Exception {
File run = repository.getRunDirectory(name);

View File

@@ -145,6 +145,10 @@ public abstract class Task {
this.variables = variables;
}
public boolean doPreExecute() {
return false;
}
/**
* @throws InterruptedException if current thread is interrupted
* @see Thread#isInterrupted
@@ -157,6 +161,10 @@ public abstract class Task {
*/
public abstract void execute() throws Exception;
public boolean doPostExecute() {
return false;
}
/**
* @throws InterruptedException if current thread is interrupted
* @see Thread#isInterrupted

View File

@@ -149,13 +149,15 @@ public final class TaskExecutor {
try {
task.setVariables(variables);
try {
task.getScheduler().schedule(task::preExecute).get();
} catch (ExecutionException e) {
if (e.getCause() instanceof Exception)
throw (Exception) e.getCause();
else
throw e;
if (task.doPreExecute()) {
try {
task.getScheduler().schedule(task::preExecute).get();
} catch (ExecutionException e) {
if (e.getCause() instanceof Exception)
throw (Exception) e.getCause();
else
throw e;
}
}
boolean doDependentsSucceeded = executeTasks(task.getDependents());
@@ -194,13 +196,15 @@ public final class TaskExecutor {
if (doDependenciesSucceeded)
task.setDependenciesSucceeded();
try {
task.getScheduler().schedule(task::postExecute).get();
} catch (ExecutionException e) {
if (e.getCause() instanceof Exception)
throw (Exception) e.getCause();
else
throw e;
if (task.doPostExecute()) {
try {
task.getScheduler().schedule(task::postExecute).get();
} catch (ExecutionException e) {
if (e.getCause() instanceof Exception)
throw (Exception) e.getCause();
else
throw e;
}
}
flag = true;