Apartment and Sidekiq
So Im working with apartment and sidekiq and the job is not switch tenant.
gem 'apartment', '~> 1.0', '>= 1.0.2'
gem 'sidekiq', '~> 4.0', '>= 4.1'
gem 'apartment-sidekiq', '~> 0.2.0'
class MyWorker
include Sidekiq::Worker
def perform(tenant)
puts "------------------------"
puts "------------------------"
puts "Tenant #{tenant}"
puts "Current Tenant #{Apartment::Tenant.current}"
puts "------------------------"
puts "------------------------"
end
end
sidekiq setup:
require 'sidekiq'
require 'autoscaler/sidekiq'
require 'autoscaler/heroku_scaler'
Rails.logger = Sidekiq::Logging.logger
heroku = nil
if ENV['HEROKU_APP']
heroku = Autoscaler::HerokuScaler.new
end
Sidekiq.configure_client do |config|
config.redis = { size: 1, url: ENV['REDIS_URL']}
if heroku
config.client_middleware do |chain|
chain.add Autoscaler::Sidekiq::Client, 'default' => heroku
end
end
end
Sidekiq.configure_server do |config|
config.redis = { size: 27, url: ENV['REDIS_URL']}
database_url = ENV['DATABASE_URL']
if database_url
ENV['DATABASE_URL'] = "#{database_url}?pool=25"
ActiveRecord::Base.establish_connection
end
config.server_middleware do |chain|
if heroku
p "[Sidekiq] Running on Heroku, autoscaler is used"
chain.add(Autoscaler::Sidekiq::Server, heroku, 60) # 60 seconds timeout
else
p "[Sidekiq] Running locally, so autoscaler isn't used"
end
end
end
MyWorker.perform_in(1.seconds, Apartment::Tenant.current)
2016-12-19T15:33:56.927Z 7489 TID-ox4ubmy98 INFO: Booting Sidekiq 4.2.7 with redis options {:size=>27, :url=>"redis://localhost:6379/0"}
11:33:56 worker.1 | "[Sidekiq] Running locally, so autoscaler isn't used"
11:33:58 worker.1 | 2016-12-19T15:33:58.356Z 7489 TID-ox4ubmy98 INFO: Running in ruby 2.3.1p112 (2016-04-26 revision 54768) [x86_64-darwin15]
11:33:58 worker.1 | 2016-12-19T15:33:58.356Z 7489 TID-ox4ubmy98 INFO: See LICENSE and the LGPL-3.0 for licensing details.
11:33:58 worker.1 | 2016-12-19T15:33:58.356Z 7489 TID-ox4ubmy98 INFO: Upgrade to Sidekiq Pro for more features and support: http://sidekiq.org
11:33:58 worker.1 | 2016-12-19T15:33:58.361Z 7489 TID-ox4ubmy98 INFO: Starting processing, hit Ctrl-C to stop
11:34:10 worker.1 | 2016-12-19T15:34:10.773Z 7489 TID-ox4vqdyhs MyWorker JID-882bc1314ea2ee37435e89ce INFO: start
11:34:10 worker.1 | 2016-12-19T15:34:10.775Z 7489 TID-ox4vqe0x0 MyWorker JID-8d1f27f683ff53d80921744f INFO: start
11:34:10 worker.1 | 2016-12-19T15:34:10.798Z 7489 TID-ox4vqdyhs MyWorker JID-882bc1314ea2ee37435e89ce INFO: fail: 0.025 sec
11:34:10 worker.1 | 2016-12-19T15:34:10.798Z 7489 TID-ox4vqdyhs WARN: {"context":"Job raised exception","job":{"class":"MyWorker","args":["companydemo"],"retry":true,"queue":"default","jid":"882bc1314ea2ee37435e89ce","created_at":1482160998.107182,"apartment":"companydemo","enqueued_at":1482161650.772489,"error_message":"One of the following schema(s) is invalid: \"companydemo\" \"public\"","error_class":"Apartment::TenantNotFound","failed_at":1482161016.38253,"retry_count":3,"retried_at":1482161650.79791},"jobstr":"{\"class\":\"MyWorker\",\"args\":[\"companydemo\"],\"retry\":true,\"queue\":\"default\",\"jid\":\"882bc1314ea2ee37435e89ce\",\"created_at\":1482160998.107182,\"apartment\":\"companydemo\",\"enqueued_at\":1482161650.772489,\"error_message\":\"One of the following schema(s) is invalid: \\\"companydemo\\\" \\\"public\\\"\",\"error_class\":\"Apartment::TenantNotFound\",\"failed_at\":1482161016.38253,\"retry_count\":2,\"retried_at\":1482161078.826396}"}
11:34:10 worker.1 | 2016-12-19T15:34:10.798Z 7489 TID-ox4vqdyhs WARN: Apartment::TenantNotFound: One of the following schema(s) is invalid: "companydemo" "public"
11:34:10 worker.1 | 2016-12-19T15:34:10.798Z 7489 TID-ox4vqdyhs WARN: /Users/IFrank/.rbenv/versions/2.3.1/lib/ruby/gems/2.3.0/gems/apartment-1.2.0/lib/apartment/adapters/postgresql_adapter.rb:72:in `rescue in connect_to_new'
11:34:10 worker.1 | /Users/IFrank/.rbenv/versions/2.3.1/lib/ruby/gems/2.3.0/gems/apartment-1.2.0/lib/apartment/adapters/postgresql_adapter.rb:65:in `connect_to_new'
11:34:10 worker.1 | /Users/IFrank/.rbenv/versions/2.3.1/lib/ruby/gems/2.3.0/gems/apartment-1.2.0/lib/apartment/adapters/abstract_adapter.rb:91:in `block in switch!'
11:34:10 worker.1 | /Users/IFrank/.rbenv/versions/2.3.1/lib/ruby/gems/2.3.0/gems/activesupport-4.2.6/lib/active_support/callbacks.rb:88:in `__run_callbacks__'
11:34:10 worker.1 | /Users/IFrank/.rbenv/versions/2.3.1/lib/ruby/gems/2.3.0/gems/activesupport-4.2.6/lib/active_support/callbacks.rb:778:in `_run_switch_callbacks'
11:34:10 worker.1 | /Users/IFrank/.rbenv/versions/2.3.1/lib/ruby/gems/2.3.0/gems/activesupport-4.2.6/lib/active_support/callbacks.rb:81:in `run_callbacks'
11:34:10 worker.1 | /Users/IFrank/.rbenv/versions/2.3.1/lib/ruby/gems/2.3.0/gems/apartment-1.2.0/lib/apartment/adapters/abstract_adapter.rb:88:in `switch!'
11:34:10 worker.1 | /Users/IFrank/.rbenv/versions/2.3.1/lib/ruby/gems/2.3.0/gems/apartment-1.2.0/lib/apartment/adapters/abstract_adapter.rb:105:in `switch'
11:34:10 worker.1 | /Users/IFrank/.rbenv/versions/2.3.1/lib/ruby/gems/2.3.0/gems/apartment-sidekiq-0.2.0/lib/apartment/sidekiq/middleware/server.rb:4:in `call'
11:34:10 worker.1 | /Users/IFrank/.rbenv/versions/2.3.1/lib/ruby/gems/2.3.0/gems/sidekiq-4.2.7/lib/sidekiq/middleware/chain.rb:130:in `block in invoke'
11:34:10 worker.1 | /Users/IFrank/.rbenv/versions/2.3.1/lib/ruby/gems/2.3.0/gems/sidekiq-4.2.7/lib/sidekiq/middleware/server/active_record.rb:6:in `call'
11:34:10 worker.1 | /Users/IFrank/.rbenv/versions/2.3.1/lib/ruby/gems/2.3.0/gems/sidekiq-4.2.7/lib/sidekiq/middleware/chain.rb:130:in `block in invoke'
11:34:10 worker.1 | /Users/IFrank/.rbenv/versions/2.3.1/lib/ruby/gems/2.3.0/gems/sidekiq-4.2.7/lib/sidekiq/middleware/server/retry_jobs.rb:74:in `call'
11:34:10 worker.1 | /Users/IFrank/.rbenv/versions/2.3.1/lib/ruby/gems/2.3.0/gems/sidekiq-4.2.7/lib/sidekiq/middleware/chain.rb:130:in `block in invoke'
11:34:10 worker.1 | /Users/IFrank/.rbenv/versions/2.3.1/lib/ruby/gems/2.3.0/gems/sidekiq-4.2.7/lib/sidekiq/middleware/server/logging.rb:11:in `block in call'
11:34:10 worker.1 | /Users/IFrank/.rbenv/versions/2.3.1/lib/ruby/gems/2.3.0/gems/sidekiq-4.2.7/lib/sidekiq/logging.rb:32:in `with_context'
11:34:10 worker.1 | /Users/IFrank/.rbenv/versions/2.3.1/lib/ruby/gems/2.3.0/gems/sidekiq-4.2.7/lib/sidekiq/middleware/server/logging.rb:7:in `call'
11:34:10 worker.1 | /Users/IFrank/.rbenv/versions/2.3.1/lib/ruby/gems/2.3.0/gems/sidekiq-4.2.7/lib/sidekiq/middleware/chain.rb:130:in `block in invoke'
11:34:10 worker.1 | /Users/IFrank/.rbenv/versions/2.3.1/lib/ruby/gems/2.3.0/gems/sidekiq-4.2.7/lib/sidekiq/middleware/chain.rb:133:in `invoke'
11:34:10 worker.1 | /Users/IFrank/.rbenv/versions/2.3.1/lib/ruby/gems/2.3.0/gems/sidekiq-4.2.7/lib/sidekiq/processor.rb:132:in `block (2 levels) in process'
11:34:10 worker.1 | /Users/IFrank/.rbenv/versions/2.3.1/lib/ruby/gems/2.3.0/gems/sidekiq-4.2.7/lib/sidekiq/processor.rb:174:in `stats'
11:34:10 worker.1 | /Users/IFrank/.rbenv/versions/2.3.1/lib/ruby/gems/2.3.0/gems/sidekiq-4.2.7/lib/sidekiq/processor.rb:131:in `block in process'
11:34:10 worker.1 | /Users/IFrank/.rbenv/versions/2.3.1/lib/ruby/gems/2.3.0/gems/sidekiq-4.2.7/lib/sidekiq.rb:35:in `block in <module:Sidekiq>'
11:34:10 worker.1 | /Users/IFrank/.rbenv/versions/2.3.1/lib/ruby/gems/2.3.0/gems/sidekiq-4.2.7/lib/sidekiq/processor.rb:126:in `process'
11:34:10 worker.1 | /Users/IFrank/.rbenv/versions/2.3.1/lib/ruby/gems/2.3.0/gems/sidekiq-4.2.7/lib/sidekiq/processor.rb:82:in `process_one'
11:34:10 worker.1 | /Users/IFrank/.rbenv/versions/2.3.1/lib/ruby/gems/2.3.0/gems/sidekiq-4.2.7/lib/sidekiq/processor.rb:70:in `run'
11:34:10 worker.1 | /Users/IFrank/.rbenv/versions/2.3.1/lib/ruby/gems/2.3.0/gems/sidekiq-4.2.7/lib/sidekiq/util.rb:17:in `watchdog'
11:34:10 worker.1 | /Users/IFrank/.rbenv/versions/2.3.1/lib/ruby/gems/2.3.0/gems/sidekiq-4.2.7/lib/sidekiq/util.rb:25:in `block in safe_thread'
11:34:10 worker.1 | 2016-12-19T15:34:10.800Z 7489 TID-ox4vqe0x0 MyWorker JID-8d1f27f683ff53d80921744f INFO: fail: 0.025 sec
Sounds like the apartment-sidekiq gem isn't finding the tenant.
11:34:10 worker.1 | 2016-12-19T15:34:10.798Z 7489 TID-ox4vqdyhs WARN: Apartment::TenantNotFound: One of the following schema(s) is invalid: "companydemo" "public"
running same code in public
12:06:03 worker.1 | 2016-12-19T16:06:03.901Z 7982 TID-ov17zarr8 INFO: Running in ruby 2.3.1p112 (2016-04-26 revision 54768) [x86_64-darwin15]
12:06:03 worker.1 | 2016-12-19T16:06:03.901Z 7982 TID-ov17zarr8 INFO: See LICENSE and the LGPL-3.0 for licensing details.
12:06:03 worker.1 | 2016-12-19T16:06:03.901Z 7982 TID-ov17zarr8 INFO: Upgrade to Sidekiq Pro for more features and support: http://sidekiq.org
12:06:03 worker.1 | 2016-12-19T16:06:03.903Z 7982 TID-ov17zarr8 INFO: Starting processing, hit Ctrl-C to stop
12:06:03 worker.1 | 2016-12-19T16:06:03.974Z 7982 TID-ov19dahp0 MyWorker JID-99b40a9908c8b8f114434bd8 INFO: start
12:06:03 worker.1 | ------------------------
12:06:03 worker.1 | ------------------------
12:06:03 worker.1 | Tenant public
12:06:03 worker.1 | ------------------------
12:06:03 worker.1 | ------------------------
Adding User.last in public schema
12:08:26 worker.1 | 2016-12-19T16:08:26.441Z 8064 TID-oxmshthdc WARN: ActiveRecord::StatementInvalid: PG::UndefinedTable: ERROR: relation "public.users" does not exist
12:08:26 worker.1 | LINE 1: SELECT "public"."users".* FROM "public"."users" ORDER BY "...
the problem was profile
worker: bundle exec sidekiq -e production -C ./config/sidekiq.yml
chage it to
worker: bundle exec sidekiq -C ./config/sidekiq.yml