使用 axlsx 在延迟作业方法中创建 excel

Create an excel in delayed job method using axlsx

我正在尝试在模型中的延迟作业方法中生成一个 excel 文件。这在当地运作良好。我在 heroku 中使用调度程序 运行 延迟作业。作业正在成功完成而没有生成 excel。

我的延迟工作方法如下:

def self.generate_excel     
Axlsx::Package.new do |p|
    p.workbook.add_worksheet(:name => "Stock Details") do |sheet|
       sheet.add_row ["S.No",  "ProductId", "Title"]
       products.each_with_index do |prods, index|
       sheet.add_row ["1", "1234", "product title"] 
               end
            end 
  p.serialize("#{Rails.root}/app/views/stock_details/stock_details.xlsx")
end

我正在使用 delayedjob 4.1。

正如@Зелёный 已经回答的那样,您在 Heroku 上的 dyno 将有一个 "read-only" 文件系统。从某种意义上说,您的文件不会在您的 dyno 重新启动之间持续存在,并且不能保证它们会在任何两个请求之间持续存在。这是一个 excerpt from the docs:

Each dyno has its own ephemeral file system, not shared with any other dyno, that is discarded as soon as you disconnect. This file system is populated with the slug archive so one-off dynos can make full use of anything deployed in the application.

您可以使用#{Rails.root}/tmp 文件夹作为临时文件夹,但您需要将文件上传到一些外部存储(S3、一些CDN 等)。 Heroku 有一些插件,使其易于处理。