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

View File

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

View File

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

View File

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

View File

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