带有 ActiveJob 和 Apartment gem 反序列化错误的 ActiveStorage

ActiveStorage with ActiveJob and Apartment gem deserialization error

我正在尝试将公寓 gem 与 activestorage 和 activejob 一起使用,但我 运行 遇到了默认 activestorage 分析任务的错误。

我有一个简单的控制器可以上传和存储文件。 运行 sidekiq 时出现以下错误。基本上,activestorage 中的默认分析作业失败。

WARN: ActiveJob::DeserializationError: Error while trying to deserialize arguments: Couldn't find ActiveStorage::Blob with 'id'=3

完整的错误是

2018-09-10T07:24:41.583Z 6956 TID-gojpxrbzw ActiveStorage::AnalyzeJob JID-9367cb9f539860eb6b372f64 INFO: start
2018-09-10T07:24:41.586Z 6956 TID-gojpxrbzw ActiveStorage::AnalyzeJob JID-9367cb9f539860eb6b372f64 INFO: fail: 0.003 sec
2018-09-10T07:24:41.586Z 6956 TID-gojpxrbzw WARN: {"context":"Job raised exception","job":{"class":"ActiveJob::QueueAdapters::SidekiqAdapter::JobWrapper","wrapped":"ActiveStorage::AnalyzeJob","queue":"default","args":[{"job_class":"ActiveStorage::AnalyzeJob","job_id":"7515b86b-ba9e-4d7e-8aaf-37dec34252be","provider_job_id":null,"queue_name":"default","priority":null,"arguments":[{"_aj_globalid":"gid://importer-v2/ActiveStorage::Blob/3"}],"executions":0,"locale":"en"}],"retry":true,"jid":"9367cb9f539860eb6b372f64","created_at":1536563963.822738,"enqueued_at":1536564281.5834057,"error_message":"Error while trying to deserialize arguments: Couldn't find ActiveStorage::Blob with 'id'=3","error_class":"ActiveJob::DeserializationError","failed_at":1536563971.316094,"retry_count":3,"retried_at":1536564147.3845608},"jobstr":"{\"class\":\"ActiveJob::QueueAdapters::SidekiqAdapter::JobWrapper\",\"wrapped\":\"ActiveStorage::AnalyzeJob\",\"queue\":\"default\",\"args\":[{\"job_class\":\"ActiveStorage::AnalyzeJob\",\"job_id\":\"7515b86b-ba9e-4d7e-8aaf-37dec34252be\",\"provider_job_id\":null,\"queue_name\":\"default\",\"priority\":null,\"arguments\":[{\"_aj_globalid\":\"gid://importer-v2/ActiveStorage::Blob/3\"}],\"executions\":0,\"locale\":\"en\"}],\"retry\":true,\"jid\":\"9367cb9f539860eb6b372f64\",\"created_at\":1536563963.822738,\"enqueued_at\":1536564281.5834057,\"error_message\":\"Error while trying to deserialize arguments: Couldn't find ActiveStorage::Blob with 'id'=3\",\"error_class\":\"ActiveJob::DeserializationError\",\"failed_at\":1536563971.316094,\"retry_count\":3,\"retried_at\":1536564147.3845608}"}
2018-09-10T07:24:41.586Z 6956 TID-gojpxrbzw WARN: ActiveJob::DeserializationError: Error while trying to deserialize arguments: Couldn't find ActiveStorage::Blob with 'id'=3
2018-09-10T07:24:41.586Z 6956 TID-gojpxrbzw WARN: /home/ubuntu/.rvm/gems/ruby-2.5.1@importer/gems/activerecord-5.2.0/lib/active_record/relation/finder_methods.rb:346:in `raise_record_not_found_exception!'
/home/ubuntu/.rvm/gems/ruby-2.5.1@importer/gems/activerecord-5.2.0/lib/active_record/relation/finder_methods.rb:454:in `find_one'
/home/ubuntu/.rvm/gems/ruby-2.5.1@importer/gems/activerecord-5.2.0/lib/active_record/relation/finder_methods.rb:433:in `find_with_ids'
/home/ubuntu/.rvm/gems/ruby-2.5.1@importer/gems/activerecord-5.2.0/lib/active_record/relation/finder_methods.rb:69:in `find'
/home/ubuntu/.rvm/gems/ruby-2.5.1@importer/gems/activerecord-5.2.0/lib/active_record/querying.rb:5:in `find'
/home/ubuntu/.rvm/gems/ruby-2.5.1@importer/gems/activerecord-5.2.0/lib/active_record/core.rb:160:in `find'
/home/ubuntu/.rvm/gems/ruby-2.5.1@importer/gems/globalid-0.4.1/lib/global_id/locator.rb:130:in `locate'
/home/ubuntu/.rvm/gems/ruby-2.5.1@importer/gems/globalid-0.4.1/lib/global_id/locator.rb:155:in `block in locate'
/home/ubuntu/.rvm/gems/ruby-2.5.1@importer/gems/globalid-0.4.1/lib/global_id/locator.rb:165:in `block in unscoped'
/home/ubuntu/.rvm/gems/ruby-2.5.1@importer/gems/activerecord-5.2.0/lib/active_record/scoping/default.rb:34:in `block in unscoped'
/home/ubuntu/.rvm/gems/ruby-2.5.1@importer/gems/activerecord-5.2.0/lib/active_record/relation.rb:281:in `scoping'
/home/ubuntu/.rvm/gems/ruby-2.5.1@importer/gems/activerecord-5.2.0/lib/active_record/scoping/default.rb:34:in `unscoped'
/home/ubuntu/.rvm/gems/ruby-2.5.1@importer/gems/globalid-0.4.1/lib/global_id/locator.rb:165:in `unscoped'
/home/ubuntu/.rvm/gems/ruby-2.5.1@importer/gems/globalid-0.4.1/lib/global_id/locator.rb:155:in `locate'
/home/ubuntu/.rvm/gems/ruby-2.5.1@importer/gems/globalid-0.4.1/lib/global_id/locator.rb:17:in `locate'
/home/ubuntu/.rvm/gems/ruby-2.5.1@importer/gems/activejob-5.2.0/lib/active_job/arguments.rb:101:in `deserialize_global_id'
/home/ubuntu/.rvm/gems/ruby-2.5.1@importer/gems/activejob-5.2.0/lib/active_job/arguments.rb:87:in `deserialize_argument'
/home/ubuntu/.rvm/gems/ruby-2.5.1@importer/gems/activejob-5.2.0/lib/active_job/arguments.rb:41:in `block in deserialize'
/home/ubuntu/.rvm/gems/ruby-2.5.1@importer/gems/activejob-5.2.0/lib/active_job/arguments.rb:41:in `map'
/home/ubuntu/.rvm/gems/ruby-2.5.1@importer/gems/activejob-5.2.0/lib/active_job/arguments.rb:41:in `deserialize'
/home/ubuntu/.rvm/gems/ruby-2.5.1@importer/gems/activejob-5.2.0/lib/active_job/core.rb:143:in `deserialize_arguments'
/home/ubuntu/.rvm/gems/ruby-2.5.1@importer/gems/activejob-5.2.0/lib/active_job/core.rb:133:in `deserialize_arguments_if_needed'
/home/ubuntu/.rvm/gems/ruby-2.5.1@importer/gems/activejob-5.2.0/lib/active_job/execution.rb:34:in `perform_now'
/home/ubuntu/.rvm/gems/ruby-2.5.1@importer/gems/activejob-5.2.0/lib/active_job/execution.rb:24:in `block in execute'
/home/ubuntu/.rvm/gems/ruby-2.5.1@importer/gems/activesupport-5.2.0/lib/active_support/callbacks.rb:109:in `block in run_callbacks'
/home/ubuntu/.rvm/gems/ruby-2.5.1@importer/gems/activejob-5.2.0/lib/active_job/railtie.rb:28:in `block (4 levels) in <class:Railtie>'
/home/ubuntu/.rvm/gems/ruby-2.5.1@importer/gems/activesupport-5.2.0/lib/active_support/execution_wrapper.rb:87:in `wrap'
/home/ubuntu/.rvm/gems/ruby-2.5.1@importer/gems/activesupport-5.2.0/lib/active_support/reloader.rb:73:in `block in wrap'
/home/ubuntu/.rvm/gems/ruby-2.5.1@importer/gems/activesupport-5.2.0/lib/active_support/execution_wrapper.rb:83:in `wrap'
/home/ubuntu/.rvm/gems/ruby-2.5.1@importer/gems/activesupport-5.2.0/lib/active_support/reloader.rb:72:in `wrap'
/home/ubuntu/.rvm/gems/ruby-2.5.1@importer/gems/activejob-5.2.0/lib/active_job/railtie.rb:27:in `block (3 levels) in <class:Railtie>'
/home/ubuntu/.rvm/gems/ruby-2.5.1@importer/gems/activesupport-5.2.0/lib/active_support/callbacks.rb:118:in `instance_exec'
/home/ubuntu/.rvm/gems/ruby-2.5.1@importer/gems/activesupport-5.2.0/lib/active_support/callbacks.rb:118:in `block in run_callbacks'
/home/ubuntu/.rvm/gems/ruby-2.5.1@importer/gems/activesupport-5.2.0/lib/active_support/callbacks.rb:136:in `run_callbacks'
/home/ubuntu/.rvm/gems/ruby-2.5.1@importer/gems/activejob-5.2.0/lib/active_job/execution.rb:22:in `execute'
/home/ubuntu/.rvm/gems/ruby-2.5.1@importer/gems/activejob-5.2.0/lib/active_job/queue_adapters/sidekiq_adapter.rb:42:in `perform'
/home/ubuntu/.rvm/gems/ruby-2.5.1@importer/gems/sidekiq-5.2.1/lib/sidekiq/processor.rb:185:in `execute_job'
/home/ubuntu/.rvm/gems/ruby-2.5.1@importer/gems/sidekiq-5.2.1/lib/sidekiq/processor.rb:167:in `block (2 levels) in process'
/home/ubuntu/.rvm/gems/ruby-2.5.1@importer/gems/sidekiq-5.2.1/lib/sidekiq/middleware/chain.rb:128:in `block in invoke'
/home/ubuntu/.rvm/gems/ruby-2.5.1@importer/gems/sidekiq-5.2.1/lib/sidekiq/middleware/chain.rb:133:in `invoke'
/home/ubuntu/.rvm/gems/ruby-2.5.1@importer/gems/sidekiq-5.2.1/lib/sidekiq/processor.rb:166:in `block in process'
/home/ubuntu/.rvm/gems/ruby-2.5.1@importer/gems/sidekiq-5.2.1/lib/sidekiq/processor.rb:137:in `block (6 levels) in dispatch'
/home/ubuntu/.rvm/gems/ruby-2.5.1@importer/gems/sidekiq-5.2.1/lib/sidekiq/job_retry.rb:98:in `local'
/home/ubuntu/.rvm/gems/ruby-2.5.1@importer/gems/sidekiq-5.2.1/lib/sidekiq/processor.rb:136:in `block (5 levels) in dispatch'
/home/ubuntu/.rvm/gems/ruby-2.5.1@importer/gems/sidekiq-5.2.1/lib/sidekiq/rails.rb:42:in `block in call'
/home/ubuntu/.rvm/gems/ruby-2.5.1@importer/gems/activesupport-5.2.0/lib/active_support/execution_wrapper.rb:87:in `wrap'
/home/ubuntu/.rvm/gems/ruby-2.5.1@importer/gems/activesupport-5.2.0/lib/active_support/reloader.rb:73:in `block in wrap'
/home/ubuntu/.rvm/gems/ruby-2.5.1@importer/gems/activesupport-5.2.0/lib/active_support/execution_wrapper.rb:87:in `wrap'
/home/ubuntu/.rvm/gems/ruby-2.5.1@importer/gems/activesupport-5.2.0/lib/active_support/reloader.rb:72:in `wrap'
/home/ubuntu/.rvm/gems/ruby-2.5.1@importer/gems/sidekiq-5.2.1/lib/sidekiq/rails.rb:41:in `call'
/home/ubuntu/.rvm/gems/ruby-2.5.1@importer/gems/sidekiq-5.2.1/lib/sidekiq/processor.rb:132:in `block (4 levels) in dispatch'
/home/ubuntu/.rvm/gems/ruby-2.5.1@importer/gems/sidekiq-5.2.1/lib/sidekiq/processor.rb:217:in `stats'
/home/ubuntu/.rvm/gems/ruby-2.5.1@importer/gems/sidekiq-5.2.1/lib/sidekiq/processor.rb:127:in `block (3 levels) in dispatch'
/home/ubuntu/.rvm/gems/ruby-2.5.1@importer/gems/sidekiq-5.2.1/lib/sidekiq/job_logger.rb:8:in `call'
/home/ubuntu/.rvm/gems/ruby-2.5.1@importer/gems/sidekiq-5.2.1/lib/sidekiq/processor.rb:126:in `block (2 levels) in dispatch'
/home/ubuntu/.rvm/gems/ruby-2.5.1@importer/gems/sidekiq-5.2.1/lib/sidekiq/job_retry.rb:73:in `global'
/home/ubuntu/.rvm/gems/ruby-2.5.1@importer/gems/sidekiq-5.2.1/lib/sidekiq/processor.rb:125:in `block in dispatch'
/home/ubuntu/.rvm/gems/ruby-2.5.1@importer/gems/sidekiq-5.2.1/lib/sidekiq/logging.rb:48:in `with_context'
/home/ubuntu/.rvm/gems/ruby-2.5.1@importer/gems/sidekiq-5.2.1/lib/sidekiq/logging.rb:42:in `with_job_hash_context'
/home/ubuntu/.rvm/gems/ruby-2.5.1@importer/gems/sidekiq-5.2.1/lib/sidekiq/processor.rb:124:in `dispatch'
/home/ubuntu/.rvm/gems/ruby-2.5.1@importer/gems/sidekiq-5.2.1/lib/sidekiq/processor.rb:165:in `process'
/home/ubuntu/.rvm/gems/ruby-2.5.1@importer/gems/sidekiq-5.2.1/lib/sidekiq/processor.rb:83:in `process_one'
/home/ubuntu/.rvm/gems/ruby-2.5.1@importer/gems/sidekiq-5.2.1/lib/sidekiq/processor.rb:71:in `run'
/home/ubuntu/.rvm/gems/ruby-2.5.1@importer/gems/sidekiq-5.2.1/lib/sidekiq/util.rb:16:in `watchdog'
/home/ubuntu/.rvm/gems/ruby-2.5.1@importer/gems/sidekiq-5.2.1/lib/sidekiq/util.rb:25:in `block in safe_thread'

所以我的问题是,当 运行 使用公寓 gem 分析作业时,如何让 activestorage 使用正确的租户?

我找到了解决办法。添加 apartment-sidekiq gem 解决了这个问题。

https://github.com/influitive/apartment-sidekiq