Resque with Rails3+Heroku+RedisToGo给出Postgresql错误



我正在编写一个应用程序,它需要发送许多电子邮件,并因为这些电子邮件而创建许多用户通知。此任务在Heroku中产生超时。为了解决这个问题,我决定使用Resque和RedistToGo。

我所做的是发送电子邮件(实际上只是一封电子邮件,因为我们使用Sendgrid来处理这件事),并使用Resque工作人员创建通知。电子邮件已经创建,所以我将其id和所有收件人一起发送给工作人员。

这在本地运行良好。在生产中,除非我们在Heroku中重新启动应用程序,否则它只能工作一次。我将发布一些我的代码和错误消息:

#lib/tasks/resque.rake
require 'resque/tasks'
task "resque:setup" => :environment do
  ENV['QUEUE'] = '*'
end
desc "Alias for resque:work (To run workers on Heroku)"
task "jobs:work" => "resque:work"

#config/initalizers/resque.rb
ENV["REDISTOGO_URL"] ||= "redis://redistogo:some_hash@some_url:some_number/"
uri = URI.parse(ENV["REDISTOGO_URL"])
Resque.redis = Redis.new(:host => uri.host, :port => uri.port, :password => uri.password)
Dir["#{Rails.root}/app/workers/*.rb"].each { |file| require file }

#app/workers/massive_email_sender.rb
class MassiveEmailSender 
  @queue = :massive_email_queue
  def self.perform(email_id, recipients)
    email = Email.find(email_id.to_i)
    email.recipients = recipients
    email.send_email
  end
end

我有一个电子邮件模型,它有一个after_create,可以让工作人员排队:

class Email < ActiveRecord::Base
...
after_create :enqueue_email
  def enqueue_email
    Resque.enqueue(MassiveEmailSender, self.id, self.recipients)
  end
...
end

这个电子邮件模型也有send_email方法,它执行我在之前所说的

我收到以下错误消息。我将发布Resque给我的所有信息:

Worker
    9dddd06a-2158-464a-b3d9-b2d16380afcf:1 on massive_email_queue at just now
    Retry or Remove
Class
    MassiveEmailSender
Arguments
    21
    ["some_email_1@gmail.com", "some_email_2@gmail.com"]
Exception
    ActiveRecord::StatementInvalid
Error
    PG::Error: SSL error: decryption failed or bad record mac : SELECT a.attname, format_type(a.atttypid, a.atttypmod), d.adsrc, a.attnotnull FROM pg_attribute a LEFT JOIN pg_attrdef d ON a.attrelid = d.adrelid AND a.attnum = d.adnum WHERE a.attrelid = '"emails"'::regclass AND a.attnum > 0 AND NOT a.attisdropped ORDER BY a.attnum
    /app/vendor/bundle/ruby/1.9.1/gems/activerecord-3.2.2/lib/active_record/connection_adapters/postgresql_adapter.rb:1139:in `async_exec'
    /app/vendor/bundle/ruby/1.9.1/gems/activerecord-3.2.2/lib/active_record/connection_adapters/postgresql_adapter.rb:1139:in `exec_no_cache'
    /app/vendor/bundle/ruby/1.9.1/gems/activerecord-3.2.2/lib/active_record/connection_adapters/postgresql_adapter.rb:663:in `block in exec_query'
    /app/vendor/bundle/ruby/1.9.1/gems/activerecord-3.2.2/lib/active_record/connection_adapters/abstract_adapter.rb:280:in `block in log'
    /app/vendor/bundle/ruby/1.9.1/gems/activesupport-3.2.2/lib/active_support/notifications/instrumenter.rb:20:in `instrument'
    /app/vendor/bundle/ruby/1.9.1/gems/activerecord-3.2.2/lib/active_record/connection_adapters/abstract_adapter.rb:275:in `log'
    /app/vendor/bundle/ruby/1.9.1/gems/newrelic_rpm-3.3.2/lib/new_relic/agent/instrumentation/active_record.rb:31:in `block in log_with_newrelic_instrumentation'
    /app/vendor/bundle/ruby/1.9.1/gems/newrelic_rpm-3.3.2/lib/new_relic/agent/method_tracer.rb:242:in `trace_execution_scoped'
    /app/vendor/bundle/ruby/1.9.1/gems/newrelic_rpm-3.3.2/lib/new_relic/agent/instrumentation/active_record.rb:28:in `log_with_newrelic_instrumentation'
    /app/vendor/bundle/ruby/1.9.1/gems/activerecord-3.2.2/lib/active_record/connection_adapters/postgresql_adapter.rb:662:in `exec_query'
    /app/vendor/bundle/ruby/1.9.1/gems/activerecord-3.2.2/lib/active_record/connection_adapters/postgresql_adapter.rb:1264:in `column_definitions'
    /app/vendor/bundle/ruby/1.9.1/gems/activerecord-3.2.2/lib/active_record/connection_adapters/postgresql_adapter.rb:858:in `columns'
    /app/vendor/bundle/ruby/1.9.1/gems/activerecord-3.2.2/lib/active_record/connection_adapters/schema_cache.rb:12:in `block in initialize'
    /app/vendor/bundle/ruby/1.9.1/gems/activerecord-3.2.2/lib/active_record/model_schema.rb:228:in `yield'
    /app/vendor/bundle/ruby/1.9.1/gems/activerecord-3.2.2/lib/active_record/model_schema.rb:228:in `default'
    /app/vendor/bundle/ruby/1.9.1/gems/activerecord-3.2.2/lib/active_record/model_schema.rb:228:in `columns'
    /app/vendor/bundle/ruby/1.9.1/gems/activerecord-3.2.2/lib/active_record/model_schema.rb:237:in `columns_hash'
    /app/vendor/bundle/ruby/1.9.1/gems/activerecord-3.2.2/lib/active_record/relation/delegation.rb:7:in `columns_hash'
    /app/vendor/bundle/ruby/1.9.1/gems/activerecord-3.2.2/lib/active_record/relation/finder_methods.rb:330:in `find_one'
    /app/vendor/bundle/ruby/1.9.1/gems/activerecord-3.2.2/lib/active_record/relation/finder_methods.rb:311:in `find_with_ids'
    /app/vendor/bundle/ruby/1.9.1/gems/activerecord-3.2.2/lib/active_record/relation/finder_methods.rb:107:in `find'
    /app/vendor/bundle/ruby/1.9.1/gems/activerecord-3.2.2/lib/active_record/querying.rb:5:in `find'
    /app/app/workers/massive_email_sender.rb:5:in `perform'

根据这一点,第一个参数是电子邮件id,第二个参数是所有收件人的列表。。。正如它应该是。

有人能帮我吗?谢谢

我遇到了同样的问题。假设您使用的是Active Record,则必须为每个分叉的Resque工作进程调用ActiveRecord::Base.establish_connection,以确保它没有过时的数据库连接。试着把它放在你的lib/tasks/resque.rake

task "resque:setup" => :environment do
  ENV['QUEUE'] = '*'
  Resque.after_fork = Proc.new { ActiveRecord::Base.establish_connection }
end

最新更新