【问题标题】:delayed job issue in rails.Rails 中的延迟作业问题。
【发布时间】:2011-03-04 05:14:55
【问题描述】:

我的控制器 data_files_controller.rb

def upload_balances
  DataFile.load_balances(params)
end

我的模型data_file.rb

def self.load_balances(params)
  #  Pull the file out of the http request, write it to file system
  name =  params['Filename']
  directory = "public/uploads"
  errors_table_name = "snapshot_errors"
  upload_file = File.join(directory, name)
  File.open(upload_file, "wb") { |f| f.write(params['Filedata'].read) }
  # Remove the old data from the table
  Balance.destroy_all
  # ------ more code-----
end

一切正常。现在我想用我的控制器使用延迟工作来调用我的模型动作,比如.. 我的控制器 data_files_controller.rb

def upload_balances
  DataFile.send_later(:load_balances,params)      
end

有可能吗??另一种方法是什么?有什么问题吗?

有了这个 send_later,我在 delay_job 表的 last_error 列中收到了这个错误。

uninitialized stream
C:/cyncabc/app/models/data_file.rb:12:in `read'
C:/cyncabc/app/models/data_file.rb:12:in `load_balances'
C:/cyncabc/app/models/data_file.rb:12:in `open'

我在网上遇到错误

File.open(upload_file, "wb") { |f| f.write(params['Filedata'].read) }

读取参数时['Filedata'].read.

如何检查我是否在 params['Filedata'] 中获取了正确的数据? 没有 send_later 它工作正常...有什么解决办法吗?

【问题讨论】:

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


    【解决方案1】:

    您应该检查数据库中的内容。延迟作业在运行时必须有字符串或内部 ID:很可能

    参数['文件数据']

    包含在稍后运行作业时无法恢复的内容。

    【讨论】:

    • 如果用户上传任何 csv 文件,那么第一个 load_balance 函数将在 public/upload 中创建该文件并从原始文件中写入内容。与使用更快的 csv 相比,它会将记录从文件输入到数据库。但它没有在数据库中存储任何 params['Filedata'] 或 params['Filename']。
    【解决方案2】:

    使用send_later 应该可以正常工作。另一种方式是定义自己的类来响应perform,并调用Delayed::Job.enqueue YourClass.new

    你是不是有什么地方出错了?

    【讨论】:

      猜你喜欢
      • 2011-10-04
      • 1970-01-01
      • 2013-11-26
      • 2012-04-20
      • 1970-01-01
      • 2016-07-23
      • 2023-04-03
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多