Version Differences for Miscellaneous Scripts

(AHPSCRIPTS-62)
(AHPSCRIPTS-34)
Line 1220:
  log.warn("Workflow property configured to NOT clean the working directory.");    log.warn("Workflow property configured to NOT clean the working directory."); 
  }</pre>    }</pre> 
       
    + = Script to Cleanup Job & Workflow Logs from AHP Server =  
    + * You can have an operations project that runs an evaluate script step with the following contents. This is safe to run any time but should only be needed for 3.5/3.6 instances that did not do the cleanup during the upgrade:  
    + ==== AHPSCRIPTS-92 ====  
    + <pre>import com.urbancode.anthill3.services.logging.LogNamingHelper;  
    + import com.urbancode.anthill3.spring.SpringSupport;  
    + import com.urbancode.commons.fileutils.FileUtils;  
    + import org.springframework.jdbc.core.JdbcTemplate;  
       
    + import javax.sql.DataSource;  
    + import java.io.File;  
    + import java.io.FileFilter;  
    + import java.io.PrintStream;  
    + import java.util.HashSet;  
    + import java.util.List;  
    + import java.util.Set;  
       
    + int jobCount = 0;  
    + int wcCount = 0;  
       
    + void cleanupDirectory(File dir, String baseDirPath, Set idSet, long highestJobId) {  
    + if (dir.isDirectory()) {  
    + File[] fileArray = dir.listFiles(new FileFilter() {  
    + public boolean accept(File pathname) {  
    + return pathname.isDirectory();  
    + }  
    + });  
    + for (File file : fileArray) {  
    + cleanupDirectory(file, baseDirPath, idSet, highestJobId);  
    + }  
       
    + commandOutput.println("\t\tCleaning up: " + dir.getAbsolutePath());  
    + String[] tokens = dir.getAbsolutePath().substring(baseDirPath.length()).split(File.separator);  
    + StringBuffer buffer = new StringBuffer();  
    + for (int i = tokens.length - 1; i > -1; i--) {  
    + buffer.append(tokens[i]);  
    + }  
    + long jobId = Long.parseLong(buffer.toString());  
    + if (!idSet.contains(jobId) && jobId < highestJobId) {  
    + File[] fileList = dir.listFiles(new FileFilter(){public boolean accept(File pathname) {return !pathname.isDirectory();}});  
    + if (fileList.length > 0) {  
    + commandOutput.println("Found orphaned dir: " + dir.getAbsolutePath());  
    + jobCount++;  
    + for (File file : fileList) {  
    + file.delete();  
    + }  
    + }  
    + }  
    + if (dir.list().length == 0) {  
    + dir.delete();  
    + }  
    + }  
    + }  
       
    + void cleanupWcDirectory(File dir, String baseDirPath, Set idSet, long highestWcId) {  
    + if (dir.isDirectory()) {  
    + File[] fileArray = dir.listFiles(new FileFilter() {  
    + public boolean accept(File pathname) {  
    + return pathname.isDirectory();  
    + }  
    + });  
    + commandOutput.println("\t\tCleaning up: " + dir.getAbsolutePath());  
    + for (File file : fileArray) {  
    + if (Character.isDigit(file.getName().charAt(0))) {  
    + cleanupWcDirectory(file, baseDirPath, idSet, highestWcId);  
    + }  
    + }  
       
    + String[] tokens = dir.getAbsolutePath().substring(baseDirPath.length()).split(File.separator);  
    + StringBuffer buffer = new StringBuffer();  
    + for (int i = tokens.length - 1; i > -1; i--) {  
    + buffer.append(tokens[i]);  
    + }  
    + long wcId = Long.parseLong(buffer.toString());  
    + if (!idSet.contains(wcId) && wcId < highestWcId) { File[] fileList = dir.listFiles(new FileFilter(){  
    + public boolean accept(File pathname) {  
    + return !pathname.isDirectory() || pathname.getName().equals("wf");  
    + }  
    + });  
    + if (fileList.length > 0) {  
    + commandOutput.println("Found orphaned dir: " + dir.getAbsolutePath());  
    + wcCount++;  
    + for (File file : fileList) {  
    + FileUtils.deleteFile(file);  
    + }  
    + }  
    + }  
    + if (dir.list().length == 0) {  
    + dir.delete();  
    + }  
    + }  
    + }  
    + try {  
    + DataSource ds = (DataSource)SpringSupport.getInstance().getBean("dataSource");  
    + JdbcTemplate jdbcTemplate = new JdbcTemplate(ds);  
       
    + String logsDirString = LogNamingHelper.getInstance().getLogDirPath() + File.separator + "job";  
    + commandOutput.println("Log files located at: " + logsDirString);  
    + long highestJobId = jdbcTemplate.queryForLong("SELECT HI_VAL FROM HI_LO_SEQ WHERE SEQ_NAME = 'JOB_TRACE'");  
    + List idList = jdbcTemplate.queryForList("SELECT ID FROM JOB_TRACE", Long.class);  
    + Set idSet = new HashSet();  
    + idSet.addAll(idList);  
    + commandOutput.println("Found " + idSet.size() + " known jobs - highest job id " + highestJobId);  
    + commandOutput.println("Deleting orphaned job log and output files.");  
    + commandOutput.println(" This may take a while, please be patient...");  
       
    + File baseDir = new File(logsDirString);  
    + File[] fileArray = baseDir.listFiles(new FileFilter() {  
    + public boolean accept(File pathname) {  
    + return pathname.isDirectory();  
    + }  
    + });  
    + for (File file : fileArray) {  
    + cleanupDirectory(file, logsDirString, idSet, highestJobId);  
    + }  
       
    + String wcDirString = LogNamingHelper.getInstance().getLogDirPath() + File.separator + "wf";  
    + commandOutput.println("Log files located at: " + logsDirString);  
    + long highestWcId = jdbcTemplate.queryForLong("SELECT HI_VAL FROM HI_LO_SEQ WHERE SEQ_NAME = 'WORKFLOW_CASE'");  
    + idList = jdbcTemplate.queryForList("SELECT ID FROM WORKFLOW_CASE", Long.class);  
    + idSet.clear();  
    + idSet.addAll(idList);  
    + commandOutput.println("---------------------------------------------------------");  
    + commandOutput.println("Found " + idSet.size() + " known workflow cases - highest workflow case id " + highestWcId);  
    + commandOutput.println("Deleting orphaned workflow case log and output files.");  
    + commandOutput.println(" This may take a while, please be patient...");  
       
    + baseDir = new File(wcDirString);  
    + fileArray = baseDir.listFiles(new FileFilter() {  
    + public boolean accept(File pathname) {  
    + return pathname.isDirectory();  
    + }  
    + });  
    + for (File file : fileArray) {  
    + cleanupWcDirectory(file, wcDirString, idSet, highestWcId);  
    + }  
       
    + commandOutput.println("Cleanup Complete!");  
    + commandOutput.println("Removed " + jobCount + " job and " + wcCount + " workflow case orphaned directories!");  
    + }  
    + catch (Exception e) {  
    + e.printStackTrace(commandOutput);  
    + }</pre>