1 /* Copyright (c) 2009 Peter Troshin
\r
3 * JAva Bioinformatics Analysis Web Services (JABAWS) @version: 1.0
\r
5 * This library is free software; you can redistribute it and/or modify it under the terms of the
\r
6 * Apache License version 2 as published by the Apache Software Foundation
\r
8 * This library is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without
\r
9 * even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the Apache
\r
10 * License for more details.
\r
12 * A copy of the license is in apache_license.txt. It is also available here:
\r
13 * @see: http://www.apache.org/licenses/LICENSE-2.0.txt
\r
15 * Any republication or derived work distributed in source code form
\r
16 * must include this copyright and license notice.
\r
19 package compbio.engine.local;
\r
21 import java.util.concurrent.ExecutionException;
\r
22 import java.util.concurrent.Future;
\r
24 import org.apache.log4j.Logger;
\r
26 import compbio.engine.AsyncExecutor;
\r
27 import compbio.engine.Configurator;
\r
28 import compbio.engine.SubmissionManager;
\r
29 import compbio.engine.client.ConfiguredExecutable;
\r
30 import compbio.engine.client.Util;
\r
31 import compbio.metadata.JobStatus;
\r
32 import compbio.metadata.JobSubmissionException;
\r
33 import compbio.metadata.ResultNotAvailableException;
\r
35 public final class AsyncLocalRunner implements AsyncExecutor {
\r
37 private static final Logger log = Logger.getLogger(AsyncLocalRunner.class);
\r
40 public String getWorkDirectory(String jobId) {
\r
41 return Configurator.getWorkDirectory(jobId);
\r
45 public boolean cancelJob(String jobId) {
\r
46 Future<ConfiguredExecutable<?>> future = SubmissionManager
\r
48 // The job has already finished or cancelled.
\r
49 if (future == null) {
\r
50 log.debug("Did not find future for local job "
\r
52 + " will not cancel it. Perhaps it has finished or cancelled already.");
\r
55 LocalEngineUtil.cancelJob(future, getWorkDirectory(jobId));
\r
56 return future.cancel(true);
\r
60 public JobStatus getJobStatus(String jobId) {
\r
61 Future<ConfiguredExecutable<?>> future = SubmissionManager
\r
63 if (future == null) {
\r
64 return LocalEngineUtil.getRecordedJobStatus(jobId);
\r
66 return LocalEngineUtil.getJobStatus(future);
\r
70 public String submitJob(ConfiguredExecutable<?> executable)
\r
71 throws JobSubmissionException {
\r
72 if (executable == null) {
\r
73 throw new NullPointerException("Executable expected!");
\r
75 LocalRunner lrunner = new LocalRunner(executable);
\r
76 lrunner.executeJob();
\r
77 Future<ConfiguredExecutable<?>> future = lrunner.getFuture();
\r
79 if (future == null) {
\r
80 throw new RuntimeException("Future is NULL for executable "
\r
83 SubmissionManager.addTask(executable, future);
\r
84 return executable.getTaskId();
\r
90 * @return true if all files were removed, false otherwise
\r
93 public boolean cleanup(String jobId) {
\r
94 Future<ConfiguredExecutable<?>> future = SubmissionManager
\r
96 ConfiguredExecutable<?> cexec = null;
\r
98 cexec = future.get();
\r
99 } catch (InterruptedException e) {
\r
100 log.error("Cannot clean up as calculation was not completed!"
\r
101 + e.getLocalizedMessage());
\r
102 } catch (ExecutionException e) {
\r
103 log.error("Cannot clean up due to ExecutionException "
\r
104 + e.getLocalizedMessage());
\r
106 if (cexec == null) {
\r
109 return LocalEngineUtil.cleanup(cexec);
\r
113 public ConfiguredExecutable<?> getResults(String taskId)
\r
114 throws ResultNotAvailableException {
\r
115 if (!Util.isValidJobId(taskId)) {
\r
116 // TODO should I be throwing something else?
\r
117 throw new IllegalArgumentException(taskId);
\r
119 Future<ConfiguredExecutable<?>> futureExec = SubmissionManager
\r
121 if (futureExec == null) {
\r
122 // If task was not find in the list of jobs, than it must have been
\r
123 // collected already
\r
124 // Resurrect the job to find out there the output is
\r
125 ConfiguredExecutable<?> exec = compbio.engine.client.Util
\r
126 .loadExecutable(taskId);
\r
129 return LocalEngineUtil.getResults(futureExec, taskId);
\r