From c4eb06f5ba9447a7731cc41a6b2e56da70c86300 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=B3=A5=E4=BA=95=20=E9=9B=AA?= Date: Wed, 17 Dec 2014 18:09:04 +0900 Subject: [PATCH] Chamge method name #backup_files -> #backup_files_in_old_order to make its sort function clearer, and Refactor #remove_over_backup_files by using it --- app/models/fluentd/agent/local_common.rb | 16 +++++++++++----- 1 file changed, 11 insertions(+), 5 deletions(-) diff --git a/app/models/fluentd/agent/local_common.rb b/app/models/fluentd/agent/local_common.rb index 02162f1..a4b6cbd 100644 --- a/app/models/fluentd/agent/local_common.rb +++ b/app/models/fluentd/agent/local_common.rb @@ -64,11 +64,15 @@ class Fluentd end def backup_files - Dir.glob("#{config_backup_dir}/*").sort + Dir.glob("#{config_backup_dir}/*") + end + + def backup_files_in_old_order + backup_files.sort end def backup_files_in_new_order - backup_files.reverse + backup_files_in_old_order.reverse end private @@ -84,9 +88,11 @@ class Fluentd def remove_over_backup_files over_file_count = backup_files.size - MAX_BACKUP_FILE_NUM - #.times do nothing if over_file_count is negative or 0. - over_file_count.times do |i| - FileUtils.rm(backup_files.shift) + return if over_file_count <= 0 + + backup_files_in_old_order.first(over_file_count).each do |file| + next unless File.exist? file + FileUtils.rm(file) end end