Version Differences for Miscellaneous Scripts

(AHPSCRIPTS-33)
(AHPSCRIPTS-121)
Line 2371:
       
  EventService.getInstance().registerEventListener(new LoggingEventListener());</pre>    EventService.getInstance().registerEventListener(new LoggingEventListener());</pre> 
       
    + = Clean up Massive Job Log Directories =  
    + *A script to find and remove unnecessary (empty) directories in the job log (or other) directory in the anthill server.  
    + ==== AHPSCRIPTS-116 ====  
    + <pre>/**  
    + * It is advisable that this script only be run by Anthill administrators.  
    + * This script will only remove empty directories left by our cleanup policy.  
    + * Typically directories are not removed because they are likely to be reused  
    + * soon. For some clients, the sheer number of jobs requires these empty  
    + * directories to be managed, despite not being part of the more typical  
    + * cleanup policy.  
    + *  
    + * This script does not remove any files! Only directories.  
    + *  
    + * To prevent conflicts, it is advisable to run this script during a time that  
    + * uses very little activity.  
    + *  
    + * This script uses the AH3ServerJobLogDir property. Ensure this is set when  
    + * running the script.  
    + *  
    + * If the AH3ServerJobLogDir property does not conform to the expected value,  
    + * it will not proceed. This is to prevent the accidental deletion of logs or  
    + * directories.  
    + *  
    + * During testing, the performance of the script is about 2000 directory reads  
    + * per second, and about 1000 directory deletes per second. Milage may vary.  
    + *  
    + * With very little effort, this script could be modified to handle this  
    + * procedure on any accessible directory. Note that the safeguards in place  
    + * should also be updated accordingly.  
    + */  
    + import com.urbancode.devilfish.services.var.VarService;  
    + import com.urbancode.anthill3.main.DefaultConfiguration;  
       
    + /**  
    + * function to delete the given directory if and only if  
    + * it consists of empty directories or is empty itself.  
    + */  
    + int deleteEmptyDirs(File dir) {  
    + int deleteCount = 0;  
    + // we only do this to directories  
    + if (dir.isDirectory()) {  
    + // verify we can list the files here  
    + try {  
    + // first, recurse for all directories in this directory  
    + for (File subdir : dir.listFiles()) {  
    + if(subdir.isDirectory()) {  
    + deleteCount += deleteEmptyDirs(subdir);  
    + }  
    + }  
       
    + // only delete if we managed to eliminate the subdirectories  
    + if (dir.listFiles().length == 0 && dir.delete()) {  
    + deleteCount++;  
    + }  
    + }  
    + catch (Exception e) {  
    + System.out.println("Permission access error in " + dir.toString() + ".");  
    + }  
    + }  
    + return deleteCount;  
    + }  
       
    + /**  
    + * Begin actual script.  
    + */  
    + VarService vs = VarService.getInstance();  
    + String logDirStr = vs.getVarValue(DefaultConfiguration.LOGS_DIR) + File.separator + "job";  
    + File logDir = new File(logDirStr);  
    + if(logDir == null) {  
    + throw new IllegalStateException("Log parent directory not found: " + logDir);  
    + }  
    + if(!logDir.isDirectory()) {  
    + throw new IllegalStateException("Log parent directory strangely not a directory: " + logDir);  
    + }  
       
    + // init metrics  
    + int totalDeletes = 0;  
    + long start = System.currentTimeMillis();  
       
    + // perform the delete  
    + for(File dir : logDir.listFiles()) {  
    + totalDeletes += deleteEmptyDirs(dir);  
    + }  
    + // calc metrics  
    + long end = System.currentTimeMillis();  
    + long secs = (end-start) / 1000;  
    + double deletionRate = ((double)totalDeletes) / ((double)secs);  
    + System.out.println("Deleted " + totalDeletes + " empty directories in " + secs + " seconds (" + deletionRate + " deletes per second).");</pre>