JWS-114 Bumping varied config files and jetbrains xmls. Also adding some java classes...
[jabaws.git] / engine / compbio / engine / cluster / drmaa / ClusterUtil.java
1 /* Copyright (c) 2009 Peter Troshin
2  *  
3  *  JAva Bioinformatics Analysis Web Services (JABAWS) @version: 1.0     
4  * 
5  *  This library is free software; you can redistribute it and/or modify it under the terms of the
6  *  Apache License version 2 as published by the Apache Software Foundation
7  * 
8  *  This library is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without
9  *  even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the Apache 
10  *  License for more details.
11  * 
12  *  A copy of the license is in apache_license.txt. It is also available here:
13  * @see: http://www.apache.org/licenses/LICENSE-2.0.txt
14  * 
15  * Any republication or derived work distributed in source code form
16  * must include this copyright and license notice.
17  */
18 package compbio.engine.cluster.drmaa;
19
20 import java.io.IOException;
21 import java.text.NumberFormat;
22
23 import org.apache.log4j.Logger;
24 import org.ggf.drmaa.DrmaaException;
25 import org.ggf.drmaa.JobInfo;
26 import org.ggf.drmaa.Session;
27
28 import compbio.engine.Configurator;
29 import compbio.engine.client.Util;
30 import compbio.metadata.JobExecutionException;
31 import compbio.metadata.JobStatus;
32
33 public class ClusterUtil {
34
35         private static final Logger log = Logger.getLogger(ClusterUtil.class);
36
37         public static final NumberFormat CLUSTER_STAT_IN_SEC = NumberFormat
38                         .getInstance();
39
40         static {
41                 CLUSTER_STAT_IN_SEC.setMinimumFractionDigits(4);
42         }
43
44         public static final boolean cancelJob(final String jobId,
45                         ClusterSession csession) {
46                 assert Util.isValidJobId(jobId);
47                 boolean cancelled = true;
48                 Session session = csession.getSession();
49                 try {
50                         log.info("Job " + jobId + " is successfully cancelled");
51                         compbio.engine.client.Util.writeMarker(Configurator.getWorkDirectory(jobId),
52                                         JobStatus.CANCELLED);
53                         session.control(ClusterSession.getClusterJobId(jobId).getJobId(),
54                                         Session.TERMINATE);
55                 } catch (DrmaaException e) {
56                         // Log silently
57                         log.error("Job " + jobId + " cancellation failed!");
58                         log.error("Cause: " + e.getLocalizedMessage(), e.getCause());
59                         cancelled = false;
60                 } catch (IOException e) {
61                         log.error(
62                                         "Could not read JOBID file to determine cluster jobid for taskid: "
63                                                         + jobId + " Message: " + e.getLocalizedMessage(), e
64                                                         .getCause());
65                 } finally {
66                         log
67                                         .trace("Job "
68                                                         + jobId
69                                                         + " has been successfully removed from the cluster engine job list");
70                         csession.removeJob(jobId);
71                 }
72                 return cancelled;
73         }
74
75         public static final JobInfo waitForResult(ClusterSession csession,
76                         String jobId) throws JobExecutionException {
77                 JobInfo jinfo = null;
78                 assert Util.isValidJobId(jobId);
79                 try {
80                         jinfo = csession.waitForJob(jobId);
81                 } catch (DrmaaException e) {
82                         log.error(e.getLocalizedMessage(), e.getCause());
83                         throw new JobExecutionException(e);
84                 } catch (IOException e) {
85                         log.error("Could not read JOBID file for job " + jobId
86                                         + " Message " + e.getMessage(), e.getCause());
87                         throw new JobExecutionException(e);
88                 } finally {
89                         // at this point the job has finished
90                         csession.removeJob(jobId);
91                 }
92                 return jinfo;
93         }
94
95 }