我正在开发rails项目,我正在生成用户模型的excel文件,然后上传到amazon s3!到目前为止所做的一切都很完美,但是我也希望使用延迟工作,问题出现了, 当我用延迟调用方法时,工作无法完成,但我也没有收到任何错误!如果我运行延迟的工作日志,我得到了这个:
[Worker(host:pure pid:11063)] Starting job worker
[Worker(host:pure pid:11063)] Job Delayed::PerformableMethod (id=77) RUNNING
[Worker(host:pure pid:11063)] Job Delayed::PerformableMethod (id=78) RUNNING
[Worker(host:pure pid:11063)] Job Delayed::PerformableMethod (id=79) RUNNING
[Worker(host:pure pid:11063)] Job Delayed::PerformableMethod (id=80) RUNNING
似乎这项工作已经停滞不前,无法完成这个过程!
以下是我的所有方法:
用户控制器:
def export
if params[:search]
@users = User.all.order('created_at DESC')
else
@users = User.search(params[:search]).order('created_at DESC')
end
User.delay.export_users_to_xlsx(@users, current_user)
redirect_to action: :index
end
用户模型:
def self.export_users_to_xlsx(users, user)
file = User.create_excel(users)
s3_upload = FileManager.upload_to_s3(file)
link = s3_upload[:file].presigned_url(:get, expires_in: 10.days) if s3_upload
end
def self.create_excel(users)
Spreadsheet.client_encoding = 'UTF-8'
filename = "#{Rails.root}/tmp/#{ DateTime.now.strftime("%m%d%Y%H%M%S") }_users.xlsx"
array = [["Name", "Balance","State","Address"]]
users.each do |user|
array.push([user.name,user.balance,user.state,user.address])
end
ExportXls.create_spreadsheet(array,filename)
end
这是用于创建电子表格的lib:
class ExportXls
def self.create_spreadsheet(content,filename)
Spreadsheet.client_encoding = 'UTF-8'
book = Spreadsheet::Workbook.new
sheet1 = book.create_worksheet
content.each_with_index do |row,index|
row.each do |column|
sheet1.row(index).push column
end
end
book.write filename
return filename
end
end
这是将文件上传到S3的方法,也就是在lib:
def self.upload_to_s3(temp_file)
s3 = Aws::S3::Resource.new
begin
obj = s3.bucket(ENV['S3_BUCKET']).object(File.basename(temp_file))
obj.upload_file(temp_file)
File.delete(temp_file) if File.exists?(temp_file)
{ :result => 'success', :file => obj }
rescue Aws::S3::Errors::ServiceError => error
{ :result => 'failed', :message => error.message }
end
end
为什么我不能让延迟的工作得到工作的任何建议?
我正在使用delayed_job_active_record gem!
抱歉我的英语不好!