0

We have a Rails application that runs several jobs in the background using Sidekiq. Those jobs will create a processing log and attempt to attach it to a database object using ActiveStorage. The following code illustrates the issue:

class Import < ApplicationRecord
  ...
  has_many_attached :log_files
  ...
end

class ImportJob
  include Sidekiq::Job

  queue_as :default

  def perform(id)
    import = Import.find(id)
    log_file_name = "test_#{DateTime.now.iso8601}.log"
    log_file = Tempfile.new(log_file_name)
    log_file.write 'Testing...'
    import.log_files.attach(io: log_file, filename: log_file_name, content_type: 'text/plain')
  end
end

If this job is run synchronously, the log file is successfully attached to the job's record.

ImportJob.new.perform(123)

If however, the job is run asynchronously, the log file attach fails:

ImportJob.perform_async(123)

The error is ActiveRecord::RecordNotSaved: Failed to replace log_files_attachments because one or more of the new records could not be saved.

This code seems to run fine in development (WLS) and on an AWS EC2 server. But cannot get past this error when running on a Heroku server. Has anyone run into this before and found a solution?

Mike
  • 1,031
  • 14
  • 36

0 Answers0