###前提・実現したいこと
railsでsidekiqを用いた非同期処理を実装しようと思っています。
クラス・キューをユーザーごとに動的生成を行い、非同期処理をユーザーごとに任意で実行・停止できるようにしたいのですが、下記エラーでキューが実行されません。
(Foo::Baz_User*というクラス、:work-*というキューを生成していきたいです。)
今回のような場合、関数で生成するためスコープの兼ね合いなどで参照できないということでしょうか?
もし対処方法があるようでしたら教えていただきたいと思います。よろしくお願いします。
###発生している問題・エラーメッセージ
キューの登録までは実行できるのですが、いざsidekiqがキューを実行する際に、下記のエラーが発生します。
2017-09-12T03:38:47.079Z 60023 TID-ow8lyypd4 WARN: NameError: uninitialized constant Foo::Baz_User1 2017-09-12T03:38:47.080Z 60023 TID-ow8lyypd4 WARN: ~/.rbenv/versions/2.2.7/lib/ruby/gems/2.2.0/gems/activesupport-5.0.6/lib/active_support/inflector/methods.rb:270:in `const_get' ~/.rbenv/versions/2.2.7/lib/ruby/gems/2.2.0/gems/activesupport-5.0.6/lib/active_support/inflector/methods.rb:270:in `block in constantize' ~/.rbenv/versions/2.2.7/lib/ruby/gems/2.2.0/gems/activesupport-5.0.6/lib/active_support/inflector/methods.rb:266:in `each' ~/.rbenv/versions/2.2.7/lib/ruby/gems/2.2.0/gems/activesupport-5.0.6/lib/active_support/inflector/methods.rb:266:in `inject' ~/.rbenv/versions/2.2.7/lib/ruby/gems/2.2.0/gems/activesupport-5.0.6/lib/active_support/inflector/methods.rb:266:in `constantize' ~/.rbenv/versions/2.2.7/lib/ruby/gems/2.2.0/gems/activesupport-5.0.6/lib/active_support/core_ext/string/inflections.rb:66:in `constantize' ~/.rbenv/versions/2.2.7/lib/ruby/gems/2.2.0/gems/sidekiq-3.4.1/lib/sidekiq/processor.rb:46:in `process' ~/.rbenv/versions/2.2.7/lib/ruby/gems/2.2.0/gems/celluloid-0.16.0/lib/celluloid/calls.rb:26:in `public_send' ~/.rbenv/versions/2.2.7/lib/ruby/gems/2.2.0/gems/celluloid-0.16.0/lib/celluloid/calls.rb:26:in `dispatch' ~/.rbenv/versions/2.2.7/lib/ruby/gems/2.2.0/gems/celluloid-0.16.0/lib/celluloid/calls.rb:122:in `dispatch' ~/.rbenv/versions/2.2.7/lib/ruby/gems/2.2.0/gems/celluloid-0.16.0/lib/celluloid/cell.rb:60:in `block in invoke' ~/.rbenv/versions/2.2.7/lib/ruby/gems/2.2.0/gems/celluloid-0.16.0/lib/celluloid/cell.rb:71:in `block in task' ~/.rbenv/versions/2.2.7/lib/ruby/gems/2.2.0/gems/celluloid-0.16.0/lib/celluloid/actor.rb:357:in `block in task' ~/.rbenv/versions/2.2.7/lib/ruby/gems/2.2.0/gems/celluloid-0.16.0/lib/celluloid/tasks.rb:57:in `block in initialize' ~/.rbenv/versions/2.2.7/lib/ruby/gems/2.2.0/gems/celluloid-0.16.0/lib/celluloid/tasks/task_fiber.rb:15:in `block in create'
###該当のソースコード
workerファイル
ruby
1module Foo 2 class Bar 3 include Sidekiq::Worker 4 5 def self.id_init(user_id) 6 klass_name = "#{self.name.split('::').last}_User#{user_id}" 7 unless Foo.const_defined?(klass_name) 8 Foo.const_set(klass_name.to_sym, self.clone) 9 klass = Foo.const_get(klass_name.to_sym) 10 queue = klass.instance_variable_get(:@queue).to_s + "-#{user_id}" 11 klass.instance_variable_set(:@queue, queue) 12 end 13 Foo.const_get(klass_name.to_sym) 14 end 15 end 16 17 class Baz < Bar 18 @queue = :work 19 20 def perform(user_id) 21 #ループ処理 22 end 23 end 24end
controllerからキューの登録
ruby
1def start 2user = current_user 3Sidekiq::Client.enqueue(Foo::Baz.id_init(user.id),user.id) 4end
最終的に呼び出されるconstanizeメソッド
ruby
1def constantize(camel_cased_word) 2 names = camel_cased_word.split('::'.freeze) 3 4 # Trigger a built-in NameError exception including the ill-formed constant in the message. 5 Object.const_get(camel_cased_word) if names.empty? 6 7 # Remove the first blank element in case of '::ClassName' notation. 8 names.shift if names.size > 1 && names.first.empty? 9 10 names.inject(Object) do |constant, name| 11 if constant == Object 12 constant.const_get(name) 13 else 14 candidate = constant.const_get(name) 15 next candidate if constant.const_defined?(name, false) 16 next candidate unless Object.const_defined?(name) 17 18 # Go down the ancestors to check if it is owned directly. The check 19 # stops when we reach Object or the end of ancestors tree. 20 constant = constant.ancestors.inject(constant) do |const, ancestor| 21 break const if ancestor == Object 22 break ancestor if ancestor.const_defined?(name, false) 23 const 24 end 25 26 # owner is in Object, so raise 27 constant.const_get(name, false) 28 end 29 end 30 end
###試したこと
http://tech.vasily.jp/entry/resque_to_sidekiq
上記サイトを参考にさせていただきました。
###補足情報(言語/FW/ツール等のバージョンなど)
Rails 5.0.6
ruby 2.2.7p470
sidekiq 3.4.1
あなたの回答
tips
プレビュー