Next version of JABA
[jabaws.git] / engine / compbio / engine / local / LocalEngineUtil.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.Cleaner;\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.ResultNotAvailableException;\r
33 \r
34 public final class LocalEngineUtil {\r
35 \r
36         private static final Logger log = Logger.getLogger(LocalEngineUtil.class);\r
37 \r
38         /**\r
39          * Coerce an unchecked Throwable to a RuntimeException or Error\r
40          * \r
41          * @param throwable\r
42          * @return\r
43          */\r
44         static RuntimeException launderThrowable(Throwable throwable) {\r
45                 if (throwable instanceof RuntimeException) {\r
46                         return (RuntimeException) throwable;\r
47                 } else if (throwable instanceof Error) {\r
48                         throw (Error) throwable;\r
49                 } else {\r
50                         // Logic error then\r
51                         throw new IllegalStateException(\r
52                                         "Checked exception being thrown and unwrapped by LocalRunner.launderThrowable method",\r
53                                         throwable);\r
54                 }\r
55         }\r
56 \r
57         public static boolean cancelJob(Future<ConfiguredExecutable<?>> future,\r
58                         String workDirectory) {\r
59                 compbio.engine.client.Util.writeMarker(workDirectory,\r
60                                 JobStatus.CANCELLED);\r
61                 log.debug("Cancelling local job from work directory " + workDirectory);\r
62                 return future.cancel(true);\r
63         }\r
64 \r
65         public static JobStatus getJobStatus(Future<ConfiguredExecutable<?>> future) {\r
66                 // Order is important here as cancelled tasks also considered done!\r
67                 if (future == null) {\r
68                         throw new NullPointerException("Future must be provided!");\r
69                 }\r
70                 if (future.isCancelled()) {\r
71                         return JobStatus.CANCELLED;\r
72                 }\r
73                 if (future.isDone()) {\r
74                         return JobStatus.FINISHED;\r
75                 }\r
76                 return JobStatus.RUNNING;\r
77         }\r
78 \r
79         public static JobStatus getRecordedJobStatus(String jobId) {\r
80                 // job has been removed from the task list\r
81                 // but there may be status written to the disk\r
82                 String workDir = Configurator.getWorkDirectory(jobId);\r
83                 if (Util.isMarked(workDir, JobStatus.FINISHED)\r
84                                 || Util.isMarked(workDir, JobStatus.COLLECTED)) {\r
85                         return JobStatus.FINISHED;\r
86                 }\r
87                 if (Util.isMarked(workDir, JobStatus.CANCELLED)) {\r
88                         return JobStatus.CANCELLED;\r
89                 }\r
90                 if (Util.isMarked(workDir, JobStatus.FAILED)) {\r
91                         return JobStatus.FAILED;\r
92                 }\r
93                 return JobStatus.UNDEFINED;\r
94         }\r
95 \r
96         public static boolean cleanup(ConfiguredExecutable<?> confExecutable) {\r
97                 if (confExecutable == null) {\r
98                         throw new NullPointerException("Future must be provided!");\r
99                 }\r
100                 return Cleaner.deleteFiles(confExecutable);\r
101         }\r
102 \r
103         public static ConfiguredExecutable<?> getResults(\r
104                         Future<ConfiguredExecutable<?>> future, final String taskId)\r
105                         throws ResultNotAvailableException {\r
106                 ConfiguredExecutable<?> exec = null;\r
107                 try {\r
108                         exec = future.get();\r
109                         if (exec == null) {\r
110                                 throw new ResultNotAvailableException(\r
111                                                 "Job return null as a Result! Job work directory is "\r
112                                                                 + Configurator.getWorkDirectory(taskId)\r
113                                                                 + " Job id is " + taskId);\r
114                         }\r
115                         compbio.engine.client.Util.writeMarker(Configurator\r
116                                         .getWorkDirectory(taskId), JobStatus.COLLECTED);\r
117                 } catch (InterruptedException e) {\r
118                         // reassert threads interrupted status\r
119                         Thread.currentThread().interrupt();\r
120                         compbio.engine.client.Util.writeMarker(Configurator\r
121                                         .getWorkDirectory(taskId), JobStatus.FAILED);\r
122                         // Cancel the job\r
123                         log.debug("Cancelling job due to Interruption");\r
124                         future.cancel(true);\r
125                         // do not clean up leave files untouched\r
126                         // this.cleanup(taskId);\r
127                 } catch (ExecutionException e) {\r
128                         // this.cleanup(taskId);\r
129                         compbio.engine.client.Util.writeMarker(Configurator\r
130                                         .getWorkDirectory(taskId), JobStatus.FAILED);\r
131                         log.debug("Job execution exception: " + e.getLocalizedMessage(), e\r
132                                         .getCause());\r
133                         // ExecutionException returned as thus Throwable needs unwrapping\r
134                         LocalEngineUtil.launderThrowable(e.getCause());\r
135                 } finally {\r
136                         future.cancel(true);// harmless if the task already completed\r
137                         // whatever happens remove task from list\r
138                         SubmissionManager.removeTask(taskId);\r
139                 }\r
140                 return exec;\r
141         }\r
142 \r
143 }\r