1 package hudson.model; 2 3 import hudson.FilePath; 4 import hudson.util.StreamTaskListener; 5 6 import java.io.File ; 7 import java.io.FileFilter ; 8 import java.io.FileOutputStream ; 9 import java.io.IOException ; 10 import java.io.OutputStream ; 11 import java.io.Serializable ; 12 import java.util.Date ; 13 import java.util.List ; 14 import java.util.logging.Level ; 15 16 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 OutputStream os = new FileOutputStream ( 40 new File (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 e) { 49 e.printStackTrace(listener.fatalError("aborted")); 50 } finally { 51 os.close(); 52 } 53 } catch (IOException e) { 54 logger.log(Level.SEVERE, "Failed to access log file",e); 55 } 56 } 57 58 private void process(Hudson h) throws IOException , InterruptedException { 59 File jobs = new File (h.getRootDir(), "jobs"); 60 File [] dirs = jobs.listFiles(DIR_FILTER); 61 if(dirs==null) return; 62 for (File dir : dirs) { 63 FilePath ws = new FilePath(new File (dir, "workspace")); 64 if(shouldBeDeleted(dir.getName(),ws,h)) { 65 delete(ws); 66 } 67 } 68 } 69 70 private boolean shouldBeDeleted(String jobName, FilePath dir, Node n) throws IOException , InterruptedException { 71 TopLevelItem item = Hudson.getInstance().getItem(jobName); 74 if(item==null) 75 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 return false; 87 } 88 89 return dir.lastModified() + 30 * DAY < new Date ().getTime(); 91 92 } 93 94 private void process(Slave s) throws InterruptedException { 95 listener.getLogger().println("Scanning "+s.getNodeName()); 96 97 try { 98 List <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 e) { 105 e.printStackTrace(listener.error("Failed on "+s.getNodeName())); 106 } 107 } 108 109 private void delete(FilePath dir) throws InterruptedException { 110 try { 111 listener.getLogger().println("Deleting "+dir); 112 dir.deleteRecursive(); 113 } catch (IOException e) { 114 e.printStackTrace(listener.error("Failed to delete "+dir)); 115 } 116 } 117 118 119 private static class DirectoryFilter implements FileFilter , Serializable { 120 public boolean accept(File f) { 121 return f.isDirectory(); 122 } 123 private static final long serialVersionUID = 1L; 124 } 125 private static final FileFilter DIR_FILTER = new DirectoryFilter(); 126 127 private static final long DAY = 1000*60*60*24; 128 } 129 | Popular Tags |