401 unauthorized errors on Heroku with a delay in operation

We created and deployed the application on Heroku, and some processes in this application started a timeout, and I urgently need to move them to the background process.

I did this using delayed_job and carefully followed the steps in the Heroku documentation on how to set this up. Locally this works fine.

In production, however, creating a slow-motion job throws 401. This error occurs outside our application, that is, not in the code that we wrote. This error appears to be due to the large number of Heroku internal components. I am going to insert the parts of the exception notification message that we receive when these types of errors occur.

A Heroku::API::Errors::Unauthorized occurred in surveys#export: Expected(200) <=> Actual(401 Unauthorized) request => {:chunk_size=>1048576, :connect_timeout=>60, :headers=>{"Accept"=>"application/json", "Accept-Encoding"=>"gzip", "Authorization"=>"Basic Og==", "User-Agent"=>"heroku-rb/0.3.5", "X-Ruby-Version"=>"1.9.3", "X-Ruby-Platform"=>"x86_64-linux", "Host"=>"api.heroku.com:443"}, :instrumentor_name=>"excon", :mock=>false, :nonblock=>false, :read_timeout=>60, :retry_limit=>4, :ssl_ca_file=>"/app/vendor/bundle/ruby/1.9.1/gems/excon-0.16.4/data/cacert.pem", :ssl_verify_peer=>true, :write_timeout=>60, :host=>"api.heroku.com", :path=>"/apps/msu/ps", :port=>"443", :query=>nil, :scheme=>"https", :expects=>200, :method=>:get} response => #<Excon::Response:0x0000000957d7e0 @body="{\"error\":\"Access denied\"}", @headers={"Cache-Control"=>"no-cache", "Content-Type"=>"application/json; charset=utf-8", "Date"=>"Tue, 13 Nov 2012 17:00:05 GMT", "Server"=>"nginx/1.2.3", "Status"=>"401 Unauthorized", "Strict-Transport-Security"=>"max-age=500", "X-Runtime"=>"11", "Content-Length"=>"25", "Connection"=>"keep-alive"}, @status=401> vendor/bundle/ruby/1.9.1/gems/excon-0.16.4/lib/excon/connection.rb:290:in `request_kernel' 

Request:

  • URL: https://example.com/surveys/5/export
  • IP Address: REDACTED
  • Parameters: {"action" => "export", "controller" => "forge / survey", "id" => "5"}
  • Rails root: / app
  • Timestamp: 2012-11-13 12:00:06 -0500

Backtrace:

 vendor/bundle/ruby/1.9.1/gems/excon-0.16.4/lib/excon/connection.rb:290:in `request_kernel' vendor/bundle/ruby/1.9.1/gems/excon-0.16.4/lib/excon/connection.rb:101:in `request' vendor/bundle/ruby/1.9.1/gems/heroku-api-0.3.5/lib/heroku/api.rb:62:in `request' vendor/bundle/ruby/1.9.1/gems/heroku-api-0.3.5/lib/heroku/api/processes.rb:9:in `get_ps' vendor/bundle/ruby/1.9.1/gems/workless-1.1.0/lib/workless/scalers/heroku_cedar.rb:18:in `workers' vendor/bundle/ruby/1.9.1/gems/workless-1.1.0/lib/workless/scalers/heroku_cedar.rb:10:in `up' vendor/bundle/ruby/1.9.1/gems/activesupport-3.2.6/lib/active_support/callbacks.rb:407:in `_run__4556705234962331285__create__3481342325198152291__callbacks' vendor/bundle/ruby/1.9.1/gems/activesupport-3.2.6/lib/active_support/callbacks.rb:405:in `__run_callback' vendor/bundle/ruby/1.9.1/gems/activesupport-3.2.6/lib/active_support/callbacks.rb:385:in `_run_create_callbacks' vendor/bundle/ruby/1.9.1/gems/activesupport-3.2.6/lib/active_support/callbacks.rb:81:in `run_callbacks' vendor/bundle/ruby/1.9.1/gems/activerecord-3.2.6/lib/active_record/callbacks.rb:268:in `create' vendor/bundle/ruby/1.9.1/gems/activerecord-3.2.6/lib/active_record/persistence.rb:344:in `create_or_update' vendor/bundle/ruby/1.9.1/gems/activerecord-3.2.6/lib/active_record/callbacks.rb:264:in `block in create_or_update' 

I removed the rest of the backtrace. Nothing in the backtrace refers to anything in our application.

Any help would be appreciated.

Update

Here is the code to export polls - pretty simple!

 def export @survey.delay.export(current_user.email) flash[:notice] = "Your survey exports have been queued for processing and will be emailed to #{current_user.email} when complete." redirect_to forge_surveys_path end 
+7
source share
1 answer

Well, I realized this, of course, by looking at the source of the various libraries that were called in backtrace.

The most important line:

 vendor/bundle/ruby/1.9.1/gems/heroku-api-0.3.5/lib/heroku/api/processes.rb:9:in `get_ps' 

It uses heroin api stone to get some workflows. When viewing the source of the hero-api-gem, I see that when you make a request through the API, it initializes the connection as follows:

 @api_key = options.delete(:api_key) || ENV['HEROKU_API_KEY'] if !@api _key && options.has_key?(:username) && options.has_key?(:password) @connection = Excon.new("#{options[:scheme]}://#{options[:host]}", options.merge(:headers => HEADERS)) @api_key = self.post_login(options[:username], options[:password]).body["api_key"] end 

There was no ENV['HEROKU_API_KEY'] set in my environment ENV['HEROKU_API_KEY'] . I had a HEROKU_PASSWORD set that contains the same data, but this is not what this pearl is looking for. Thus, I was getting 401 errors.

I will send an update to Heroku documentation and ask to include this as one of the steps, since it is not there.

To fix this, I simply did:

 heroku config:add HEROKU_API_KEY=KEY 

Where KEY matches the value for HEROKU_PASSWORD. (You can view all vars configs using heroku config ).

+9
source

All Articles