KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > hudson > model > WorkspaceCleanupThread


1 package hudson.model;
2
3 import hudson.FilePath;
4 import hudson.util.StreamTaskListener;
5
6 import java.io.File JavaDoc;
7 import java.io.FileFilter JavaDoc;
8 import java.io.FileOutputStream JavaDoc;
9 import java.io.IOException JavaDoc;
10 import java.io.OutputStream JavaDoc;
11 import java.io.Serializable JavaDoc;
12 import java.util.Date JavaDoc;
13 import java.util.List JavaDoc;
14 import java.util.logging.Level JavaDoc;
15
16 /**
17  * Clean up old left-over workspaces from slaves.
18  *
19  * @author Kohsuke Kawaguchi
20  */

21 public class WorkspaceCleanupThread extends PeriodicWork {
22     private static WorkspaceCleanupThread theInstance;
23
24     public WorkspaceCleanupThread() {
25         super("Workspace clean-up");
26         theInstance = this;
27     }
28
29     public static void invoke() {
30         theInstance.run();
31     }
32
33     private TaskListener listener;
34
35     protected void execute() {
36         Hudson h = Hudson.getInstance();
37         try {
38             // don't buffer this, so that the log shows what the worker thread is up to in real time
39
OutputStream JavaDoc os = new FileOutputStream JavaDoc(
40                 new File JavaDoc(h.getRootDir(),"workspace-cleanup.log"));
41             try {
42                 listener = new StreamTaskListener(os);
43
44                 for (Slave s : h.getSlaves())
45                     process(s);
46
47                 process(h);
48             } catch (InterruptedException JavaDoc e) {
49                 e.printStackTrace(listener.fatalError("aborted"));
50             } finally {
51                 os.close();
52             }
53         } catch (IOException JavaDoc e) {
54             logger.log(Level.SEVERE, "Failed to access log file",e);
55         }
56     }
57
58     private void process(Hudson h) throws IOException JavaDoc, InterruptedException JavaDoc {
59         File JavaDoc jobs = new File JavaDoc(h.getRootDir(), "jobs");
60         File JavaDoc[] dirs = jobs.listFiles(DIR_FILTER);
61         if(dirs==null) return;
62         for (File JavaDoc dir : dirs) {
63             FilePath ws = new FilePath(new File JavaDoc(dir, "workspace"));
64             if(shouldBeDeleted(dir.getName(),ws,h)) {
65                 delete(ws);
66             }
67         }
68     }
69
70     private boolean shouldBeDeleted(String JavaDoc jobName, FilePath dir, Node n) throws IOException JavaDoc, InterruptedException JavaDoc {
71         // TODO: the use of remoting is not optimal.
72
// One remoting can execute "exists", "lastModified", and "delete" all at once.
73
TopLevelItem item = Hudson.getInstance().getItem(jobName);
74         if(item==null)
75             // no such project anymore
76
return true;
77
78         if(!dir.exists())
79             return false;
80
81         if (item instanceof Project) {
82             Project p = (Project) item;
83             Node lb = p.getLastBuiltOn();
84             if(lb!=null && lb.equals(n))
85                 // this is the active workspace. keep it.
86
return false;
87         }
88
89         // if older than a month, delete
90
return dir.lastModified() + 30 * DAY < new Date JavaDoc().getTime();
91
92     }
93
94     private void process(Slave s) throws InterruptedException JavaDoc {
95         listener.getLogger().println("Scanning "+s.getNodeName());
96
97         try {
98             List JavaDoc<FilePath> dirs = s.getWorkspaceRoot().list(DIR_FILTER);
99             if(dirs ==null) return;
100             for (FilePath dir : dirs) {
101                 if(shouldBeDeleted(dir.getName(),dir,s))
102                     delete(dir);
103             }
104         } catch (IOException JavaDoc e) {
105             e.printStackTrace(listener.error("Failed on "+s.getNodeName()));
106         }
107     }
108
109     private void delete(FilePath dir) throws InterruptedException JavaDoc {
110         try {
111             listener.getLogger().println("Deleting "+dir);
112             dir.deleteRecursive();
113         } catch (IOException JavaDoc e) {
114             e.printStackTrace(listener.error("Failed to delete "+dir));
115         }
116     }
117
118
119     private static class DirectoryFilter implements FileFilter JavaDoc, Serializable JavaDoc {
120         public boolean accept(File JavaDoc f) {
121             return f.isDirectory();
122         }
123         private static final long serialVersionUID = 1L;
124     }
125     private static final FileFilter JavaDoc DIR_FILTER = new DirectoryFilter();
126
127     private static final long DAY = 1000*60*60*24;
128 }
129
Popular Tags