Merge pull request #3099 from techAPJ/patch-1

FIX: old csv's were not getting deleted
This commit is contained in:
Robin Ward 2015-01-14 13:44:31 -05:00
commit fcba826202
3 changed files with 12 additions and 23 deletions

View File

@ -17,22 +17,12 @@ module Jobs
sidekiq_options retry: false sidekiq_options retry: false
attr_accessor :current_user attr_accessor :current_user
def initialize
@file_name = ""
@entity_type = "admin"
end
def execute(args) def execute(args)
entity = args[:entity] @entity = args[:entity]
@file_name = entity @file_name = @entity
if entity == "user_archive"
@entity_type = "user"
end
@current_user = User.find_by(id: args[:user_id]) @current_user = User.find_by(id: args[:user_id])
export_method = "#{entity}_export".to_sym export_method = "#{@entity}_export".to_sym
data = data =
if respond_to?(export_method) if respond_to?(export_method)
send(export_method) send(export_method)
@ -42,8 +32,7 @@ module Jobs
if data && data.length > 0 if data && data.length > 0
set_file_path set_file_path
header = get_header(entity) write_csv_file(data)
write_csv_file(data, header)
end end
ensure ensure
@ -102,9 +91,9 @@ module Jobs
end end
end end
def get_header(entity) def get_header
case entity case @entity
when 'user_list' when 'user_list'
header_array = HEADER_ATTRS_FOR['user_list'] + HEADER_ATTRS_FOR['user_stats'] header_array = HEADER_ATTRS_FOR['user_list'] + HEADER_ATTRS_FOR['user_stats']
if SiteSetting.enable_sso if SiteSetting.enable_sso
@ -118,7 +107,7 @@ module Jobs
end end
header_array.push("group_names") header_array.push("group_names")
else else
header_array = HEADER_ATTRS_FOR[entity] header_array = HEADER_ATTRS_FOR[@entity]
end end
header_array header_array
@ -271,8 +260,8 @@ module Jobs
def set_file_path def set_file_path
@file = UserExport.create(export_type: @entity_type, user_id: @current_user.id)
file_name_prefix = @file_name.split('_').join('-') file_name_prefix = @file_name.split('_').join('-')
@file = UserExport.create(export_type: file_name_prefix, user_id: @current_user.id)
@file_name = "#{file_name_prefix}-#{@file.id}.csv" @file_name = "#{file_name_prefix}-#{@file.id}.csv"
# ensure directory exists # ensure directory exists
@ -280,10 +269,10 @@ module Jobs
FileUtils.mkdir_p(dir) unless Dir.exists?(dir) FileUtils.mkdir_p(dir) unless Dir.exists?(dir)
end end
def write_csv_file(data, header) def write_csv_file(data)
# write to CSV file # write to CSV file
CSV.open(File.expand_path("#{UserExport.base_directory}/#{@file_name}", __FILE__), "w") do |csv| CSV.open(File.expand_path("#{UserExport.base_directory}/#{@file_name}", __FILE__), "w") do |csv|
csv << header csv << get_header
data.each do |value| data.each do |value|
csv << value csv << value
end end

View File

@ -12,7 +12,7 @@ class UserExport < ActiveRecord::Base
def self.remove_old_exports def self.remove_old_exports
expired_exports = UserExport.where('created_at < ?', 2.days.ago).to_a expired_exports = UserExport.where('created_at < ?', 2.days.ago).to_a
expired_exports.map do |expired_export| expired_exports.map do |expired_export|
file_name = "export_#{expired_export.id}.csv" file_name = "#{expired_export.export_type}-#{expired_export.id}.csv.gz"
file_path = "#{UserExport.base_directory}/#{file_name}" file_path = "#{UserExport.base_directory}/#{file_name}"
if File.exist?(file_path) if File.exist?(file_path)

View File

@ -9,7 +9,7 @@ describe Jobs::ExportCsvFile do
end end
let :user_list_header do let :user_list_header do
Jobs::ExportCsvFile.new.get_header('user_list') ['id','name','username','email','title','created_at','trust_level','active','admin','moderator','ip_address','topics_entered','posts_read_count','time_read','topic_count','post_count','likes_given','likes_received','external_id','external_email', 'external_username', 'external_name', 'external_avatar_url']
end end
let :user_list_export do let :user_list_export do