未完成作业的Heroku上有401个未经授权的错误

时间:2012-11-13 20:00:40

标签: ruby-on-rails heroku delayed-job

我们在Heroku上构建并部署了一个应用程序,此应用程序上的某些进程已经开始超时,我迫切需要将它们移到后台进程。

我使用delayed_job完成了这项工作,并仔细按照Heroku文档中有关如何进行设置的步骤进行操作。在当地,这很好。

然而,在生产中,创建延迟的作业会抛出401.此错误似乎发生在我们的应用程序之外,即不在我们编写的代码中。我几乎完全被这个错误所困扰,因为它似乎处理了很多Heroku内部。我将在发生这些类型的错误时粘贴部分异常通知电子邮件。

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'

请求:

  • 网址:https://example.com/surveys/5/export
  • IP地址:已删除
  • 参数:{“action”=&gt;“export”,“controller”=&gt;“forge / surveys”,“id”=&gt;“5”}
  • Rails root:/ app
  • 时间戳:2012-11-13 12:00:06 -0500

回溯:

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'

我删除了其余的回溯。回溯中没有任何内容引用我们的应用程序中的任何内容。

任何帮助都将不胜感激。

更新

以下是调查导出的代码 - 非常基本!

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

1 个答案:

答案 0 :(得分:9)

好的,我当然想到这一点 - 浏览回溯中调用的各种库的来源。

最相关的一句话:

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

这使用heroku-api gem来获取一些工作进程。浏览heroku-api gem的源代码,我看到当你通过API发出请求时,它会像这样初始化一个连接:

@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

我的环境没有ENV['HEROKU_API_KEY']设置。我确实拥有HEROKU_PASSWORD集,其中包含相同的数据,但这并不是这个宝石所寻求的。因此,我得到了401错误。

我将提交Heroku文档的更新,并要求他们将此作为其中一个步骤,因为它现在不在那里。

要解决这个问题,我只是做了:

heroku config:add HEROKU_API_KEY=KEY

其中KEY与HEROKU_PASSWORD的值相同。 (您可以使用heroku config查看所有配置变量。)