Im using a rails multitenant app with sidekiq. I add it apartment-sidekiq so it runs the jobs in the background with redis. My problem is that when i run MyWorker on the rails c I get this error:
WARN: Apartment::TenantNotFound: One of the following schema(s) is invalid: "companydemo" "public".
or if I try to query "User.last" inside the worker, I get the PG::UndefinedTable: ERROR: relation "public.users" does not exist.
my gemset
gem 'apartment', '~> 1.0', '>= 1.0.2'
gem 'sidekiq', '~> 4.0', '>= 4.1'
gem 'apartment-sidekiq', '~> 0.2.0'
procfile
web: bundle exec puma -C config/puma.rb
log: tail -f log/development.log
redis_s: redis-server
worker: bundle exec sidekiq -e production -C ./config/sidekiq.yml
I setup a simple worker to see the results.
class MyWorker
include Sidekiq::Worker
def perform(tenant)
puts "------------------------"
puts "------------------------"
puts "Tenant #{tenant}"
puts "Current Tenant #{Apartment::Tenant.current}"
puts "------------------------"
puts "------------------------"
end
end
more details here: gist detail, my sidekiq post, my apartment-sidekiq post
Update
running rake middleware
I found that apartment-sidekiq is not running on the middleware.
use Rack::Sendfile
use ActionDispatch::Static
use Rack::Lock
use #<ActiveSupport::Cache::Strategy::LocalCache::Middleware:0x007febcb0e51f0>
use Rack::Runtime
use Rack::MethodOverride
use ActionDispatch::RequestId
use Rails::Rack::Logger
use ActionDispatch::ShowExceptions
use MetaRequest::Middlewares::Headers
use WebConsole::Middleware
use ActionDispatch::DebugExceptions
use BetterErrors::Middleware
use Raygun::Middleware::RackExceptionInterceptor
use Raygun::Middleware::RailsInsertAffectedUser
use ActionDispatch::RemoteIp
use ActionDispatch::Reloader
use ActionDispatch::Callbacks
use ActiveRecord::Migration::CheckPending
use ActiveRecord::ConnectionAdapters::ConnectionManagement
use ActiveRecord::QueryCache
use ActionDispatch::Cookies
use ActionDispatch::Session::CookieStore
use ActionDispatch::Flash
use ActionDispatch::ParamsParser
use Rack::Head
use Rack::ConditionalGet
use Rack::ETag
use Apartment::Elevators::Subdomain
use Warden::Manager
use Apartment::Reloader
use JQuery::FileUpload::Rails::Middleware
use MetaRequest::Middlewares::MetaRequestHandler
use MetaRequest::Middlewares::AppRequestHandler
use Bullet::Rack
use Apartment::Elevators::Subdomain
use RecurringSelectMiddleware
run App::Application.routes
Update soluction was using on the procfile sidekiq with -e production.
Based on your error log, it looks as if the job is enqueued successfully, and pushed to Sidekiq to be processed. It's also got your "apartment" param sent through, so the problem appears to be either:
Assuming connecting to console with
rails console
and then runningApartment::Tenant.switch!('companydemo')
throws no errors...you've definitely got some complex Sidekiq setup here so I'm leaning towards "B".The one thing that looks odd to me is this line in your Procfile:
If you are running your project locally with Foreman, you're explicitly setting your environment to "production" (
-e production
) even when it's in dev. That could potentially cause you to fail to connect to a database if you're running it locally and sidekiq is trying to connect to a remote database.