title: Rails+resque background job import not adding anything to the database
body: <p>I have an issue with importing a lot of records from a user provided excel file into a database. The logic for this is working fine, and I’m using ActiveRecord-import to cut down on the number of database calls. However, when a file is too large, the processing can take too long and Heroku will return a timeout. Solution: Resque and moving the processing to a background job.</p>
  
  <p>So far, so good. I’ve needed to add CarrierWave to upload the files to S3 because I can’t just hold the file in memory for the background job. The upload portion is also working fine, I created a model for them and am passing the IDs through to the queued job to retrieve the file later as I understand I can’t pass a whole ActiveRecord object through to the job.</p>
  
  <p>I’ve installed Resque and Redis locally, and everything seems to be setup correctly in that regard. I can see the jobs I’m creating being queued and then run without failing. The job seems to run fine, but no records are added to the database. If I run the code from my job line by line in the console, the records are added to the database as I would expect. But when the queued jobs I’m creating run, nothing happens.</p>
  
  <p>I can’t quite work out where the problem might be.</p>
  
  <p>Here’s my upload controller’s create action:</p>
  
  <pre><code>def create
    @upload = Upload.new(upload_params)
    if @upload.save
      Resque.enqueue(ExcelImportJob, @upload.id)
      flash[:info] = 'File uploaded.
          Data will be processed and added to the database.'
      redirect_to root_path
    else
      flash[:warning] = 'Upload failed. Please try again.'
      render :new
    end
  end
  </code></pre>
  
  <p>This is a simplified version of the job with fewer sheet columns for clarity:</p>
  
  <pre><code>class ExcelImportJob &lt; ApplicationJob
    @queue = :default
  
    def perform(upload_id)
      file = Upload.find(upload_id).file.file.file
      data = parse_excel(file)
      if header_matches? data
        # Create a database entry for each row, ignoring the first header row
        # using activerecord-import
        sales = []
        data.drop(1).each_with_index do |row, index|
          sales &lt;&lt; Sale.new(row)
          if index % 2500 == 0
            Sale.import sales
            sales = []
          end
        end
        Sale.import sales
      end
  
      def parse_excel(upload)
        # Open the uploaded excel document
        doc = Creek::Book.new upload
  
        # Map rows to the hash keys from the database
        doc.sheets.first.rows.map do |row|
          { date: row.values[0],
            title: row.values[1],
            author: row.values[2],
            isbn: row.values[3],
            release_date: row.values[5],
            units_sold: row.values[6],
            units_refunded: row.values[7],
            net_units_sold: row.values[8],
            payment_amount: row.values[9],
            payment_amount_currency: row.values[10] }
        end
      end
  
      # Returns true if header matches the expected format
      def header_matches?(data)
        data.first == {:date =&gt; 'Date',
                       :title =&gt; 'Title',
                       :author =&gt; 'Author',
                       :isbn =&gt; 'ISBN',
                       :release_date =&gt; 'Release Date',
                       :units_sold =&gt; 'Units Sold',
                       :units_refunded =&gt; 'Units Refunded',
                       :net_units_sold =&gt; 'Net Units Sold',
                       :payment_amount =&gt; 'Payment Amount',
                       :payment_amount_currency =&gt; 'Payment Amount Currency'}
      end
    end
  end
  </code></pre>
  
  <p>I can probably have some improved logic anyway as right now I’m holding the whole file in memory, but that isn’t the issue I’m having – even with a small file that has only 500 or so rows, the job doesn’t add anything to the database.</p>
  
  <p>Like I said my code worked fine when I wasn’t using a background job, and still works if I run it in the console. But for some reason the job is doing nothing. </p>
  
  <p>This is my first time using Resque so I don’t know if I’m missing something obvious? I did create a worker and as I said it does seem to run the job. Here’s the output from Resque’s verbose formatter:</p>
  
  <pre><code>*** resque-1.27.4: Waiting for default
  *** Checking default
  *** Found job on default
  *** resque-1.27.4: Processing default since 1508342426 [ExcelImportJob]
  *** got: (Job{default} | ExcelImportJob | [15])
  *** Running before_fork hooks with [(Job{default} | ExcelImportJob | [15])]
  *** resque-1.27.4: Forked 63706 at 1508342426
  *** Running after_fork hooks with [(Job{default} | ExcelImportJob | [15])]
  *** done: (Job{default} | ExcelImportJob | [15])
  </code></pre>
  
  <p>In the Resque dashboard the jobs aren’t logged as failed. They get executed and I can see an increment in the ‘processed’ jobs on the stats page. But as I say the DB remains untouched. What’s going on? How can I debug the job more clearly? Is there a way to get into it with Pry?</p>