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,38 +29,33 @@ public class FileSystemTask extends Task {
@Override @Override
public void start() { public void start() {
threadPool try (ZipFile zipFile = new ZipFile(outputZipFile)) {
.getPool() System.out.println("Indexing files...");
.submit( ProgressMonitor progressMonitor = zipFile.getProgressMonitor();
() -> { zipFile.setRunInThread(true);
try (ZipFile zipFile = new ZipFile(outputZipFile)) { zipFile.addFolder(target);
System.out.println("Indexing files..."); ProgressBarBuilder pbb =
ProgressMonitor progressMonitor = zipFile.getProgressMonitor(); new ProgressBarBuilder()
zipFile.setRunInThread(true); .setStyle(ProgressBarStyle.ASCII)
zipFile.addFolder(target); .setInitialMax(progressMonitor.getTotalWork())
ProgressBarBuilder pbb = .setTaskName("Adding Files...")
new ProgressBarBuilder() .setUnit("MiB", 1048576);
.setStyle(ProgressBarStyle.ASCII)
.setInitialMax(progressMonitor.getTotalWork())
.setTaskName("Adding Files...")
.setUnit("MiB", 1048576);
try (ProgressBar pb = pbb.build()) { try (ProgressBar pb = pbb.build()) {
while (!progressMonitor.getState().equals(ProgressMonitor.State.READY)) { while (!progressMonitor.getState().equals(ProgressMonitor.State.READY)) {
pb.stepTo(progressMonitor.getWorkCompleted()); pb.stepTo(progressMonitor.getWorkCompleted());
Thread.sleep(100); Thread.sleep(100);
} }
pb.stepTo(progressMonitor.getTotalWork()); pb.stepTo(progressMonitor.getTotalWork());
} catch (Exception exception) { } catch (Exception exception) {
exception.printStackTrace(); exception.printStackTrace();
onDone(); onDone();
} }
progressMonitor.endProgressMonitor(); progressMonitor.endProgressMonitor();
onDone(); onDone();
} catch (Exception ex) { } catch (Exception ex) {
ex.printStackTrace(); ex.printStackTrace();
onDone(); onDone();
} }
});
} }
} }