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

View File

@ -4,7 +4,6 @@ import me.tongfei.progressbar.ProgressBar;
import me.tongfei.progressbar.ProgressBarBuilder;
import me.tongfei.progressbar.ProgressBarStyle;
import net.horizoncode.sysbackup.tasks.Task;
import net.horizoncode.sysbackup.threading.ThreadPool;
import net.lingala.zip4j.ZipFile;
import net.lingala.zip4j.progress.ProgressMonitor;
@ -14,11 +13,9 @@ import java.nio.file.Paths;
public class FileSystemTask extends Task {
private final File target;
private final ThreadPool threadPool;
private File outputZipFile;
public FileSystemTask(String folderOrFilePath, File outputZipFile) {
this.threadPool = new ThreadPool(3, 10);
this.target = Paths.get(folderOrFilePath).toFile();
if (!target.exists()) {
onDone();
@ -32,38 +29,33 @@ public class FileSystemTask extends Task {
@Override
public void start() {
threadPool
.getPool()
.submit(
() -> {
try (ZipFile zipFile = new ZipFile(outputZipFile)) {
System.out.println("Indexing files...");
ProgressMonitor progressMonitor = zipFile.getProgressMonitor();
zipFile.setRunInThread(true);
zipFile.addFolder(target);
ProgressBarBuilder pbb =
new ProgressBarBuilder()
.setStyle(ProgressBarStyle.ASCII)
.setInitialMax(progressMonitor.getTotalWork())
.setTaskName("Adding Files...")
.setUnit("MiB", 1048576);
try (ZipFile zipFile = new ZipFile(outputZipFile)) {
System.out.println("Indexing files...");
ProgressMonitor progressMonitor = zipFile.getProgressMonitor();
zipFile.setRunInThread(true);
zipFile.addFolder(target);
ProgressBarBuilder pbb =
new ProgressBarBuilder()
.setStyle(ProgressBarStyle.ASCII)
.setInitialMax(progressMonitor.getTotalWork())
.setTaskName("Adding Files...")
.setUnit("MiB", 1048576);
try (ProgressBar pb = pbb.build()) {
while (!progressMonitor.getState().equals(ProgressMonitor.State.READY)) {
pb.stepTo(progressMonitor.getWorkCompleted());
Thread.sleep(100);
}
pb.stepTo(progressMonitor.getTotalWork());
} catch (Exception exception) {
exception.printStackTrace();
onDone();
}
progressMonitor.endProgressMonitor();
onDone();
} catch (Exception ex) {
ex.printStackTrace();
onDone();
}
});
try (ProgressBar pb = pbb.build()) {
while (!progressMonitor.getState().equals(ProgressMonitor.State.READY)) {
pb.stepTo(progressMonitor.getWorkCompleted());
Thread.sleep(100);
}
pb.stepTo(progressMonitor.getTotalWork());
} catch (Exception exception) {
exception.printStackTrace();
onDone();
}
progressMonitor.endProgressMonitor();
onDone();
} catch (Exception ex) {
ex.printStackTrace();
onDone();
}
}
}