Change header template for a new version
[jabaws.git] / webservices / compbio / stat / collector / ExecutionStatCollector.java
index bf3130a..cd97f4a 100644 (file)
@@ -1,3 +1,20 @@
+/* Copyright (c) 2011 Peter Troshin\r
+ *  \r
+ *  JAva Bioinformatics Analysis Web Services (JABAWS) @version: 2.0     \r
+ * \r
+ *  This library is free software; you can redistribute it and/or modify it under the terms of the\r
+ *  Apache License version 2 as published by the Apache Software Foundation\r
+ * \r
+ *  This library is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without\r
+ *  even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the Apache \r
+ *  License for more details.\r
+ * \r
+ *  A copy of the license is in apache_license.txt. It is also available here:\r
+ * @see: http://www.apache.org/licenses/LICENSE-2.0.txt\r
+ * \r
+ * Any republication or derived work distributed in source code form\r
+ * must include this copyright and license notice.\r
+ */\r
 package compbio.stat.collector;\r
 \r
 import java.io.File;\r
@@ -16,6 +33,8 @@ import java.util.Set;
 import org.apache.log4j.Logger;\r
 \r
 import compbio.engine.client.Executable;\r
+import compbio.engine.client.PathValidator;\r
+import compbio.engine.client.SkeletalExecutable;\r
 import compbio.metadata.JobStatus;\r
 import compbio.util.FileUtil;\r
 import compbio.ws.client.Services;\r
@@ -57,6 +76,7 @@ public class ExecutionStatCollector implements Runnable {
 \r
        static SimpleDateFormat DF = new SimpleDateFormat("dd/MM/yyyy hh:mm:ss");\r
 \r
+       final private File workDirectory;\r
        final private List<JobStat> stats;\r
        /**\r
         * Consider the job that has been working for longer than timeOutInHours\r
@@ -73,22 +93,18 @@ public class ExecutionStatCollector implements Runnable {
        public ExecutionStatCollector(String workDirectory, int timeOutInHours) {\r
                log.info("Starting stat collector for directory: " + workDirectory);\r
                log.info("Maximum allowed runtime(h): " + timeOutInHours);\r
-               File[] files = FileUtil.getFiles(workDirectory, directories);\r
+               if (!PathValidator.isValidDirectory(workDirectory)) {\r
+                       throw new IllegalArgumentException("workDirectory '"\r
+                                       + workDirectory + "' does not exist!");\r
+               }\r
+               this.workDirectory = new File(workDirectory);\r
                stats = new ArrayList<JobStat>();\r
-               assert timeOutInHours > 0;\r
-               this.timeOutInHours = timeOutInHours;\r
-               for (File file : files) {\r
-                       JobDirectory jd = new JobDirectory(file);\r
-                       JobStat jstat = jd.getJobStat();\r
-                       // Do not record stats on the job that has not completed yet\r
-                       if (hasCompleted(jd)) {\r
-                               stats.add(jstat);\r
-                       } else {\r
-                               log.debug("Skipping the job: " + jstat);\r
-                               log.debug("As it has not completed yet");\r
-                       }\r
-                       // System.out.println(jd.getJobStat().getJobReportTabulated());\r
+               if (timeOutInHours <= 0) {\r
+                       throw new IllegalArgumentException(\r
+                                       "Timeout value must be greater than 0! Given value: "\r
+                                                       + timeOutInHours);\r
                }\r
+               this.timeOutInHours = timeOutInHours;\r
        }\r
 \r
        boolean hasCompleted(JobDirectory jd) {\r
@@ -104,11 +120,18 @@ public class ExecutionStatCollector implements Runnable {
                return ((System.currentTimeMillis() - jd.jobdir.lastModified()) / (1000 * 60 * 60)) > timeOutInHours;\r
        }\r
 \r
-       public StatProcessor getStats() {\r
+       /*\r
+        * Make sure that collectStatistics methods was called prior to calling\r
+        * this! TODO consider running collectStatistics from here on the first call\r
+        */\r
+       StatProcessor getStats() {\r
+               if (stats.isEmpty()) {\r
+                       log.info("Please make sure collectStatistics method was called prior to calling getStats()!");\r
+               }\r
                return new StatProcessor(stats);\r
        }\r
 \r
-       public void writeStatToDB() throws SQLException {\r
+       void writeStatToDB() throws SQLException {\r
                Set<JobStat> rjobs = new HashSet<JobStat>(stats);\r
                StatDB statdb = new StatDB();\r
                log.debug("Removing records that has already been recorded");\r
@@ -125,10 +148,7 @@ public class ExecutionStatCollector implements Runnable {
         */\r
 \r
        /**\r
-        * \r
-        * @param args\r
-        * @throws IOException\r
-        * @throws SQLException\r
+        * Not in use\r
         */\r
        public static void main(String[] args) throws IOException, SQLException {\r
 \r
@@ -168,14 +188,14 @@ public class ExecutionStatCollector implements Runnable {
                File jobdir;\r
                Map<String, File> files = new HashMap<String, File>();\r
 \r
-               public JobDirectory(File directory) {\r
+               JobDirectory(File directory) {\r
                        this.jobdir = directory;\r
                        for (File f : jobdir.listFiles()) {\r
                                files.put(f.getName(), f);\r
                        }\r
                }\r
 \r
-               public boolean hasStatus(JobStatus status) {\r
+               boolean hasStatus(JobStatus status) {\r
                        return files.containsKey(status.toString());\r
                }\r
 \r
@@ -193,13 +213,25 @@ public class ExecutionStatCollector implements Runnable {
                        if (startfile == null) {\r
                                startfile = files.get(JobStatus.SUBMITTED.toString());\r
                        }\r
-                       if (startfile != null) {\r
+                       try {\r
+                               if (startfile != null) {\r
+                                       String start = FileUtil.readFileToString(startfile);\r
+                                       starttime = Long.parseLong(start.trim());\r
+                               }\r
+                       } catch (IOException ignore) {\r
+                               log.warn(\r
+                                               "IOException while reading STARTED status file! Ignoring...",\r
+                                               ignore);\r
+                               // fall back\r
+                               starttime = startfile.lastModified();\r
+                       } catch (NumberFormatException ignore) {\r
+                               log.warn(\r
+                                               "NumberFormatException while reading STARTED status file! Ignoring...",\r
+                                               ignore);\r
+                               // fall back\r
                                starttime = startfile.lastModified();\r
-                               /*\r
-                                * String start = FileUtil.readFileToString(startfile);\r
-                                * starttime = Long.parseLong(start.trim());\r
-                                */\r
                        }\r
+\r
                        return starttime;\r
                }\r
 \r
@@ -211,8 +243,9 @@ public class ExecutionStatCollector implements Runnable {
                                        clustjobId = FileUtil.readFileToString(jobid);\r
                                }\r
                        } catch (IOException ioe) {\r
-                               ioe.printStackTrace();\r
-                               // TODO LOG\r
+                               log.error(\r
+                                               "IO Exception while reading the content of JOBID file for job "\r
+                                                               + jobid, ioe);\r
                        }\r
                        return clustjobId.trim();\r
                }\r
@@ -221,19 +254,25 @@ public class ExecutionStatCollector implements Runnable {
                        long ftime = UNDEFINED;\r
                        File finished = files.get(JobStatus.FINISHED.toString());\r
                        if (finished != null) {\r
-                               ftime = finished.lastModified();\r
-                               /*\r
-                                * String start = FileUtil.readFileToString(finished); ftime =\r
-                                * Long.parseLong(start.trim());\r
-                                */\r
-                               // System.out.println("f " + ftime);\r
+                               try {\r
+                                       if (finished != null) {\r
+                                               String start = FileUtil.readFileToString(finished);\r
+                                               ftime = Long.parseLong(start.trim());\r
+                                       }\r
+                               } catch (IOException ignore) {\r
+                                       log.warn(\r
+                                                       "IOException while reading FINISHED status file! Ignoring...",\r
+                                                       ignore);\r
+                                       // fall back\r
+                                       ftime = finished.lastModified();\r
+                               } catch (NumberFormatException ignore) {\r
+                                       log.warn(\r
+                                                       "NumberFormatException while reading FINISHED status file! Ignoring...",\r
+                                                       ignore);\r
+                                       // fall back\r
+                                       ftime = finished.lastModified();\r
+                               }\r
                        }\r
-                       /*\r
-                        * } catch (IOException e) { log.log(Level.WARN,\r
-                        * "Cannot parse finished time: " + e.getMessage(), e); } catch\r
-                        * (NumberFormatException e) { log.log(Level.WARN,\r
-                        * "Cannot parse finished time: " + e.getMessage(), e); }\r
-                        */\r
                        return ftime;\r
                }\r
 \r
@@ -241,22 +280,19 @@ public class ExecutionStatCollector implements Runnable {
                        return Services.getServiceByJobDirectory(jobdir);\r
                }\r
 \r
-               // Mafft, Muscle, Tcoffee, Clustal task:fasta.in result:fasta.out\r
-               // Probcons task:fasta.in result:alignment.out\r
-               /*\r
-                * TODO replace with Universal names for WS!\r
-                */\r
                long getResultSize() {\r
                        Class<? extends Executable<?>> name = Services\r
                                        .getRunnerByJobDirectory(jobdir);\r
 \r
                        File f = null;\r
-                       if (name.getSimpleName().equalsIgnoreCase("Probcons")) {\r
-                               f = files.get("alignment.out");\r
-                       } else if (name.getSimpleName().equalsIgnoreCase("ClustalW")) {\r
-                               f = files.get("output.txt");\r
+                       if (name.getSimpleName().equalsIgnoreCase("IUPred")) {\r
+                               f = files.get("out.glob");\r
+                               if (f == null)\r
+                                       f = files.get("out.short");\r
+                               if (f == null)\r
+                                       f = files.get("out.long");\r
                        } else {\r
-                               f = files.get("fasta.out");\r
+                               f = files.get(SkeletalExecutable.OUTPUT);\r
                        }\r
                        if (f != null) {\r
                                return f.length();\r
@@ -265,7 +301,10 @@ public class ExecutionStatCollector implements Runnable {
                }\r
 \r
                long getInputSize() {\r
-                       File input = files.get("fasta.in");\r
+                       Class<? extends Executable<?>> name = Services\r
+                                       .getRunnerByJobDirectory(jobdir);\r
+\r
+                       File input = files.get(SkeletalExecutable.INPUT);\r
                        if (input != null) {\r
                                return input.length();\r
                        }\r
@@ -304,12 +343,30 @@ public class ExecutionStatCollector implements Runnable {
                                return false;\r
                        return true;\r
                }\r
+       }\r
 \r
+       void collectStatistics() {\r
+               File[] files = workDirectory.listFiles(directories);\r
+               for (File file : files) {\r
+                       JobDirectory jd = new JobDirectory(file);\r
+                       JobStat jstat = jd.getJobStat();\r
+                       // Do not record stats on the job that has not completed yet\r
+                       if (hasCompleted(jd)) {\r
+                               stats.add(jstat);\r
+                       } else {\r
+                               log.debug("Skipping the job: " + jstat);\r
+                               log.debug("As it has not completed yet");\r
+                       }\r
+                       // System.out.println(jd.getJobStat().getJobReportTabulated());\r
+               }\r
        }\r
 \r
        @Override\r
        public void run() {\r
                log.info("Started updating statistics at " + new Date());\r
+               log.info("For directory: " + workDirectory.getAbsolutePath());\r
+\r
+               collectStatistics();\r
 \r
                StatProcessor local_stats = getStats();\r
                log.info("Found " + local_stats.getJobNumber() + " jobs!");\r
@@ -321,5 +378,4 @@ public class ExecutionStatCollector implements Runnable {
                }\r
                log.info("Finished updating statistics at " + new Date());\r
        }\r
-\r
 }\r