46debcc9974d07e91542d44428edfd13a73b1360
[jabaws.git] / engine / compbio / engine / cluster / drmaa / AsyncJobRunner.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.Util;\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  * @date 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 AsyncJobRunner implements AsyncExecutor {\r
43 \r
44         private static Logger log = Logger.getLogger(AsyncJobRunner.class);\r
45 \r
46         @Override\r
47         public String submitJob(ConfiguredExecutable<?> executable)\r
48                         throws JobSubmissionException {\r
49                 JobRunner jr = new JobRunner(executable);\r
50                 jr.submitJob(); // ignore cluster job id as it could be retrieved from\r
51                 // fs\r
52                 return executable.getTaskId();\r
53         }\r
54 \r
55         @Override\r
56         public boolean cancelJob(String jobId) {\r
57                 ClusterSession clustSession = ClusterSession.getInstance();\r
58                 return compbio.engine.cluster.drmaa.ClusterUtil.cancelJob(jobId,\r
59                                 clustSession);\r
60         }\r
61 \r
62         /**\r
63          * This will never return clust.engine.JobStatus.CANCELLED as for sun grid\r
64          * engine cancelled job is the same as failed. Cancelled jobs needs to be\r
65          * tracked manually!\r
66          */\r
67         @Override\r
68         public compbio.metadata.JobStatus getJobStatus(String jobId) {\r
69                 return JobRunner.getJobStatus(jobId);\r
70         }\r
71 \r
72         @Override\r
73         public boolean cleanup(String jobId) {\r
74                 String workDir = Configurator.getWorkDirectory(jobId);\r
75                 return Cleaner.deleteAllFiles(workDir);\r
76         }\r
77 \r
78         @Override\r
79         public ConfiguredExecutable<?> getResults(String jobId)\r
80                         throws ResultNotAvailableException {\r
81 \r
82                 assert Util.isValidJobId(jobId);\r
83 \r
84                 ClusterSession csession = ClusterSession.getInstance();\r
85                 ConfiguredExecutable<?> exec;\r
86                 try {\r
87                         exec = csession.getResults(jobId);\r
88                 } catch (DrmaaException e) {\r
89                         log.error(e.getLocalizedMessage(), e.getCause());\r
90                         throw new ResultNotAvailableException(e);\r
91                 }\r
92                 return exec;\r
93         }\r
94 \r
95         @Override\r
96         public String getWorkDirectory(String jobId) {\r
97                 return Configurator.getWorkDirectory(jobId);\r
98         }\r
99 \r
100 } // class end\r