FIX: old csv's were not getting deleted

This commit is contained in:
Arpit Jalan 2015-01-14 21:30:51 +05:30
parent 6734a51b6a
commit c4da9ce62f
3 changed files with 12 additions and 23 deletions

View file

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

View file

@ -12,7 +12,7 @@ class UserExport < ActiveRecord::Base
def self.remove_old_exports
expired_exports = UserExport.where('created_at < ?', 2.days.ago).to_a
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}"
if File.exist?(file_path)

View file

@ -9,7 +9,7 @@ describe Jobs::ExportCsvFile do
end
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
let :user_list_export do