Search code examples
ruby-on-railsherokudelayed-jobaxlsx

Create an excel in delayed job method using axlsx


I am trying to generate an excel file in my delayed job method in model. which is working fine in local. i'm using scheduler to run delayed jobs in heroku. Jobs are getting finished successfully without generating excel.

my delayed job method looks like:

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

I'm using delayedjob 4.1.


Solution

  • As @Зелёный has already answered, your dyno on Heroku would have a "read-only" filesystem. In a sense, your files will not be persisted between your dyno restarts and there is no guarantee that they will persist between any two requests. Here is an 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.

    You can use the #{Rails.root}/tmp folder as temporary folder, but you need to upload your files to some external storage (S3, some CDN, etc.). Heroku has some addons that makes it easy to handle.