sidekiq进程中的Rails.cache.read

时间:2015-12-23 11:32:03

标签: ruby-on-rails ruby heroku redis sidekiq

在我的Ruby on Rails应用程序中,我的模型看起来像这样:

class Schema < ActiveRecord::Base
  has_many :schema_items

  def self.from_cache(schema_id)
    schema = Rails.cache.read("schema_#{schema_id}")
    unless schema
      schema = Schema.where(id: schema_id).includes(:schema_items).first
      Rails.cache.write("schema_#{schema_id}", schema) if schema
    end
    schema
  end
end

我在Sidekiq worker调用的其他类中使用此类方法。有时它会返回以下错误:

NotImplementedError

使用谷歌我在redis_store gem中发现了这个问题:https://github.com/redis-store/redis-store/issues/74

但他们修复了这个错误。我的应用程序托管在Heroku上,我使用的是RedisCloud。任何想法如何解决这个问题?

编辑:

以下是完整的回溯:

NotImplementedError: NotImplementedError
  from celluloid/tasks/task_fiber.rb:15:in `block in create'
  from celluloid/tasks.rb:57:in `block in initialize'
  from celluloid/actor.rb:357:in `block in task'
  from celluloid/cell.rb:71:in `block in task'
  from celluloid/cell.rb:60:in `block in invoke'
  from celluloid/calls.rb:122:in `dispatch'
  from celluloid/calls.rb:26:in `dispatch'
  from celluloid/calls.rb:26:in `public_send'
  from sidekiq/processor.rb:50:in `process'
  from sidekiq/processor.rb:98:in `stats'
  from sidekiq/processor.rb:51:in `block in process'
  from sidekiq/middleware/chain.rb:132:in `invoke'
  from sidekiq/middleware/chain.rb:132:in `call'
  from sidekiq/middleware/chain.rb:129:in `block in invoke'
  from sidekiq/middleware/server/logging.rb:11:in `call'
  from sidekiq/logging.rb:30:in `with_context'
  from sidekiq/middleware/server/logging.rb:15:in `block in call'
  from sidekiq/middleware/chain.rb:129:in `block in invoke'
  from sidekiq/failures/middleware.rb:9:in `call'
  from sidekiq/middleware/chain.rb:129:in `block in invoke'
  from sidekiq/middleware/server/retry_jobs.rb:74:in `call'
  from sidekiq/middleware/chain.rb:129:in `block in invoke'
  from sidekiq/middleware/server/active_record.rb:6:in `call'
  from sidekiq/middleware/chain.rb:129:in `block in invoke'
  from sidekiq/batch/middleware.rb:25:in `call'
  from sidekiq/middleware/chain.rb:129:in `block in invoke'
  from sidekiq_unique_jobs/middleware/server/unique_jobs.rb:16:in `call'
  from sidekiq/middleware/chain.rb:129:in `block in invoke'
  from sidetiq/middleware/history.rb:8:in `call'
  from sidekiq/middleware/chain.rb:129:in `block in invoke'
  from new_relic/agent/instrumentation/sidekiq.rb:29:in `call'
  from new_relic/agent/instrumentation/controller_instrumentation.rb:352:in `perform_action_with_newrelic_trace'
  from new_relic/agent/instrumentation/sidekiq.rb:33:in `block in call'
  from sidekiq/middleware/chain.rb:127:in `block in invoke'
  from sidekiq/processor.rb:52:in `block (2 levels) in process'
  from sidekiq/processor.rb:75:in `execute_job'
  from app/workers/response_processor_worker.rb:8:in `perform'
  from app/services/import/response_processor.rb:28:in `process'
  from app/models/import/importer/raw_response_validator.rb:17:in `validate'
  from app/models/survey_schema.rb:51:in `from_cache'
  from active_record/relation/finder_methods.rb:127:in `first'
  from active_record/relation/finder_methods.rb:484:in `find_nth'
  from active_record/relation/finder_methods.rb:500:in `find_nth_with_limit'
  from active_record/relation.rb:243:in `to_a'
  from active_record/relation.rb:514:in `load'
  from active_record/relation.rb:643:in `exec_queries'
  from active_record/relation.rb:643:in `each'
  from active_record/relation.rb:644:in `block in exec_queries'
  from active_record/associations/preloader.rb:102:in `preload'
  from active_record/associations/preloader.rb:102:in `flat_map'
  from active_record/associations/preloader.rb:102:in `each'
  from active_record/associations/preloader.rb:103:in `block in preload'
  from active_record/associations/preloader.rb:115:in `preloaders_on'
  from active_record/associations/preloader.rb:143:in `preloaders_for_one'
  from active_record/associations/preloader.rb:143:in `flat_map'
  from active_record/associations/preloader.rb:143:in `each'
  from active_record/associations/preloader.rb:144:in `block in preloaders_for_one'
  from active_record/associations/preloader.rb:144:in `map'
  from active_record/associations/preloader.rb:144:in `each'
  from active_record/associations/preloader.rb:146:in `block (2 levels) in preloaders_for_one'
  from active_record/associations/preloader/association.rb:20:in `run'
  from active_record/associations/preloader/collection_association.rb:13:in `preload'
  from active_record/associations/preloader/association.rb:78:in `associated_records_by_owner'
  from active_record/associations/preloader/association.rb:60:in `owners_by_key'
  from active_record/associations/preloader/association.rb:103:in `key_conversion_required?'
  from active_record/associations/preloader/association.rb:107:in `association_key_type'
  from active_record/associations/preloader/association.rb:45:in `association_key_name'

我的工人看起来像这样:

class ResponseCreatorWorker
  include Sidekiq::Worker

  sidekiq_options queue: :response_processing

  def perform(schema_id)
    # some not important code
    schema = Schema.from_cache(schema_id) # this line throws the error
    Response.create(schema: schema)
  end
end

1 个答案:

答案 0 :(得分:1)

一些选项无法进入专业版代码:

您发现的错误与redis-store相关,请尝试memcached

尝试将缓存移动到memcached,它应该是一个简单且无中断的操作,在简单的计划上没有成本,并且看看问题是否消失,或者更改是否有助于解决核心错误

对于简单的键值内容,memcache无论如何都是一个更好的缓存服务,

您链接的工作人员不是堆栈跟踪的一部分

您展示了ResponseCreatorWorker,但这并未显示在堆栈跟踪中。出现的是ResponseProcessorWorker,可能会链接该工作人员吗?

您确定要运行正在运行的代码吗?

Mike Perham说Sidekiq回溯与你给他的版本不匹配,错误的工人在堆栈跟踪中?你真的确定服务器正在更新吗?查看部署日志并使用heroku run bash登录到工作人员并检查几个关键文件,确保平台没有任何关闭。

你能救它吗? :d

应该抓住NotImplementedError进行抢救,再次尝试并将其记录记录到日志中应该是非常安全的。适当时更新宝石,当警告消失时,打开威士忌并庆祝。

在你的模型中加入这样的东西:

def wrap_me_scotty
  yield
rescue NotImplementedError => e
  warn "#{e} :-("
  yield # try once more, let it explode if no joy
end

然后换Rails.cache次来电

def self.from_cache(schema_id)
  schema = wrap_me_scotty{ Rails.cache.read("schema_#{schema_id}") }
  unless schema
    schema = Schema.where(id: schema_id).includes(:schema_items).first
    wrap_me_scotty{ Rails.cache.write("schema_#{schema_id}", schema) } if schema
  end
  schema
end