2

rake apn:notifications:deliver在我安装的APN on Rails gem上触发命令后,我正在努力解决这个错误。

它在咆哮 RAILS_ENV 变量。我已经尝试了几个将 RAILS_ENV 更改为 Rails.env 的分支,但我仍然遇到相同的错误。我已经在那个 repo上发布了我的问题,希望我能找到某个地方。

我对铁轨的了解还不够,无法进一步挖掘。我希望有人能指出我正确的方向,以便我可以 a)更好地了解问题所在 b)解决问题。

我正在使用捆绑器,并将我的 gem 文件指向 git repo:rake apn:notifications:deliver

air:apnapp azcoov$ rake apn:notifications:deliver --trace
    /Users/azcoov/.bundler/ruby/1.8/apn_on_rails-ca98c7c130f0/lib/apn_on_rails/version.rb:2: warning: already initialized constant VERSION
    ** Invoke apn:notifications:deliver (first_time)
    ** Invoke environment (first_time)
    ** Execute environment
    ** Execute apn:notifications:deliver
    rake aborted!
    uninitialized constant APN::App::RAILS_ENV
    /Users/azcoov/.bundler/ruby/1.8/apn_on_rails-ca98c7c130f0/lib/apn_on_rails/app/models/apn/app.rb:11:in `cert'
    /Users/azcoov/.bundler/ruby/1.8/apn_on_rails-ca98c7c130f0/lib/apn_on_rails/app/models/apn/app.rb:22:in `send_notifications'
    /Users/azcoov/.bundler/ruby/1.8/apn_on_rails-ca98c7c130f0/lib/apn_on_rails/app/models/apn/app.rb:32:in `send_notifications'
    /Users/azcoov/.bundler/ruby/1.8/apn_on_rails-ca98c7c130f0/lib/apn_on_rails/app/models/apn/app.rb:31:in `each'
    /Users/azcoov/.bundler/ruby/1.8/apn_on_rails-ca98c7c130f0/lib/apn_on_rails/app/models/apn/app.rb:31:in `send_notifications'
    /Users/azcoov/.bundler/ruby/1.8/apn_on_rails-ca98c7c130f0/lib/apn_on_rails/rails/../tasks/apn.rake:7
    /Library/Ruby/Gems/1.8/gems/rake-0.9.2.2/lib/rake/task.rb:205:in `call'
    /Library/Ruby/Gems/1.8/gems/rake-0.9.2.2/lib/rake/task.rb:205:in `execute'
    /Library/Ruby/Gems/1.8/gems/rake-0.9.2.2/lib/rake/task.rb:200:in `each'
    /Library/Ruby/Gems/1.8/gems/rake-0.9.2.2/lib/rake/task.rb:200:in `execute'
    /Library/Ruby/Gems/1.8/gems/rake-0.9.2.2/lib/rake/task.rb:158:in `invoke_with_call_chain'
    /System/Library/Frameworks/Ruby.framework/Versions/1.8/usr/lib/ruby/1.8/monitor.rb:242:in `synchronize'
    /Library/Ruby/Gems/1.8/gems/rake-0.9.2.2/lib/rake/task.rb:151:in `invoke_with_call_chain'
    /Library/Ruby/Gems/1.8/gems/rake-0.9.2.2/lib/rake/task.rb:144:in `invoke'
    /Library/Ruby/Gems/1.8/gems/rake-0.9.2.2/lib/rake/application.rb:116:in `invoke_task'
    /Library/Ruby/Gems/1.8/gems/rake-0.9.2.2/lib/rake/application.rb:94:in `top_level'
    /Library/Ruby/Gems/1.8/gems/rake-0.9.2.2/lib/rake/application.rb:94:in `each'
    /Library/Ruby/Gems/1.8/gems/rake-0.9.2.2/lib/rake/application.rb:94:in `top_level'
    /Library/Ruby/Gems/1.8/gems/rake-0.9.2.2/lib/rake/application.rb:133:in `standard_exception_handling'
    /Library/Ruby/Gems/1.8/gems/rake-0.9.2.2/lib/rake/application.rb:88:in `top_level'
    /Library/Ruby/Gems/1.8/gems/rake-0.9.2.2/lib/rake/application.rb:66:in `run'
    /Library/Ruby/Gems/1.8/gems/rake-0.9.2.2/lib/rake/application.rb:133:in `standard_exception_handling'
    /Library/Ruby/Gems/1.8/gems/rake-0.9.2.2/lib/rake/application.rb:63:in `run'
    /Library/Ruby/Gems/1.8/gems/rake-0.9.2.2/bin/rake:33
    /usr/bin/rake:19:in `load'
    /usr/bin/rake:19
    Tasks: TOP => apn:notifications:deliver
4

1 回答 1

2

您使用哪个版本的 Rails?我也遇到了这个问题。我的 Rails 知识不是很深,但问题似乎是 RAILS_* 环境变量的弃用。我使用 Rails 3.1.0,但没有:

RAILS_ROOT/ENV/blah

在我的环境中。相反,我有:

Rails.root/env/blah

当我在 app/models/apn/app.rb 中用 Rails.env 替换 RAILS_ENV 时,问题就消失了。

那么,这个 gem 是否与 Rails 3.1.0 兼容?似乎没有。是否有允许其在生产环境中使用的解决方法?也许在某处定义:

APN::App::RAILS_ENV = Rails.env

但是哪里?

更新:

我在这里使用了 garyfoster 描述的解决方法:https ://github.com/PRX/apn_on_rails/issues/53 。

总结起来就是三点:

1) 将此 ghetto hack 添加到您的 config/environment.rb 文件中:

APN::App::RAILS_ENV = Rails.env

2) 在 APN::App 表中插入您的证书。我为此写了一个一次性的 rake 任务:

task :init_apn_certs => [:environment] do # NOTE: One-time task...
  print "Creating APN::App with certs..."
  app = APN::App.create(:apn_dev_cert => Rails.root.join('config', 'apple_push_notification_development.pem').read,
                        :apn_prod_cert => Rails.root.join('config', 'apple_push_notification_production.pem').read)
  puts (app.valid? ? "done" : "failed")
end

3) 编写您自己的绕过 APN::App.send_notifications 方法的 rake 任务,因为这会引发另一个错误(关于 '/config/apple_push_notification_development.pem' 的内容不存在)。我的代码:

task :deliver_notifications => [:environment] do
  print "Delivering APNs for app ids..."
  apps = APN::App.all
  apps.each do |app|
    app.send_notifications
    print app.id.to_s + "..."
  end
  puts "done."
end

或者,如果您只有一个应用程序,您可以这样做并完成它:

task :deliver_notifications => [:environment] do
  APN::App.first.send_notifications
end

免责声明:我尚未在 Staging 和 Production 上对此进行测试(将在本周末进行),但我认为它没有理由不工作。

于 2012-07-25T01:20:47.340 回答