Skip to content
Snippets Groups Projects
Commit c86a3dbf authored by Petr Bainar's avatar Petr Bainar
Browse files

Merge remote-tracking branch 'origin/master' into formatStatistics

parents d1e22772 1bab16c7
No related branches found
No related tags found
1 merge request!3Format statistics
Showing
with 240 additions and 196 deletions
...@@ -2,15 +2,11 @@ package cz.it4i.fiji.haas; ...@@ -2,15 +2,11 @@ package cz.it4i.fiji.haas;
import java.io.IOException; import java.io.IOException;
import java.io.InputStream; import java.io.InputStream;
import java.io.OutputStream;
import java.nio.file.Files; import java.nio.file.Files;
import java.nio.file.Path; import java.nio.file.Path;
import java.nio.file.StandardOpenOption;
import java.util.Calendar; import java.util.Calendar;
import java.util.Collections; import java.util.Collections;
import java.util.EnumSet;
import java.util.List; import java.util.List;
import java.util.Properties;
import java.util.function.Predicate; import java.util.function.Predicate;
import java.util.function.Supplier; import java.util.function.Supplier;
import java.util.stream.Collectors; import java.util.stream.Collectors;
...@@ -29,8 +25,6 @@ import net.imagej.updater.util.Progress; ...@@ -29,8 +25,6 @@ import net.imagej.updater.util.Progress;
public class Job { public class Job {
private static final String JOB_HAS_DATA_TO_DOWNLOAD_PROPERTY = "job.needDownload";
private static final String JOB_NAME = "job.name"; private static final String JOB_NAME = "job.name";
public static boolean isJobPath(Path p) { public static boolean isJobPath(Path p) {
...@@ -47,27 +41,32 @@ public class Job { ...@@ -47,27 +41,32 @@ public class Job {
private Supplier<HaaSClient> haasClientSupplier; private Supplier<HaaSClient> haasClientSupplier;
// private JobState state; // private JobState state;
private Boolean needsDownload; //private Boolean needsDownload;
private JobInfo jobInfo; private JobInfo jobInfo;
private Long jobId; private Long jobId;
private String name;
private PropertyHolder propertyHolder;
public Job(String name, Path basePath, Supplier<HaaSClient> haasClientSupplier) throws IOException { public Job(String name, Path basePath, Supplier<HaaSClient> haasClientSupplier) throws IOException {
this(haasClientSupplier); this(haasClientSupplier);
HaaSClient client = this.haasClientSupplier.get(); HaaSClient client = this.haasClientSupplier.get();
long id = client.createJob(name, Collections.emptyList()); long id = client.createJob(name, Collections.emptyList());
jobDir = basePath.resolve("" + id); jobDir = basePath.resolve("" + id);
this.name = name; propertyHolder = new PropertyHolder(jobDir.resolve(JOB_INFO_FILE));
Files.createDirectory(jobDir); Files.createDirectory(jobDir);
updateNeedsDownload(); setName(name);
}
public void setName(String name) {
setProperty(JOB_NAME, name);
} }
public Job(Path p, Supplier<HaaSClient> haasClientSupplier) throws IOException { public Job(Path p, Supplier<HaaSClient> haasClientSupplier) {
this(haasClientSupplier); this(haasClientSupplier);
jobDir = p; jobDir = p;
loadJobInfo(); propertyHolder = new PropertyHolder(jobDir.resolve(JOB_INFO_FILE));
updateNeedsDownload();
} }
public void uploadFiles(Iterable<UploadingFile> files, Progress notifier) { public void uploadFiles(Iterable<UploadingFile> files, Progress notifier) {
...@@ -87,13 +86,11 @@ public class Job { ...@@ -87,13 +86,11 @@ public class Job {
client.submitJob(jobId); client.submitJob(jobId);
} }
private Job(Supplier<HaaSClient> haasClientSupplier) throws IOException { private Job(Supplier<HaaSClient> haasClientSupplier) {
this.haasClientSupplier = haasClientSupplier; this.haasClientSupplier = haasClientSupplier;
} }
public boolean needsDownload() {
return needsDownload != null && needsDownload;
}
synchronized public long getJobId() { synchronized public long getJobId() {
if (jobId == null) { if (jobId == null) {
...@@ -103,7 +100,7 @@ public class Job { ...@@ -103,7 +100,7 @@ public class Job {
} }
public void download(Progress notifier) { public void download(Progress notifier) {
download(x -> true, notifier, false); download(x -> true, notifier);
} }
public Path storeDataInWorkdirectory(UploadingFile uploadingFile) throws IOException { public Path storeDataInWorkdirectory(UploadingFile uploadingFile) throws IOException {
...@@ -114,19 +111,8 @@ public class Job { ...@@ -114,19 +111,8 @@ public class Job {
return result; return result;
} }
synchronized public void download(Predicate<String> predicate, Progress notifier, boolean allowAgain) { synchronized public void download(Predicate<String> predicate, Progress notifier) {
if (!allowAgain && !needsDownload()) {
throw new IllegalStateException("Job: " + getJobId() + " doesn't need download");
}
haasClientSupplier.get().download(getJobId(), jobDir, predicate, new P_ProgressNotifierAdapter(notifier)); haasClientSupplier.get().download(getJobId(), jobDir, predicate, new P_ProgressNotifierAdapter(notifier));
if(!allowAgain) {
needsDownload = false;
try {
saveJobinfo();
} catch (IOException e) {
log.error(e);
}
}
} }
public JobState getState() { public JobState getState() {
...@@ -157,14 +143,12 @@ public class Job { ...@@ -157,14 +143,12 @@ public class Job {
return Files.newInputStream(jobDir.resolve(name)); return Files.newInputStream(jobDir.resolve(name));
} }
public void setProperty(String name, String value) throws IOException { public void setProperty(String name, String value) {
Properties prop = loadPropertiesIfExists(); propertyHolder.setValue(name, value);
prop.setProperty(name, value);
storeProperties(prop);
} }
public String getProperty(String name) throws IOException { public String getProperty(String name) {
return loadPropertiesIfExists().getProperty(name); return propertyHolder.getValue(name);
} }
public void updateInfo() { public void updateInfo() {
...@@ -174,49 +158,7 @@ public class Job { ...@@ -174,49 +158,7 @@ public class Job {
public Path getDirectory() { public Path getDirectory() {
return jobDir; return jobDir;
} }
synchronized private void updateNeedsDownload() throws IOException {
if (needsDownload == null
&& EnumSet.of(JobState.Failed, JobState.Finished, JobState.Canceled).contains(getState())) {
needsDownload = true;
}
saveJobinfo();
}
private synchronized void saveJobinfo() throws IOException {
Properties prop = loadPropertiesIfExists();
if (needsDownload != null) {
prop.setProperty(JOB_HAS_DATA_TO_DOWNLOAD_PROPERTY, needsDownload.toString());
}
prop.setProperty(JOB_NAME, name);
storeProperties(prop);
}
private void storeProperties(Properties prop) throws IOException {
try (OutputStream ow = Files.newOutputStream(jobDir.resolve(JOB_INFO_FILE),
StandardOpenOption.TRUNCATE_EXISTING, StandardOpenOption.CREATE)) {
prop.store(ow, null);
}
}
private synchronized void loadJobInfo() throws IOException {
Properties prop = loadPropertiesIfExists();
if (prop.containsKey(JOB_HAS_DATA_TO_DOWNLOAD_PROPERTY)) {
needsDownload = Boolean.parseBoolean(prop.getProperty(JOB_HAS_DATA_TO_DOWNLOAD_PROPERTY));
}
name = prop.getProperty(JOB_NAME);
}
private Properties loadPropertiesIfExists() throws IOException {
Properties prop = new Properties();
if (Files.exists(jobDir.resolve(JOB_INFO_FILE))) {
try (InputStream is = Files.newInputStream(jobDir.resolve(JOB_INFO_FILE))) {
prop.load(is);
}
}
return prop;
}
private JobInfo getJobInfo() { private JobInfo getJobInfo() {
if (jobInfo == null) { if (jobInfo == null) {
updateJobInfo(); updateJobInfo();
...@@ -226,11 +168,6 @@ public class Job { ...@@ -226,11 +168,6 @@ public class Job {
private void updateJobInfo() { private void updateJobInfo() {
jobInfo = haasClientSupplier.get().obtainJobInfo(getJobId()); jobInfo = haasClientSupplier.get().obtainJobInfo(getJobId());
try {
updateNeedsDownload();
} catch (IOException e) {
log.error(e.getMessage(), e);
}
} }
private static boolean isValidPath(Path path) { private static boolean isValidPath(Path path) {
......
...@@ -63,20 +63,17 @@ public class JobManager { ...@@ -63,20 +63,17 @@ public class JobManager {
return result; return result;
} }
public Iterable<JobInfo> getJobsNeedingDownload() { public Collection<JobInfo> getJobs() {
return () -> jobs.stream().filter(j -> j.needsDownload()).map(j -> new JobInfo(j)).iterator();
}
public Collection<JobInfo> getJobs() throws IOException {
if (jobs == null) { if (jobs == null) {
jobs = new LinkedList<>(); jobs = new LinkedList<>();
Files.list(this.workDirectory).filter(p -> Files.isDirectory(p) && Job.isJobPath(p)).forEach(p -> { try {
try { Files.list(this.workDirectory).filter(p -> Files.isDirectory(p) && Job.isJobPath(p)).forEach(p -> {
jobs.add(new Job(p, this::getHaasClient)); jobs.add(new Job(p, this::getHaasClient));
} catch (IOException e) {
e.printStackTrace(); });
} } catch (IOException e) {
}); throw new RuntimeException(e);
}
} }
return jobs.stream().map(j -> new JobInfo(j)).collect(Collectors.toList()); return jobs.stream().map(j -> new JobInfo(j)).collect(Collectors.toList());
} }
...@@ -146,10 +143,6 @@ public class JobManager { ...@@ -146,10 +143,6 @@ public class JobManager {
return job.getState(); return job.getState();
} }
public boolean needsDownload() {
return job.needsDownload();
}
public String getCreationTime() { public String getCreationTime() {
return getStringFromTimeSafely(job.getCreationTime()); return getStringFromTimeSafely(job.getCreationTime());
} }
...@@ -163,11 +156,11 @@ public class JobManager { ...@@ -163,11 +156,11 @@ public class JobManager {
} }
public void downloadData(Progress notifier) { public void downloadData(Progress notifier) {
downloadData(x -> true, notifier, false); downloadData(x -> true, notifier);
} }
public void downloadData(Predicate<String> predicate, Progress notifier, boolean allowAgain) { public void downloadData(Predicate<String> predicate, Progress notifier) {
job.download(predicate, notifier, allowAgain); job.download(predicate, notifier);
fireValueChangedEvent(); fireValueChangedEvent();
} }
...@@ -202,12 +195,12 @@ public class JobManager { ...@@ -202,12 +195,12 @@ public class JobManager {
return job.openLocalFile(name); return job.openLocalFile(name);
} }
public void setProperty(String name, String value) throws IOException { public void setProperty(String name, String value) {
job.setProperty(name, value); job.setProperty(name, value);
} }
public String getProperty(String name) throws IOException { public String getProperty(String name) {
return job.getProperty(name); return job.getProperty(name);
} }
......
package cz.it4i.fiji.haas;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.StandardOpenOption;
import java.util.Properties;
public class PropertyHolder {
private Path storage;
private Properties properties;
public PropertyHolder(Path storage) {
super();
this.storage = storage;
}
public String getValue(String key) {
Properties properties = getProperties();
return properties.getProperty(key);
}
public void setValue(String key, String value) {
Properties prop = getProperties();
prop.setProperty(key, value);
try {
storeProperties(prop);
} catch (IOException e) {
throw new RuntimeException(e);
}
}
private Properties getProperties() {
if (properties == null) {
try {
properties = loadPropertiesIfExists();
} catch (IOException e) {
throw new RuntimeException(e);
}
}
return properties;
}
private Properties loadPropertiesIfExists() throws IOException {
Properties prop = new Properties();
if (Files.exists(storage)) {
try (InputStream is = Files.newInputStream(storage)) {
prop.load(is);
}
}
return prop;
}
private void storeProperties(Properties prop) throws IOException {
try (OutputStream ow = Files.newOutputStream(storage, StandardOpenOption.TRUNCATE_EXISTING,
StandardOpenOption.CREATE)) {
prop.store(ow, null);
}
}
}
...@@ -2,7 +2,6 @@ package cz.it4i.fiji.haas_snakemake_spim.commands; ...@@ -2,7 +2,6 @@ package cz.it4i.fiji.haas_snakemake_spim.commands;
import java.awt.Frame; import java.awt.Frame;
import java.io.File; import java.io.File;
import java.io.IOException;
import java.nio.file.Path; import java.nio.file.Path;
import java.nio.file.Paths; import java.nio.file.Paths;
import java.util.Collection; import java.util.Collection;
...@@ -51,29 +50,27 @@ public class CheckStatusOfHaaS implements Command { ...@@ -51,29 +50,27 @@ public class CheckStatusOfHaaS implements Command {
@Override @Override
public void run() { public void run() {
try { jobManager = new JobManager(getWorkingDirectoryPath(), TestingConstants.getSettings());
jobManager = new JobManager(getWorkingDirectoryPath(),TestingConstants.getSettings()); if (uiService.isHeadless()) {
if (uiService.isHeadless()) { downloadAll();
downloadAll(); } else {
} else { CheckStatusOfHaaSWindow window;
CheckStatusOfHaaSWindow window; window = ModalDialogs.doModal(new CheckStatusOfHaaSWindow(getFrame(), context),
window = ModalDialogs.doModal(new CheckStatusOfHaaSWindow(getFrame(), context),WindowConstants.DISPOSE_ON_CLOSE); WindowConstants.DISPOSE_ON_CLOSE);
ProgressDialog dialog = ModalDialogs.doModal(new ProgressDialog(window),WindowConstants.DO_NOTHING_ON_CLOSE); ProgressDialog dialog = ModalDialogs.doModal(new ProgressDialog(window),
dialog.setTitle("Downloading info about jobs"); WindowConstants.DO_NOTHING_ON_CLOSE);
Collection<JobInfo> jobs = jobManager.getJobs(); dialog.setTitle("Downloading info about jobs");
int count = 0; Collection<JobInfo> jobs = jobManager.getJobs();
for (JobInfo ji : jobs) { int count = 0;
String item; for (JobInfo ji : jobs) {
dialog.addItem(item = "job id:" + ji.getId()); String item;
window.addJob(ji); dialog.addItem(item = "job id:" + ji.getId());
dialog.itemDone(item); window.addJob(ji);
dialog.setCount(count, jobs.size()); dialog.itemDone(item);
count++; dialog.setCount(count, jobs.size());
} count++;
dialog.done();
} }
} catch (IOException e) { dialog.done();
log.error(e);
} }
} }
...@@ -92,7 +89,7 @@ public class CheckStatusOfHaaS implements Command { ...@@ -92,7 +89,7 @@ public class CheckStatusOfHaaS implements Command {
} }
private void downloadAll() { private void downloadAll() {
for (JobInfo id : jobManager.getJobsNeedingDownload()) { for (JobInfo id : jobManager.getJobs()) {
System.out.println("Job " + id.getId() + " needs download"); System.out.println("Job " + id.getId() + " needs download");
jobManager.downloadJob(id.getId(), new DummyProgress()); jobManager.downloadJob(id.getId(), new DummyProgress());
} }
......
...@@ -69,7 +69,7 @@ public class CheckStatusOfHaaSController implements FXFrame.Controller { ...@@ -69,7 +69,7 @@ public class CheckStatusOfHaaSController implements FXFrame.Controller {
} }
JobInfo job = jobs.getSelectionModel().getSelectedItem(); JobInfo job = jobs.getSelectionModel().getSelectedItem();
if (job != null && job.needsDownload()) { if (job != null) {
download.setDisable(false); download.setDisable(false);
} else { } else {
download.setDisable(true); download.setDisable(true);
...@@ -80,7 +80,7 @@ public class CheckStatusOfHaaSController implements FXFrame.Controller { ...@@ -80,7 +80,7 @@ public class CheckStatusOfHaaSController implements FXFrame.Controller {
private void initTable() { private void initTable() {
setCellValueFactory(0, j -> j.getId().toString()); setCellValueFactory(0, j -> j.getId().toString());
setCellValueFactory(1, j -> j.getState().toString() + (j.needsDownload() ? " - needs download" : "")); setCellValueFactory(1, j -> j.getState().toString());
setCellValueFactory(2, j -> j.getCreationTime().toString()); setCellValueFactory(2, j -> j.getCreationTime().toString());
setCellValueFactory(3, j -> j.getStartTime().toString()); setCellValueFactory(3, j -> j.getStartTime().toString());
setCellValueFactory(4, j -> j.getEndTime().toString()); setCellValueFactory(4, j -> j.getEndTime().toString());
......
...@@ -85,8 +85,7 @@ public class ManageSPIMBenchmark implements Command { ...@@ -85,8 +85,7 @@ public class ManageSPIMBenchmark implements Command {
// Launch ImageJ as usual. // Launch ImageJ as usual.
final ImageJ ij = new ImageJ(); final ImageJ ij = new ImageJ();
ij.launch(args); ij.launch(args);
ij.command().run(ManageSPIMBenchmark.class, true);
//ij.command().run(ManageSPIMBenchmark.class, true);
} }
} }
...@@ -33,6 +33,8 @@ import net.imagej.updater.util.Progress; ...@@ -33,6 +33,8 @@ import net.imagej.updater.util.Progress;
public class BenchmarkJobManager { public class BenchmarkJobManager {
private static final String JOB_HAS_DATA_TO_DOWNLOAD_PROPERTY = "job.needDownload";
private static Logger log = LoggerFactory private static Logger log = LoggerFactory
.getLogger(cz.it4i.fiji.haas_spim_benchmark.core.BenchmarkJobManager.class); .getLogger(cz.it4i.fiji.haas_spim_benchmark.core.BenchmarkJobManager.class);
...@@ -51,6 +53,7 @@ public class BenchmarkJobManager { ...@@ -51,6 +53,7 @@ public class BenchmarkJobManager {
String outputName = getOutputName(jobInfo.openLocalFile(Constants.CONFIG_YAML)); String outputName = getOutputName(jobInfo.openLocalFile(Constants.CONFIG_YAML));
jobInfo.submit(); jobInfo.submit();
jobInfo.setProperty(Constants.SPIM_OUTPUT_FILENAME_PATTERN, outputName); jobInfo.setProperty(Constants.SPIM_OUTPUT_FILENAME_PATTERN, outputName);
setDownloaded(false);
} }
public JobState getState() { public JobState getState() {
...@@ -59,19 +62,18 @@ public class BenchmarkJobManager { ...@@ -59,19 +62,18 @@ public class BenchmarkJobManager {
public void downloadData(Progress progress) throws IOException { public void downloadData(Progress progress) throws IOException {
JobInfo ji = jobInfo; JobInfo ji = jobInfo;
if (ji.needsDownload()) { if (ji.getState() == JobState.Finished) {
if (ji.getState() == JobState.Finished) { String filePattern = ji.getProperty(Constants.SPIM_OUTPUT_FILENAME_PATTERN);
String filePattern = ji.getProperty(Constants.SPIM_OUTPUT_FILENAME_PATTERN); ji.downloadData(downloadFinishedData(filePattern), progress);
ji.downloadData(downloadFinishedData(filePattern), progress, false); } else if (ji.getState() == JobState.Failed) {
} else if (ji.getState() == JobState.Failed) { ji.downloadData(downloadFailedData(), progress);
ji.downloadData(downloadFailedData(), progress, false);
}
} }
setDownloaded(true);
} }
public void downloadStatistics(Progress progress) throws IOException { public void downloadStatistics(Progress progress) throws IOException {
JobInfo ji = jobInfo; JobInfo ji = jobInfo;
ji.downloadData(BenchmarkJobManager.downloadStatistics(), progress, true); ji.downloadData(BenchmarkJobManager.downloadStatistics(), progress);
Path resultFile = ji.getDirectory().resolve(Constants.BENCHMARK_RESULT_FILE); Path resultFile = ji.getDirectory().resolve(Constants.BENCHMARK_RESULT_FILE);
if (resultFile != null) if (resultFile != null)
BenchmarkJobManager.formatResultFile(resultFile); BenchmarkJobManager.formatResultFile(resultFile);
...@@ -125,7 +127,7 @@ public class BenchmarkJobManager { ...@@ -125,7 +127,7 @@ public class BenchmarkJobManager {
} }
public boolean downloaded() { public boolean downloaded() {
return !jobInfo.needsDownload(); return getDownloaded();
} }
public Job update() { public Job update() {
...@@ -136,6 +138,16 @@ public class BenchmarkJobManager { ...@@ -136,6 +138,16 @@ public class BenchmarkJobManager {
public Path getDirectory() { public Path getDirectory() {
return jobInfo.getDirectory(); return jobInfo.getDirectory();
} }
private void setDownloaded(boolean b) {
jobInfo.setProperty(JOB_HAS_DATA_TO_DOWNLOAD_PROPERTY, b + "");
}
private boolean getDownloaded() {
String downloadedStr = jobInfo.getProperty(JOB_HAS_DATA_TO_DOWNLOAD_PROPERTY);
return downloadedStr != null && Boolean.parseBoolean(downloadedStr);
}
} }
private JobManager jobManager; private JobManager jobManager;
...@@ -151,10 +163,10 @@ public class BenchmarkJobManager { ...@@ -151,10 +163,10 @@ public class BenchmarkJobManager {
} }
public Collection<Job> getJobs() throws IOException { public Collection<Job> getJobs() throws IOException {
return jobManager.getJobs().stream().map(this::convertJob).collect(Collectors.toList()); return jobManager.getJobs().stream().map(this::convertJob).collect(Collectors.toList());
} }
private HaaSClient.UploadingFile getUploadingFile() { private HaaSClient.UploadingFile getUploadingFile() {
return new UploadingFileFromResource("", Constants.CONFIG_YAML); return new UploadingFileFromResource("", Constants.CONFIG_YAML);
} }
......
...@@ -4,7 +4,7 @@ import java.util.HashMap; ...@@ -4,7 +4,7 @@ import java.util.HashMap;
import java.util.Map; import java.util.Map;
public interface Constants { public interface Constants {
long HAAS_UPDATE_TIMEOUT = 1000; long HAAS_UPDATE_TIMEOUT = 30000;
String HAAS_JOB_NAME = "HaaSSPIMBenchmark"; String HAAS_JOB_NAME = "HaaSSPIMBenchmark";
int HAAS_CLUSTER_NODE_TYPE = 6; int HAAS_CLUSTER_NODE_TYPE = 6;
int HAAS_TEMPLATE_ID = 4; int HAAS_TEMPLATE_ID = 4;
......
package cz.it4i.fiji.haas_spim_benchmark.core;
import java.util.concurrent.Executor;
import cz.it4i.fiji.haas.ui.FXFrame;
public class FXFrameExecutorService implements Executor{
@Override
public void execute(Runnable command) {
FXFrame.runOnFxThread(() -> {
command.run();
});
}
}
...@@ -11,8 +11,11 @@ import java.util.Map; ...@@ -11,8 +11,11 @@ import java.util.Map;
import java.util.Set; import java.util.Set;
import java.util.Timer; import java.util.Timer;
import java.util.TimerTask; import java.util.TimerTask;
import java.util.concurrent.Callable;
import java.util.concurrent.Executor;
import java.util.concurrent.ExecutorService; import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors; import java.util.concurrent.Executors;
import java.util.function.Consumer;
import java.util.function.Function; import java.util.function.Function;
import java.util.function.Predicate; import java.util.function.Predicate;
import java.util.stream.Collectors; import java.util.stream.Collectors;
...@@ -32,6 +35,7 @@ import cz.it4i.fiji.haas_java_client.JobState; ...@@ -32,6 +35,7 @@ import cz.it4i.fiji.haas_java_client.JobState;
import cz.it4i.fiji.haas_spim_benchmark.core.BenchmarkJobManager; import cz.it4i.fiji.haas_spim_benchmark.core.BenchmarkJobManager;
import cz.it4i.fiji.haas_spim_benchmark.core.BenchmarkJobManager.Job; import cz.it4i.fiji.haas_spim_benchmark.core.BenchmarkJobManager.Job;
import cz.it4i.fiji.haas_spim_benchmark.core.Constants; import cz.it4i.fiji.haas_spim_benchmark.core.Constants;
import cz.it4i.fiji.haas_spim_benchmark.core.FXFrameExecutorService;
import javafx.fxml.FXML; import javafx.fxml.FXML;
import javafx.scene.control.TableColumn; import javafx.scene.control.TableColumn;
import javafx.scene.control.TableView; import javafx.scene.control.TableView;
...@@ -54,7 +58,9 @@ public class BenchmarkSPIMController implements FXFrame.Controller { ...@@ -54,7 +58,9 @@ public class BenchmarkSPIMController implements FXFrame.Controller {
private Window root; private Window root;
private ExecutorService executorService; private ExecutorService executorServiceUI;
private ExecutorService executorServiceWS;
private Executor executorServiceFX = new FXFrameExecutorService();
private Timer timer; private Timer timer;
...@@ -63,7 +69,8 @@ public class BenchmarkSPIMController implements FXFrame.Controller { ...@@ -63,7 +69,8 @@ public class BenchmarkSPIMController implements FXFrame.Controller {
@Override @Override
public void init(Window frame) { public void init(Window frame) {
executorService = Executors.newSingleThreadExecutor(); executorServiceWS = Executors.newSingleThreadExecutor();
executorServiceUI = Executors.newSingleThreadExecutor();
timer = new Timer(); timer = new Timer();
timer.scheduleAtFixedRate(new TimerTask() { timer.scheduleAtFixedRate(new TimerTask() {
@Override @Override
...@@ -91,89 +98,108 @@ public class BenchmarkSPIMController implements FXFrame.Controller { ...@@ -91,89 +98,108 @@ public class BenchmarkSPIMController implements FXFrame.Controller {
private void initMenu() { private void initMenu() {
TableViewContextMenu<Job> menu = new TableViewContextMenu<>(jobs); TableViewContextMenu<Job> menu = new TableViewContextMenu<>(jobs);
menu.addItem("Create job", x -> executeJobActionAsync("Creating job", false, p -> manager.createJob()), j -> true); menu.addItem("Create job", x -> executeWSCallAsync("Creating job", p -> manager.createJob()),
menu.addItem("Start job", job -> executeJobActionAsync("Starting job", p -> job.startJob(p)), j -> true);
job -> notNullValue(job, j -> j.getState() == JobState.Configuring || j.getState() == JobState.Finished)); menu.addItem("Start job", job -> executeWSCallAsync("Starting job", p -> job.startJob(p)),
menu.addItem("Download result", job -> executeJobActionAsync("Downloading data", p -> job.downloadData(p)), job -> notNullValue(job,
j -> j.getState() == JobState.Configuring || j.getState() == JobState.Finished));
menu.addItem("Download result", job -> executeWSCallAsync("Downloading data", p -> job.downloadData(p)),
job -> notNullValue(job, job -> notNullValue(job,
j -> EnumSet.of(JobState.Failed, JobState.Finished).contains(j.getState()) && !j.downloaded())); j -> EnumSet.of(JobState.Failed, JobState.Finished).contains(j.getState()) && !j.downloaded()));
menu.addItem("Download statistics", menu.addItem("Download statistics",
job -> executeJobActionAsync("Downloading data", p -> job.downloadStatistics(p)), job -> executeWSCallAsync("Downloading data", p -> job.downloadStatistics(p)),
job -> notNullValue(job, j -> j.getState() == JobState.Finished)); job -> notNullValue(job, j -> j.getState() == JobState.Finished));
menu.addItem("Show output", j -> new JobOutputView(root, executorService, j, Constants.HAAS_UPDATE_TIMEOUT), menu.addItem("Show output", j -> new JobOutputView(root, executorServiceUI, j, Constants.HAAS_UPDATE_TIMEOUT),
job -> notNullValue(job, job -> notNullValue(job,
j -> EnumSet.of(JobState.Failed, JobState.Finished, JobState.Running).contains(j.getState()))); j -> EnumSet.of(JobState.Failed, JobState.Finished, JobState.Running).contains(j.getState())));
menu.addItem("Open", j->open(j), x->true); menu.addItem("Open", j -> open(j), x -> true);
menu.addItem("Update table", job -> updateJobs(), j -> true); menu.addItem("Update table", job -> updateJobs(), j -> true);
} }
private void open(Job j) { private void open(Job j) {
executorService.execute(() -> { executorServiceUI.execute(() -> {
Desktop desktop = Desktop.getDesktop(); Desktop desktop = Desktop.getDesktop();
try { try {
desktop.open(j.getDirectory().toFile()); desktop.open(j.getDirectory().toFile());
} catch (IOException e) { } catch (IOException e) {
log.error(e.getMessage(), e); log.error(e.getMessage(), e);
}}); }
} });
private void executeJobActionAsync(String title, P_JobAction action) {
executeJobActionAsync(title, true, action);
} }
private void executeJobActionAsync(String title, boolean update, P_JobAction action) { private <V> void executeAsync(Executor executor, Callable<V> action, Consumer<V> postAction) {
executorService.execute(() -> { executor.execute(() -> {
V result;
try { try {
ProgressDialog dialog = ModalDialogs.doModal(new ProgressDialog(root, title), result = action.call();
WindowConstants.DO_NOTHING_ON_CLOSE); postAction.accept(result);
action.doAction(dialog); } catch (Exception e) {
dialog.done();
if(update) {
updateJobs();
}
} catch (IOException e) {
log.error(e.getMessage(), e); log.error(e.getMessage(), e);
} }
}); });
} }
private void executeWSCallAsync(String title, P_JobAction action) {
executeWSCallAsync(title, true, action);
}
private void executeWSCallAsync(String title, boolean update, P_JobAction action) {
executeAsync(executorServiceWS, (Callable<Void>) ()->{
ProgressDialog dialog = ModalDialogs.doModal(new ProgressDialog(root, title),
WindowConstants.DO_NOTHING_ON_CLOSE);
action.doAction(dialog);
dialog.done();
return null;
}, x-> {if(update) updateJobs(); });
}
private void updateJobs() { private void updateJobs() {
updateJobs(true); updateJobs(true);
} }
private void updateJobs(boolean showProgress) { private void updateJobs(boolean showProgress) {
executorService.execute(() -> { executorServiceUI.execute(() -> {
Progress progress = showProgress Progress progress = showProgress
? ModalDialogs.doModal(new ProgressDialog(root, "Updating jobs"), ? ModalDialogs.doModal(new ProgressDialog(root, "Updating jobs"),
WindowConstants.DO_NOTHING_ON_CLOSE) WindowConstants.DO_NOTHING_ON_CLOSE)
: new DummyProgress(); : new DummyProgress();
Set<Job> old = new HashSet<Job>(jobs.getItems());
Map<Job, Job> actual;
try { try {
actual = manager.getJobs().stream().map(job -> job.update()) manager.getJobs().forEach(job -> job.update());
.collect(Collectors.toMap(job -> job, job -> job)); } catch (IOException e1) {
} catch (IOException e) { throw new RuntimeException(e1);
throw new RuntimeException(e);
} }
for (Job job : old) { executorServiceUI.execute(() -> {
if (!actual.containsKey(job)) {
jobs.getItems().remove(job);
} else { Set<Job> old = new HashSet<Job>(jobs.getItems());
job.update(actual.get(job)); Map<Job, Job> actual;
try {
actual = manager.getJobs().stream().
collect(Collectors.toMap(job -> job, job -> job));
} catch (IOException e) {
throw new RuntimeException(e);
} }
} for (Job job : old) {
progress.done(); if (!actual.containsKey(job)) {
FXFrame.runOnFxThread(() -> { jobs.getItems().remove(job);
for (Job job : actual.keySet()) { } else {
if (!old.contains(job)) { job.update(actual.get(job));
jobs.getItems().add(job);
} }
} }
progress.done();
executorServiceFX.execute(() -> {
for (Job job : actual.keySet()) {
if (!old.contains(job)) {
jobs.getItems().add(job);
}
}
});
}); });
}); });
} }
private void initTable() { private void initTable() {
...@@ -196,7 +222,8 @@ public class BenchmarkSPIMController implements FXFrame.Controller { ...@@ -196,7 +222,8 @@ public class BenchmarkSPIMController implements FXFrame.Controller {
} }
private void dispose() { private void dispose() {
executorService.shutdown(); executorServiceUI.shutdown();
executorServiceWS.shutdown();
timer.cancel(); timer.cancel();
} }
} }
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment