private void initQueueDir() { queueDir = new File(homeDir, "queue"); queueCleanupScheduler = Executors.newScheduledThreadPool(1); queueCleanupScheduler.scheduleAtFixedRate(new Runnable() { @Override public void run() { if (!queueDir.isDirectory()) return; long cutoff = System.currentTimeMillis() - (10 * 60 * 1000); // 10 minutes old File[] oldFiles = queueDir.listFiles((FileFilter) new AgeFileFilter(cutoff)); for (File file : oldFiles) { if (!FileUtils.deleteQuietly(file)) logger.error("Could not delete stale file \"" + file.getAbsolutePath() + "\" in queue folder"); } } }, 10, 10, TimeUnit.MINUTES); }
private void deleteBackupFilesOlderThen(File backupDir, int days){ try { if (backupDir.isDirectory()) { Collection<File> filesToDelete = FileUtils.listFiles(backupDir, new AgeFileFilter(DateUtils.addDays(new Date(), days * -1)), TrueFileFilter.TRUE); for (File file : filesToDelete) { boolean success = FileUtils.deleteQuietly(file); if (!success) { LoggerFactory.getLogger(getClass()) .warn("Cannot delete old backup file at: " + file.getAbsolutePath()); } } } } catch (Exception e) { LoggerFactory.getLogger(getClass()).warn("Cannot delete old backup files.", e); } }
void cleanup() { String taskLogsHome = getTaskLogHome(); if (taskLogsHome == null) { // we are forgiving if the task logs home is not defined. Just log a message with a call to action. log.warn("Unable to cleanup task log files. Please check that the 'tasklogfile' appender exists in logback.xml"); return; } File logFilesHome = new File(taskLogsHome); log.info("Cleaning up log files in {} older than {} days", logFilesHome.getAbsolutePath(), numberOfDays); LocalDate now = LocalDate.now().minusDays(numberOfDays); Date thresholdDate = Date.from(now.atStartOfDay(ZoneId.systemDefault()).toInstant()); AgeFileFilter ageFileFilter = new AgeFileFilter(thresholdDate); Iterator<File> filesToDelete = iterateFiles(logFilesHome, ageFileFilter, ageFileFilter); filesToDelete.forEachRemaining(f -> { try { forceDelete(f); log.info("Removed task log file {}", f.toString()); } catch (IOException e) { // NOSONAR log.error("Unable to delete task file {}. Message was {}.", f.toString(), e.getMessage()); } } ); }
/** * Removes aged deleted segments from the deleted directory * @param retentionInDays: retention for deleted segments in days */ public void removeAgedDeletedSegments(int retentionInDays) { if (_localDiskDir != null) { File deletedDir = new File(_localDiskDir, DELETED_SEGMENTS); // Check that the directory for deleted segments exists if (!deletedDir.isDirectory()) { LOGGER.warn("Deleted segment directory {} does not exist or it is not directory.", deletedDir.getAbsolutePath()); return; } AgeFileFilter fileFilter = new AgeFileFilter(DateTime.now().minusDays(retentionInDays).toDate()); File[] directories = deletedDir.listFiles((FileFilter) DirectoryFileFilter.DIRECTORY); // Check that the directory for deleted segments is empty if (directories == null) { LOGGER.warn("Deleted segment directory {} does not exist or it caused an I/O error.", deletedDir); return; } for (File currentDir : directories) { // Get files that are aged Collection<File> targetFiles = FileUtils.listFiles(currentDir, fileFilter, null); // Delete aged files for (File f : targetFiles) { if (!f.delete()) { LOGGER.warn("Cannot remove file {} from deleted directory.", f.getAbsolutePath()); } } // Delete directory if it's empty if (currentDir.list() != null && currentDir.list().length == 0) { if (!currentDir.delete()) { LOGGER.warn("The directory {} cannot be removed. The directory may not be empty.", currentDir.getAbsolutePath()); } } } } else { LOGGER.info("localDiskDir is not configured, won't delete any expired segments from deleted directory."); } }
/** * Automatically exports the usage statistics to a file. All files older then 30 days from the * statistics directory will be deleted. This method can only be executed once. * * @throws IOException */ public void autoExportStatistics() throws IOException{ TimeTool t = new TimeTool(System.currentTimeMillis()); String dir = CoreHub.getWritableUserDir().getAbsolutePath() + File.separator + "statistics"; String fileName = "usage" + t.toString(TimeTool.TIMESTAMP) + ".xml"; if (!disableAutoExport) { try { File directory = new File(dir); if (directory.isDirectory()) { Collection<File> filesToDelete = FileUtils.listFiles(directory, new AgeFileFilter(DateUtils.addDays(new Date(), -30)), TrueFileFilter.TRUE); for (File file : filesToDelete) { boolean success = FileUtils.deleteQuietly(file); if (!success) { LoggerFactory.getLogger(getClass()) .warn("Cannot delete old file at: " + file.getAbsolutePath()); } } } } catch (Exception e) { LoggerFactory.getLogger(getClass()).warn("Cannot delete old files.", e); } exportStatisticsToFile(dir + File.separator + fileName); disableAutoExport = true; } }
private void setupFS() throws IOException { log.config("Setting up filesystem..."); Collection<File> directories = new ArrayList<File>(); File reportsfolder = new File("ng-reports"); directories.add(bridge.getNGFolder()); directories.add(new File(bridge.getNGFolder(), "config")); directories.add(new File(bridge.getNGFolder(), "data")); directories.add(new File(bridge.getNGFolder(), "commands")); directories.add(new File(bridge.getNGFolder(), "scripts")); directories.add(reportsfolder); for (File f : directories) { if (f == null) continue; if (!f.exists()) { f.mkdir(); } else { if (!f.isDirectory()) { if (f.delete()) { f.mkdir(); } else { reportError(new IOException("Cannot fix " + f.getName() + ".")); } } } } if (!System.getProperty("nekoooguilds.dontcleanreports", "false").equalsIgnoreCase("true")) { log.config("Cleaning up old reports..."); LocalDate today = LocalDate.now(); LocalDate eailer = today.minusDays(14); Date threshold = Date.from(eailer.atStartOfDay(ZoneId.systemDefault()).toInstant()); AgeFileFilter filter = new AgeFileFilter(threshold); File[] remove = FileFilterUtils.filter(filter, reportsfolder); for (File file : remove) { if (file.delete()) log.config("Deleted " + file.getName()); } } else { log.info("Report cleanup disabled with JVM/PropertyLoader option."); } }
protected void executeReadFolder(final CommandLine cmdLine) throws IOException, JMSException, ScriptException { final long fileAgeMS = 1000; String pathFilter = cmdLine.getOptionValue(CMD_READ_FOLDER); if( pathFilter.isEmpty() ){ output("Option " + CMD_READ_FOLDER + " requires a path and wildcard filename."); } // expression will be like: /path/to/file/*.txt // Last index of / will divide filter from path File directory = Paths.get(".").toFile(); String filter = pathFilter; int indexOfPathFilterSeparator = pathFilter.lastIndexOf('/'); if( indexOfPathFilterSeparator > 0){ // path + filename/filter String path = pathFilter.substring(0, indexOfPathFilterSeparator); directory = new File(path); if ( pathFilter.endsWith("/")) { output("Option " + CMD_READ_FOLDER + " cannot end with /. Please pass a wildcard filename after path."); return; } else { filter = pathFilter.substring(indexOfPathFilterSeparator + 1); } } AndFileFilter fileFilters = new AndFileFilter(); fileFilters.addFileFilter(new WildcardFileFilter(filter)); fileFilters.addFileFilter(new AgeFileFilter(System.currentTimeMillis() - fileAgeMS)); long startTime = System.currentTimeMillis(); long waitTime = Long.parseLong(cmdLine.getOptionValue(CMD_WAIT,"0")); long endTime = startTime + waitTime; do { Collection<File> files = FileUtils.listFiles(directory, fileFilters, null); // no recursion for(File file : files) { putData("@"+file.getAbsolutePath(),cmdLine); if (!file.delete()) { output("Failed to delete file " + file.getName()); } output("File " + file.getName() + " sent"); } try { Thread.sleep(SLEEP_TIME_BETWEEN_FILE_CHECK); } catch (InterruptedException e) { output("Interrupted"); break; } } while( endTime > System.currentTimeMillis()); }
@Action(value = "/admin/push", results = { @Result(name = "success", location = "/admin/upload_result.jsp") } ) public String push() { Long userId=AuthUtil.getUserId(servletRequest.getSession()); Long sessionId=AuthUtil.getSessionId(servletRequest.getSession()); try { //get next pending system pendingSystemStatus = SystemStatusDB.getNextPendingSystem(userId); if (pendingSystemStatus != null) { //get session for system SchSession session = null; for(Integer instanceId : SecureShellAction.getUserSchSessionMap().get(sessionId).getSchSessionMap().keySet()) { //if host system id matches pending system then upload if(pendingSystemStatus.getId().equals(SecureShellAction.getUserSchSessionMap().get(sessionId).getSchSessionMap().get(instanceId).getHostSystem().getId())){ session = SecureShellAction.getUserSchSessionMap().get(sessionId).getSchSessionMap().get(instanceId); } } if(session!=null) { //push upload to system currentSystemStatus = SSHUtil.pushUpload(pendingSystemStatus, session.getSession(), UPLOAD_PATH + "/" + uploadFileName, pushDir + "/" + uploadFileName); //update system status SystemStatusDB.updateSystemStatus(currentSystemStatus, userId); pendingSystemStatus = SystemStatusDB.getNextPendingSystem(userId); } } //if push has finished to all servers then delete uploaded file if (pendingSystemStatus == null) { File delFile = new File(UPLOAD_PATH, uploadFileName); FileUtils.deleteQuietly(delFile); //delete all expired files in upload path File delDir = new File(UPLOAD_PATH); if (delDir.isDirectory()) { //set expire time to delete all files older than 48 hrs Calendar expireTime = Calendar.getInstance(); expireTime.add(Calendar.HOUR, - 48); Iterator<File> filesToDelete = FileUtils.iterateFiles(delDir, new AgeFileFilter(expireTime.getTime()), TrueFileFilter.TRUE); while(filesToDelete.hasNext()) { delFile=filesToDelete.next(); delFile.delete(); } } } hostSystemList = SystemStatusDB.getAllSystemStatus(userId); } catch (Exception e) { log.error(e.toString(), e); } return SUCCESS; }
/** * Walks the baseDir recursively and deletes files and directories older than cutoff. * When traversing directories, does not follow symbolic links. * * If a directory is old but contains files (which are not too old), it is not deleted. * * TODO better problem handling? * * @param baseDir * @param cutoff milliseconds * @throws IOException */ public static void cleanOldFiles(File baseDir, long cutoff ) throws IOException { walkAndDelete(baseDir, new AgeFileFilter(System.currentTimeMillis() - cutoff)); }