Assign JABAService.V2_SERVICE_NAMESPACE to all new services: JpredWS, RNAalifoldWS...
[jabaws.git] / engine / compbio / engine / cluster / drmaa / AsyncClusterRunner.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.cluster.drmaa;\r
20 \r
21 import org.apache.log4j.Logger;\r
22 import org.ggf.drmaa.DrmaaException;\r
23 \r
24 import compbio.engine.AsyncExecutor;\r
25 import compbio.engine.Cleaner;\r
26 import compbio.engine.Configurator;\r
27 import compbio.engine.client.ConfiguredExecutable;\r
28 import compbio.engine.client.EngineUtil;\r
29 import compbio.metadata.JobSubmissionException;\r
30 import compbio.metadata.ResultNotAvailableException;\r
31 \r
32 /**\r
33  * Single cluster job runner class\r
34  * \r
35  * @author pvtroshin\r
36  * @version 1.0 August 2009\r
37  * \r
38  *          TODO after call to submitJob() no setters really work as the job\r
39  *          template gets deleted, this needs to be taken into account in this\r
40  *          class design!\r
41  */\r
42 public class AsyncClusterRunner implements AsyncExecutor {\r
43 \r
44         private static Logger log = Logger.getLogger(AsyncClusterRunner.class);\r
45 \r
46         @Override\r
47         public String submitJob(ConfiguredExecutable<?> executable) throws JobSubmissionException {\r
48                 ClusterRunner jr = new ClusterRunner(executable);\r
49                 jr.submitJob();\r
50                 // ignore cluster job id as it could be retrieved from fs\r
51                 return executable.getTaskId();\r
52         }\r
53 \r
54         @Override\r
55         public boolean cancelJob(String jobId) {\r
56                 ClusterSession clustSession = ClusterSession.getInstance();\r
57                 return compbio.engine.cluster.drmaa.ClusterEngineUtil.cancelJob(jobId, clustSession);\r
58         }\r
59 \r
60         /*\r
61          * This will never return clust.engine.JobStatus.CANCELLED as for sun grid\r
62          * engine cancelled job is the same as failed. Cancelled jobs needs to be\r
63          * tracked manually!\r
64          */\r
65         @Override\r
66         public compbio.metadata.JobStatus getJobStatus(String jobId) {\r
67                 return ClusterRunner.getJobStatus(jobId);\r
68         }\r
69 \r
70         @Override\r
71         public boolean cleanup(String jobId) {\r
72                 String workDir = Configurator.getWorkDirectory(jobId);\r
73                 return Cleaner.deleteAllFiles(workDir);\r
74         }\r
75 \r
76         @Override\r
77         public ConfiguredExecutable<?> getResults(String jobId) throws ResultNotAvailableException {\r
78                 assert EngineUtil.isValidJobId(jobId);\r
79                 ClusterSession csession = ClusterSession.getInstance();\r
80                 ConfiguredExecutable<?> exec;\r
81                 try {\r
82                         exec = csession.getResults(jobId);\r
83                 } catch (DrmaaException e) {\r
84                         log.error(e.getLocalizedMessage(), e.getCause());\r
85                         throw new ResultNotAvailableException(e);\r
86                 }\r
87                 return exec;\r
88         }\r
89 \r
90         @Override\r
91         public String getWorkDirectory(String jobId) {\r
92                 return Configurator.getWorkDirectory(jobId);\r
93         }\r
94 \r
95 } // class end\r