1 /* Copyright (c) 2013 Alexander Sherstnev
\r
3 * JAva Bioinformatics Analysis Web Services (JABAWS) @version: 2.1
\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
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
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
15 * Any republication or derived work distributed in source code form
\r
16 * must include this copyright and license notice.
\r
18 package compbio.stat.collector;
\r
20 import java.io.File;
\r
21 import java.io.FileFilter;
\r
22 import java.io.IOException;
\r
23 import java.sql.SQLException;
\r
24 import java.text.SimpleDateFormat;
\r
25 import java.util.ArrayList;
\r
26 import java.util.Date;
\r
28 import org.apache.log4j.Logger;
\r
30 import compbio.engine.Cleaner;
\r
31 import compbio.engine.client.Executable;
\r
32 import compbio.engine.client.PathValidator;
\r
33 import compbio.engine.client.SkeletalExecutable;
\r
34 import compbio.metadata.JobStatus;
\r
35 import compbio.stat.collector.JobStat;
\r
36 import compbio.util.FileUtil;
\r
39 * Number of runs of each WS = number of folders with name
\r
41 * Number of successful runs = all runs with no result file
\r
43 * Per period of time = limit per file creating time Runtime (avg/max) =
\r
45 * started time - finished time
\r
47 * Task & result size = result.size
\r
49 * Abandoned runs - not collected runs
\r
51 * Cancelled runs - cancelled
\r
53 * Cluster vs local runs
\r
55 * Reasons for failure = look in the err out?
\r
57 * Metadata required:
\r
59 * work directory for local and cluster tasks = from Helper or cmd parameter. WS
\r
60 * names - enumeration. Status file names and content.
\r
65 public class DirCleaner implements Runnable {
\r
67 static final int UNDEFINED = -1;
\r
69 private static final Logger log = Logger.getLogger(DirCleaner.class);
\r
71 final private File workDirectory;
\r
72 final private int LifeSpanInHours;
\r
76 * @param workDirectory
\r
77 * @param timeOutInHours
\r
79 public DirCleaner(String workDirectory, int LifeSpanInHours) {
\r
80 log.info("Starting cleaning for directory: " + workDirectory);
\r
81 log.info("Maximum allowed directory life span (h): " + LifeSpanInHours);
\r
82 if (!PathValidator.isValidDirectory(workDirectory)) {
\r
83 throw new IllegalArgumentException("workDirectory '" + workDirectory + "' does not exist!");
\r
85 this.workDirectory = new File(workDirectory);
\r
86 this.LifeSpanInHours = LifeSpanInHours;
\r
89 boolean hasCompleted(JobDirectory jd) {
\r
90 JobStat jstat = jd.getJobStat();
\r
91 if (jstat.hasResult() || jstat.getIsCancelled() || jstat.getIsFinished()) {
\r
97 boolean livesOverLifeSpan(JobDirectory jd) {
\r
98 return ((System.currentTimeMillis() - jd.jobdir.lastModified()) / (1000 * 60 * 60)) > LifeSpanInHours;
\r
101 static FileFilter directories = new FileFilter() {
\r
103 public boolean accept(File pathname) {
\r
104 return pathname.isDirectory() && !pathname.getName().startsWith(".");
\r
109 void doCleaning() {
\r
110 File[] dirs = workDirectory.listFiles(directories);
\r
111 for (File dir : dirs) {
\r
112 // Do not look at dirs with unfinished jobs
\r
113 JobDirectory jd = new JobDirectory(dir);
\r
114 if (hasCompleted(jd) && livesOverLifeSpan(jd)) {
\r
115 Cleaner.deleteDirectory(workDirectory.getAbsolutePath() + File.separator + dir.getName());
\r
116 log.debug("Directory " + dir.getName() + " is deleted in doCleaning");
\r
118 log.debug("Directory " + dir.getName() + " is too new and kept in doCleaning");
\r
123 public void run() {
\r
124 log.info("Started cleaning job directory at " + new Date());
\r
125 log.info("For directory: " + workDirectory.getAbsolutePath());
\r
127 log.info("Finished cleaning job directory at " + new Date());
\r