cfc29167136ad6cc9ca5dd657bbc57326cb127ea
[jabaws.git] / engine / compbio / engine / local / AsyncLocalRunner.java
1 /* Copyright (c) 2009 Peter Troshin\r
2  *  \r
3  *  JAva Bioinformatics Analysis Web Services (JABAWS) @version: 1.0     \r
4  * \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
7  * \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
11  * \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
14  * \r
15  * Any republication or derived work distributed in source code form\r
16  * must include this copyright and license notice.\r
17  */\r
18 \r
19 package compbio.engine.local;\r
20 \r
21 import java.util.concurrent.ExecutionException;\r
22 import java.util.concurrent.Future;\r
23 \r
24 import org.apache.log4j.Logger;\r
25 \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
34 \r
35 public final class AsyncLocalRunner implements AsyncExecutor {\r
36 \r
37         private static final Logger log = Logger.getLogger(AsyncLocalRunner.class);\r
38 \r
39         @Override\r
40         public String getWorkDirectory(String jobId) {\r
41                 return Configurator.getWorkDirectory(jobId);\r
42         }\r
43 \r
44         @Override\r
45         public boolean cancelJob(String jobId) {\r
46                 Future<ConfiguredExecutable<?>> future = SubmissionManager\r
47                                 .getTask(jobId);\r
48                 // The job has already finished or cancelled.\r
49                 if (future == null) {\r
50                         log\r
51                                         .debug("Did not find future for local job "\r
52                                                         + jobId\r
53                                                         + " will not cancel it. Perhaps it has finished or cancelled already.");\r
54                         return false;\r
55                 }\r
56                 LocalEngineUtil.cancelJob(future, getWorkDirectory(jobId));\r
57                 return future.cancel(true);\r
58         }\r
59 \r
60         @Override\r
61         public JobStatus getJobStatus(String jobId) {\r
62                 Future<ConfiguredExecutable<?>> future = SubmissionManager\r
63                                 .getTask(jobId);\r
64                 if (future == null) {\r
65                         return LocalEngineUtil.getRecordedJobStatus(jobId);\r
66                 }\r
67                 return LocalEngineUtil.getJobStatus(future);\r
68         }\r
69 \r
70         @Override\r
71         public String submitJob(ConfiguredExecutable<?> executable)\r
72                         throws JobSubmissionException {\r
73                 if (executable == null) {\r
74                         throw new NullPointerException("Executable expected!");\r
75                 }\r
76                 LocalRunner lrunner = new LocalRunner(executable);\r
77                 lrunner.executeJob();\r
78                 Future<ConfiguredExecutable<?>> future = lrunner.getFuture();\r
79 \r
80                 if (future == null) {\r
81                         throw new RuntimeException("Future is NULL for executable "\r
82                                         + executable);\r
83                 }\r
84                 SubmissionManager.addTask(executable, future);\r
85                 return executable.getTaskId();\r
86         }\r
87 \r
88         /**\r
89          * \r
90          * @param jobId\r
91          * @return\r
92          */\r
93         @Override\r
94         public boolean cleanup(String jobId) {\r
95                 Future<ConfiguredExecutable<?>> future = SubmissionManager\r
96                                 .getTask(jobId);\r
97                 ConfiguredExecutable<?> cexec = null;\r
98                 try {\r
99                         cexec = future.get();\r
100                 } catch (InterruptedException e) {\r
101                         log.error("Cannot clean up as calculation was not completed!"\r
102                                         + e.getLocalizedMessage());\r
103                 } catch (ExecutionException e) {\r
104                         log.error("Cannot clean up due to ExecutionException "\r
105                                         + e.getLocalizedMessage());\r
106                 }\r
107                 if (cexec == null) {\r
108                         return false;\r
109                 }\r
110                 return LocalEngineUtil.cleanup(cexec);\r
111         }\r
112 \r
113         @Override\r
114         public ConfiguredExecutable<?> getResults(String taskId)\r
115                         throws ResultNotAvailableException {\r
116                 if (!Util.isValidJobId(taskId)) {\r
117                         // TODO should I be throwing something else?\r
118                         throw new IllegalArgumentException(taskId);\r
119                 }\r
120                 Future<ConfiguredExecutable<?>> futureExec = SubmissionManager\r
121                                 .getTask(taskId);\r
122                 if (futureExec == null) {\r
123                         // If task was not find in the list of jobs, than it must have been\r
124                         // collected already\r
125                         // Resurrect the job to find out there the output is\r
126                         ConfiguredExecutable<?> exec = compbio.engine.client.Util\r
127                                         .loadExecutable(taskId);\r
128                         return exec;\r
129                 }\r
130                 return LocalEngineUtil.getResults(futureExec, taskId);\r
131         }\r
132 \r
133 }\r