Next version of JABA
[jabaws.git] / engine / compbio / engine / cluster / drmaa / ClusterUtil.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 package compbio.engine.cluster.drmaa;\r
19 \r
20 import java.io.IOException;\r
21 import java.text.NumberFormat;\r
22 \r
23 import org.apache.log4j.Logger;\r
24 import org.ggf.drmaa.DrmaaException;\r
25 import org.ggf.drmaa.JobInfo;\r
26 import org.ggf.drmaa.Session;\r
27 \r
28 import compbio.engine.Configurator;\r
29 import compbio.engine.client.Util;\r
30 import compbio.metadata.JobExecutionException;\r
31 import compbio.metadata.JobStatus;\r
32 \r
33 public class ClusterUtil {\r
34 \r
35         private static final Logger log = Logger.getLogger(ClusterUtil.class);\r
36 \r
37         public static final NumberFormat CLUSTER_STAT_IN_SEC = NumberFormat\r
38                         .getInstance();\r
39 \r
40         static {\r
41                 CLUSTER_STAT_IN_SEC.setMinimumFractionDigits(4);\r
42         }\r
43 \r
44         public static final boolean cancelJob(final String jobId,\r
45                         ClusterSession csession) {\r
46                 assert Util.isValidJobId(jobId);\r
47                 boolean cancelled = true;\r
48                 Session session = csession.getSession();\r
49                 try {\r
50                         log.info("Job " + jobId + " is successfully cancelled");\r
51                         compbio.engine.client.Util.writeMarker(Configurator.getWorkDirectory(jobId),\r
52                                         JobStatus.CANCELLED);\r
53                         session.control(ClusterSession.getClusterJobId(jobId).getJobId(),\r
54                                         Session.TERMINATE);\r
55                 } catch (DrmaaException e) {\r
56                         // Log silently\r
57                         log.error("Job " + jobId + " cancellation failed!");\r
58                         log.error("Cause: " + e.getLocalizedMessage(), e.getCause());\r
59                         cancelled = false;\r
60                 } catch (IOException e) {\r
61                         log.error(\r
62                                         "Could not read JOBID file to determine cluster jobid for taskid: "\r
63                                                         + jobId + " Message: " + e.getLocalizedMessage(), e\r
64                                                         .getCause());\r
65                 } finally {\r
66                         log\r
67                                         .trace("Job "\r
68                                                         + jobId\r
69                                                         + " has been successfully removed from the cluster engine job list");\r
70                         csession.removeJob(jobId);\r
71                 }\r
72                 return cancelled;\r
73         }\r
74 \r
75         public static final JobInfo waitForResult(ClusterSession csession,\r
76                         String jobId) throws JobExecutionException {\r
77                 JobInfo jinfo = null;\r
78                 assert Util.isValidJobId(jobId);\r
79                 try {\r
80                         jinfo = csession.waitForJob(jobId);\r
81                 } catch (DrmaaException e) {\r
82                         log.error(e.getLocalizedMessage(), e.getCause());\r
83                         throw new JobExecutionException(e);\r
84                 } catch (IOException e) {\r
85                         log.error("Could not read JOBID file for job " + jobId\r
86                                         + " Message " + e.getMessage(), e.getCause());\r
87                         throw new JobExecutionException(e);\r
88                 } finally {\r
89                         // at this point the job has finished\r
90                         csession.removeJob(jobId);\r
91                 }\r
92                 return jinfo;\r
93         }\r
94 \r
95 }\r