为什么 apartment-sidekiq 找不到租户?
Why is apartment-sidekiq not finding the tenant?
我正在使用带有 sidekiq 的 rails 多租户应用程序。
我将它添加到 apartment-sidekiq,因此它 运行 使用 redis 在后台执行作业。
我的问题是,当我在 rails c 上使用 运行 MyWorker 时,出现此错误:
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.
我的宝石
gem 'apartment', '~> 1.0', '>= 1.0.2'
gem 'sidekiq', '~> 4.0', '>= 4.1'
gem 'apartment-sidekiq', '~> 0.2.0'
proc文件
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
我设置了一个简单的 worker 来查看结果。
class MyWorker
include Sidekiq::Worker
def perform(tenant)
puts "------------------------"
puts "------------------------"
puts "Tenant #{tenant}"
puts "Current Tenant #{Apartment::Tenant.current}"
puts "------------------------"
puts "------------------------"
end
end
此处有更多详细信息:gist detail, my sidekiq post, my apartment-sidekiq post
更新
运行ning rake middleware
我发现中间件上没有运行ning
apartment-sidekiq
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
更新解决方案
正在使用 -e production 的 procfile sidekiq。
根据您的错误日志,作业似乎已成功入队,并已推送到 Sidekiq 进行处理。它还发送了您的 "apartment" 参数,因此问题似乎是:
- A) 您实际上并没有创建名为 "companydemo" 的架构。
- B)
您没有 "properly" 连接到您的 postgre 数据库。
假设使用 rails console
连接到控制台然后 运行 Apartment::Tenant.switch!('companydemo')
不会抛出任何错误...您肯定有一些复杂的 Sidekiq 设置 here 所以我'我倾向于 "B".
我觉得奇怪的一件事是您的 Procfile 中的这一行:
worker: bundle exec sidekiq -e production -C ./config/sidekiq.yml
如果您是 运行 您在本地使用 Foreman 的项目,即使在开发环境中,您也会明确地将环境设置为 "production" (-e production
)。如果您在本地 运行 并且 sidekiq 正在尝试连接到远程数据库,这可能会导致您无法连接到数据库。
我正在使用带有 sidekiq 的 rails 多租户应用程序。 我将它添加到 apartment-sidekiq,因此它 运行 使用 redis 在后台执行作业。 我的问题是,当我在 rails c 上使用 运行 MyWorker 时,出现此错误:
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.
我的宝石
gem 'apartment', '~> 1.0', '>= 1.0.2'
gem 'sidekiq', '~> 4.0', '>= 4.1'
gem 'apartment-sidekiq', '~> 0.2.0'
proc文件
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
我设置了一个简单的 worker 来查看结果。
class MyWorker
include Sidekiq::Worker
def perform(tenant)
puts "------------------------"
puts "------------------------"
puts "Tenant #{tenant}"
puts "Current Tenant #{Apartment::Tenant.current}"
puts "------------------------"
puts "------------------------"
end
end
此处有更多详细信息:gist detail, my sidekiq post, my apartment-sidekiq post
更新
运行ning rake middleware
我发现中间件上没有运行ning
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
更新解决方案 正在使用 -e production 的 procfile sidekiq。
根据您的错误日志,作业似乎已成功入队,并已推送到 Sidekiq 进行处理。它还发送了您的 "apartment" 参数,因此问题似乎是:
- A) 您实际上并没有创建名为 "companydemo" 的架构。
- B) 您没有 "properly" 连接到您的 postgre 数据库。
假设使用 rails console
连接到控制台然后 运行 Apartment::Tenant.switch!('companydemo')
不会抛出任何错误...您肯定有一些复杂的 Sidekiq 设置 here 所以我'我倾向于 "B".
我觉得奇怪的一件事是您的 Procfile 中的这一行:
worker: bundle exec sidekiq -e production -C ./config/sidekiq.yml
如果您是 运行 您在本地使用 Foreman 的项目,即使在开发环境中,您也会明确地将环境设置为 "production" (-e production
)。如果您在本地 运行 并且 sidekiq 正在尝试连接到远程数据库,这可能会导致您无法连接到数据库。