6

我有一个使用 Gmail 发送电子邮件通知的 Rails 应用程序(目前仅处于开发模式)。我用这样的环境变量设置用户名和密码

GMAIL_USERNAME='me@gmail.com' GMAIL_PASSWORD='mygreatpassword' rails s

一切正常。但是,在我添加 sidekiq/redis 以将电子邮件作为后台作业发送后,当它尝试发送电子邮件时出现身份验证错误。

2013-04-24T19:26:45Z 887 TID-ovdxl6qxs WARN: {"retry"=>true, "queue"=>"default", "timeout"=>30, "class"=>"Sidekiq::Extensions::DelayedMailer", "args"=>["---\n- !ruby/class 'Notifier'\n- :answer_updated\n- - !ruby/object:Answer\n    attributes:\n      id: 32\n      content: Billy\n      accepted: \n      user_id: 37\n      question_id: 38\n      created_at: 2013-04-24 19:26:44.948753000 Z\n      updated_at: 2013-04-24 19:26:44.948753000 Z\n  - !ruby/object:User\n    attributes:\n      id: 35\n      email: emailaddress@gmail.com\n      encrypted_password: $2a$10$5XLIJ6F1KNPOjbTUX3fqROE0oDHgf/WnSGE4OhCm7g.pzN1bzLS0q\n      reset_password_token: \n      reset_password_sent_at: \n      remember_created_at: 2013-04-19 19:28:52.926838000 Z\n      sign_in_count: 9\n      current_sign_in_at: 2013-04-24 19:17:31.694245000 Z\n      last_sign_in_at: 2013-04-22 18:24:29.946303000 Z\n      current_sign_in_ip: 127.0.0.1\n      last_sign_in_ip: 127.0.0.1\n      created_at: 2013-04-18 19:28:09.569895000 Z\n      updated_at: 2013-04-24 19:17:31.695692000 Z\n      name: emailaddress@gmail.com\n      lawyer: \n      student: \n"], "jid"=>"4c9cb74b27080c4df581715c", "error_message"=>"530-5.5.1 Authentication Required. Learn more at\n", "error_class"=>"Net::SMTPAuthenticationError", "failed_at"=>2013-04-24 19:26:45 UTC, "retry_count"=>0}
2013-04-24T19:26:45Z 887 TID-ovdxl6qxs WARN: 530-5.5.1 Authentication Required. Learn more at

添加 sidekiq 后,我以相同的方式启动 rails 服务器,在“rails s”命令之前使用 GMAIL_USERNAME 和 GMAIL_PASSWORD 环境变量。我也是这样启动redis的

redis-server /usr/local/etc/redis.conf

和sidekiq这样

bundle exec sidekiq

然后,添加redis后,我使用sidekiq的'delay'方法而不是'deliver'来发送消息。但是,如上所述,我收到了身份验证错误消息。

    def after_create(answer)
      answer.question.watchers.each do |user|
      Notifier.delay.answer_updated(answer, user)
      # Notifier.answer_updated(answer, user).deliver  
    end 

更新:

这些是我的配置操作邮件设置

config.action_mailer.smtp_settings = {
  address: "smtp.gmail.com",
  port: 587,
  domain: "localhost:3000",
  authentication: "plain",
  enable_starttls_auto: true,
  user_name: ENV["GMAIL_USERNAME"],
  password: ENV["GMAIL_PASSWORD"]
}

这是它在“了解更多”之后所说的

2013-04-25T00:11:30Z 6904 TID-owuofcbno WARN: /Users/me/.rbenv/versions/1.9.3-p327/lib/ruby/1.9.1/net/smtp.rb:948:in `check_response'
/Users/me/.rbenv/versions/1.9.3-p327/lib/ruby/1.9.1/net/smtp.rb:917:in `getok'
/Users/me/.rbenv/versions/1.9.3-p327/lib/ruby/1.9.1/net/smtp.rb:832:in `mailfrom'
/Users/me/.rbenv/versions/1.9.3-p327/lib/ruby/1.9.1/net/smtp.rb:659:in `send_message'
/Users/me/.rbenv/versions/1.9.3-p327/lib/ruby/gems/1.9.1/gems/mail-2.4.4/lib/mail/network/delivery_methods/smtp.rb:145:in `block in deliver!'
/Users/me/.rbenv/versions/1.9.3-p327/lib/ruby/1.9.1/net/smtp.rb:520:in `start'
/Users/me/.rbenv/versions/1.9.3-p327/lib/ruby/gems/1.9.1/gems/mail-2.4.4/lib/mail/network/delivery_methods/smtp.rb:144:in `deliver!'
/Users/me/.rbenv/versions/1.9.3-p327/lib/ruby/gems/1.9.1/gems/mail-2.4.4/lib/mail/message.rb:2034:in `do_delivery'
/Users/me/.rbenv/versions/1.9.3-p327/lib/ruby/gems/1.9.1/gems/mail-2.4.4/lib/mail/message.rb:229:in `block in deliver'
/Users/me/.rbenv/versions/1.9.3-p327/lib/ruby/gems/1.9.1/gems/actionmailer-3.2.12/lib/action_mailer/base.rb:415:in `block in deliver_mail'
/Users/me/.rbenv/versions/1.9.3-p327/lib/ruby/gems/1.9.1/gems/activesupport-3.2.12/lib/active_support/notifications.rb:123:in `block in instrument'
/Users/me/.rbenv/versions/1.9.3-p327/lib/ruby/gems/1.9.1/gems/activesupport-3.2.12/lib/active_support/notifications/instrumenter.rb:20:in `instrument'
/Users/me/.rbenv/versions/1.9.3-p327/lib/ruby/gems/1.9.1/gems/activesupport-3.2.12/lib/active_support/notifications.rb:123:in `instrument'
/Users/me/.rbenv/versions/1.9.3-p327/lib/ruby/gems/1.9.1/gems/actionmailer-3.2.12/lib/action_mailer/base.rb:413:in `deliver_mail'
/Users/me/.rbenv/versions/1.9.3-p327/lib/ruby/gems/1.9.1/gems/mail-2.4.4/lib/mail/message.rb:229:in `deliver'
/Users/me/.rbenv/versions/1.9.3-p327/lib/ruby/gems/1.9.1/gems/sidekiq-2.10.1/lib/sidekiq/extensions/action_mailer.rb:23:in `perform'
/Users/me/.rbenv/versions/1.9.3-p327/lib/ruby/gems/1.9.1/gems/sidekiq-2.10.1/lib/sidekiq/processor.rb:49:in `block (3 levels) in process'
/Users/me/.rbenv/versions/1.9.3-p327/lib/ruby/gems/1.9.1/gems/sidekiq-2.10.1/lib/sidekiq/middleware/chain.rb:109:in `call'
/Users/me/.rbenv/versions/1.9.3-p327/lib/ruby/gems/1.9.1/gems/sidekiq-2.10.1/lib/sidekiq/middleware/chain.rb:109:in `block in invoke'
/Users/me/.rbenv/versions/1.9.3-p327/lib/ruby/gems/1.9.1/gems/sidekiq-2.10.1/lib/sidekiq/middleware/server/timeout.rb:11:in `block in call'
/Users/me/.rbenv/versions/1.9.3-p327/lib/ruby/1.9.1/timeout.rb:68:in `timeout'
/Users/me/.rbenv/versions/1.9.3-p327/lib/ruby/gems/1.9.1/gems/sidekiq-2.10.1/lib/sidekiq/middleware/server/timeout.rb:10:in `call'
/Users/me/.rbenv/versions/1.9.3-p327/lib/ruby/gems/1.9.1/gems/sidekiq-2.10.1/lib/sidekiq/middleware/chain.rb:111:in `block in invoke'
/Users/me/.rbenv/versions/1.9.3-p327/lib/ruby/gems/1.9.1/gems/sidekiq-2.10.1/lib/sidekiq/middleware/server/active_record.rb:6:in `call'
/Users/me/.rbenv/versions/1.9.3-p327/lib/ruby/gems/1.9.1/gems/sidekiq-2.10.1/lib/sidekiq/middleware/chain.rb:111:in `block in invoke'
/Users/me/.rbenv/versions/1.9.3-p327/lib/ruby/gems/1.9.1/gems/sidekiq-2.10.1/lib/sidekiq/middleware/server/retry_jobs.rb:50:in `call'
/Users/me/.rbenv/versions/1.9.3-p327/lib/ruby/gems/1.9.1/gems/sidekiq-2.10.1/lib/sidekiq/middleware/chain.rb:111:in `block in invoke'
/Users/me/.rbenv/versions/1.9.3-p327/lib/ruby/gems/1.9.1/gems/sidekiq-2.10.1/lib/sidekiq/middleware/server/logging.rb:11:in `block in call'
/Users/me/.rbenv/versions/1.9.3-p327/lib/ruby/gems/1.9.1/gems/sidekiq-2.10.1/lib/sidekiq/logging.rb:22:in `with_context'
/Users/me/.rbenv/versions/1.9.3-p327/lib/ruby/gems/1.9.1/gems/sidekiq-2.10.1/lib/sidekiq/middleware/server/logging.rb:7:in `call'
/Users/me/.rbenv/versions/1.9.3-p327/lib/ruby/gems/1.9.1/gems/sidekiq-2.10.1/lib/sidekiq/middleware/chain.rb:111:in `block in invoke'
/Users/me/.rbenv/versions/1.9.3-p327/lib/ruby/gems/1.9.1/gems/sidekiq-2.10.1/lib/sidekiq/middleware/chain.rb:114:in `call'
/Users/me/.rbenv/versions/1.9.3-p327/lib/ruby/gems/1.9.1/gems/sidekiq-2.10.1/lib/sidekiq/middleware/chain.rb:114:in `invoke'
/Users/me/.rbenv/versions/1.9.3-p327/lib/ruby/gems/1.9.1/gems/sidekiq-2.10.1/lib/sidekiq/processor.rb:48:in `block (2 levels) in process'
/Users/me/.rbenv/versions/1.9.3-p327/lib/ruby/gems/1.9.1/gems/sidekiq-2.10.1/lib/sidekiq/processor.rb:87:in `stats'
/Users/me/.rbenv/versions/1.9.3-p327/lib/ruby/gems/1.9.1/gems/sidekiq-2.10.1/lib/sidekiq/processor.rb:47:in `block in process'
/Users/me/.rbenv/versions/1.9.3-p327/lib/ruby/gems/1.9.1/gems/celluloid-0.12.4/lib/celluloid/calls.rb:23:in `call'
/Users/me/.rbenv/versions/1.9.3-p327/lib/ruby/gems/1.9.1/gems/celluloid-0.12.4/lib/celluloid/calls.rb:23:in `public_send'
/Users/me/.rbenv/versions/1.9.3-p327/lib/ruby/gems/1.9.1/gems/celluloid-0.12.4/lib/celluloid/calls.rb:23:in `dispatch'
/Users/me/.rbenv/versions/1.9.3-p327/lib/ruby/gems/1.9.1/gems/celluloid-0.12.4/lib/celluloid/future.rb:18:in `block in initialize'
/Users/me/.rbenv/versions/1.9.3-p327/lib/ruby/gems/1.9.1/gems/celluloid-0.12.4/lib/celluloid/internal_pool.rb:48:in `call'
/Users/me/.rbenv/versions/1.9.3-p327/lib/ruby/gems/1.9.1/gems/celluloid-0.12.4/lib/celluloid/internal_pool.rb:48:in `block in create'
4

2 回答 2

10

Sidekiq 启动了它自己的 rails 环境,并且缺少用于配置 ActionMailer 的环境变量,这就是为什么在使用 sidekiq 时会出现异常,但在 rails 服务器进程本身内发送时不会出现异常。

基本上你只需要使用与服务器相同的变量来启动sidekiq worker:

GMAIL_USERNAME='me@gmail.com' GMAIL_PASSWORD='mygreatpwd' bundle exec sidekiq

尽管这可能会使工作正常,但请考虑使用像工头这样的东西来处理这个问题(尤其是在开发中)。

于 2013-04-27T08:57:37.777 回答
4

我的猜测是,您在控制台中设置的环境变量仅应用于您的 Web 服务器的进程(由 启动rails s)。因此,当 sidekiq 查看邮件配置时,它会从ENV["GMAIL_USERNAME"]and中返回 nil ENV["GMAIL_PASSWORD"]。您需要将这些环境变量放在 sidekiq 进程可以访问它们的地方。

我将在下面列出一些选项(#2 看起来最简单),但是已经有一些资源已经更详细地解释了这一点(并且归功于他们编译这些选项)。查看:

选项

在 bash 中设置环境变量

我不推荐这种方法,但想把它作为下面两个选项的对立面。

export GMAIL_USERNAME="myemail@gmail.com"

~/.bashrc在你的 bash 控制台中运行它会在 ENV中创建一个可访问的值。

但是,此解决方案不是特定于应用程序的(它在您机器上的任何地方都可用),并且它与您的应用程序相距甚远,您可能会发现难以调试问题或忘记删除/更改此私人信息。

费加罗宝石

figaro gem为您提供了一个.gitignored YAML 文件,您可以在其中放置敏感的配置信息。

它还提供了将这些配置变量上传到 heroku 的帮助程序,同时仍将配置排除在您的 git 存储库之外。

从 yml 配置文件中读取

这几乎就是 Figaro gem 正在做的事情,但值得理解。

# 1. create a config file
touch config/gmail_config.yml
# 2. add the new file to .gitignore
# 3. add code to application.rb to load yml file
config.before_configuration do
  env_file = File.join(Rails.root, 'config', 'local_env.yml')
  YAML.load(File.open(env_file)).each do |key, value|
    ENV[key.to_s] = value
  end if File.exists?(env_file)
end

如果文件退出(即您在本地机器上),它将加载文件并设置变量。如果文件不存在,它将不会加载文件,您需要在生产环境中设置变量。

于 2013-04-26T02:35:28.277 回答