2012-03-28 47 views
3

我正在寫一個應用程序,因爲這些電子郵件需要發送很多電子郵件並創建許多用戶通知。此任務在Heroku中產生超時。爲了解決這個問題,我決定使用Resque和RedistToGo。Resque with Rails 3 + Heroku + RedisToGo給Postgresql錯誤

我所做的就是發送電子郵件(實際上只是一封電子郵件,因爲我們使用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:[email protected]_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 
    ["[email protected]", "[email protected]"] 

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,而第二個是所有收件人列表...正是因爲它應該是。

任何人都可以幫助我嗎?謝謝!

+0

我有完全相同的問題。你設法解決它嗎? – Arkan 2012-04-01 00:37:38

+2

我解決了我的問題,你可以看看我的帖子找出我的解決方案,http://stackoverflow.com/questions/9961044/postgres-error-on-heroku-with-resque/9964924#9964924 – Arkan 2012-04-01 13:58:44

+0

謝謝,阿爾坎,那工作 – sauronnikko 2012-04-03 16:13:52

回答

4

我遇到了同樣的問題。假設您使用的是Active Record,則必須爲每個分叉的Resque worker調用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