run each task in a extra thread

This commit is contained in:
HorizonCode 2022-07-04 11:03:10 +02:00
parent e5abc4823d
commit 8f9d7d5297
2 changed files with 33 additions and 39 deletions

View File

@ -6,6 +6,7 @@ import net.horizoncode.sysbackup.config.Config;
import net.horizoncode.sysbackup.tasks.impl.DatabaseTask; import net.horizoncode.sysbackup.tasks.impl.DatabaseTask;
import net.horizoncode.sysbackup.tasks.impl.FileSystemTask; import net.horizoncode.sysbackup.tasks.impl.FileSystemTask;
import net.horizoncode.sysbackup.tasks.impl.VacuumTask; import net.horizoncode.sysbackup.tasks.impl.VacuumTask;
import net.horizoncode.sysbackup.threading.ThreadPool;
import org.apache.commons.io.FilenameUtils; import org.apache.commons.io.FilenameUtils;
import org.tomlj.TomlArray; import org.tomlj.TomlArray;
@ -25,6 +26,7 @@ public class TaskBuilder {
private final String taskName; private final String taskName;
@Builder.Default private final LinkedBlockingQueue<Task> taskList = new LinkedBlockingQueue<>(); @Builder.Default private final LinkedBlockingQueue<Task> taskList = new LinkedBlockingQueue<>();
@Builder.Default @Getter private final ThreadPool threadPool = new ThreadPool(3, 10);
private final File executionPath; private final File executionPath;
@ -74,7 +76,7 @@ public class TaskBuilder {
new VacuumTask(backupDir, unit, value) { new VacuumTask(backupDir, unit, value) {
@Override @Override
public void onDone() { public void onDone() {
executeNextTask(); getThreadPool().getPool().submit(() -> executeNextTask());
} }
}); });
} }
@ -92,7 +94,7 @@ public class TaskBuilder {
new FileSystemTask(target, outputFile) { new FileSystemTask(target, outputFile) {
@Override @Override
public void onDone() { public void onDone() {
executeNextTask(); getThreadPool().getPool().submit(() -> executeNextTask());
} }
}); });
}); });
@ -117,7 +119,7 @@ public class TaskBuilder {
new DatabaseTask(databaseCredentials, outputFile) { new DatabaseTask(databaseCredentials, outputFile) {
@Override @Override
public void onDone() { public void onDone() {
executeNextTask(); getThreadPool().getPool().submit(() -> executeNextTask());
} }
}); });
} else { } else {
@ -125,7 +127,7 @@ public class TaskBuilder {
} }
} }
executeNextTask(); getThreadPool().getPool().submit(this::executeNextTask);
} }
private void executeNextTask() { private void executeNextTask() {

View File

@ -4,7 +4,6 @@ import me.tongfei.progressbar.ProgressBar;
import me.tongfei.progressbar.ProgressBarBuilder; import me.tongfei.progressbar.ProgressBarBuilder;
import me.tongfei.progressbar.ProgressBarStyle; import me.tongfei.progressbar.ProgressBarStyle;
import net.horizoncode.sysbackup.tasks.Task; import net.horizoncode.sysbackup.tasks.Task;
import net.horizoncode.sysbackup.threading.ThreadPool;
import net.lingala.zip4j.ZipFile; import net.lingala.zip4j.ZipFile;
import net.lingala.zip4j.progress.ProgressMonitor; import net.lingala.zip4j.progress.ProgressMonitor;
@ -14,11 +13,9 @@ import java.nio.file.Paths;
public class FileSystemTask extends Task { public class FileSystemTask extends Task {
private final File target; private final File target;
private final ThreadPool threadPool;
private File outputZipFile; private File outputZipFile;
public FileSystemTask(String folderOrFilePath, File outputZipFile) { public FileSystemTask(String folderOrFilePath, File outputZipFile) {
this.threadPool = new ThreadPool(3, 10);
this.target = Paths.get(folderOrFilePath).toFile(); this.target = Paths.get(folderOrFilePath).toFile();
if (!target.exists()) { if (!target.exists()) {
onDone(); onDone();
@ -32,10 +29,6 @@ public class FileSystemTask extends Task {
@Override @Override
public void start() { public void start() {
threadPool
.getPool()
.submit(
() -> {
try (ZipFile zipFile = new ZipFile(outputZipFile)) { try (ZipFile zipFile = new ZipFile(outputZipFile)) {
System.out.println("Indexing files..."); System.out.println("Indexing files...");
ProgressMonitor progressMonitor = zipFile.getProgressMonitor(); ProgressMonitor progressMonitor = zipFile.getProgressMonitor();
@ -64,6 +57,5 @@ public class FileSystemTask extends Task {
ex.printStackTrace(); ex.printStackTrace();
onDone(); onDone();
} }
});
} }
} }