上传具有延迟作业的csv文件

时间:2010-06-15 16:55:28

标签: ruby-on-rails ruby delayed-job

我有一个rake任务loaddata.rake。

require File.join(File.dirname(__FILE__), 'load_test_data.rb')
namespace :test do
  desc "Insert test data into the database"
  task(:loadtest => :environment) do
    puts "environment = #{RAILS_ENV}"
    puts "load_test_data            ";   load_test_data

    puts "DONE!"
  end
end

我有另一个文件load_test_data.rb我有一些函数来读取csv文件并将数据存储在数据库中。我从我的rake任务中调用该文件。

This is my load_test_data.rb
require "faster_csv"

def load_test_data
  puts "test: Balance"
  directory = "test/data"
  name = "balances.csv"
  lines = get_lines_from_csv(directory,name,"snapshot_errors")
  unless lines.nil?
    row_number = 1
    lines.each do |row|
      unless row.header_row?
        begin
          attributes = row.to_hash
          attributes[:balance_name_id] = BalanceName.find(:first, :conditions =>            ["name = ?", attributes[:balance_name]]).id
          attributes.delete(:balance_name)
          Balance.create! attributes
        rescue
          store_error_in_table(row.fields.to_csv,name,row_number,"snapshot_errors")
        end
        row_number += 1
      end
    end
  end
end

Rake任务正常运行我的代码..

现在我想从延迟工作的rake任务中调用此函数..无论如何我能处理吗?

1 个答案:

答案 0 :(得分:1)

您可以通过系统命令

调用它

system 'rake test:loadtest'