为什么apartment-sidekiq找不到房客?

时间:2016-12-19 19:08:59

标签: ruby-on-rails sidekiq jobs apartment-gem

我正在使用带有sidekiq的rails multitenant应用程序。 我添加它apartment-sidekiq所以它在后台用redis运行作业。 我的问题是,当我在rails上运行MyWorker时,我收到此错误:

  

警告:Apartment :: TenantNotFound:以下架构之一无效:" companydemo" "公共"

     

或者如果我尝试查询" User.last"在worker中,我得到了PG :: UndefinedTable:ERROR:relation" public.users"不存在。

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

我设置了一个简单的工作人员来查看结果。

  class MyWorker
  include Sidekiq::Worker
    def perform(tenant)
      puts  "------------------------"
      puts  "------------------------"
      puts  "Tenant #{tenant}"
      puts  "Current Tenant #{Apartment::Tenant.current}"
      puts  "------------------------"
      puts  "------------------------"
    end
  end

enter image description here

此处提供了更多详细信息:gist detailmy sidekiq post my apartment-sidekiq post

更新

运行rake middleware我发现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

更新解决方案 正在使用proc文件sidekiq和-e production。

1 个答案:

答案 0 :(得分:1)

根据您的错误日志,看起来该作业已成功排队,并推送到Sidekiq进行处理。这也是你的“公寓”传递,所以问题似乎是:

  • A)您实际上没有创建名为“companydemo”的架构。
  • B) 您没有“正确”连接到postgre数据库。

假设使用rails console连接到控制台然后运行Apartment::Tenant.switch!('companydemo')没有错误...你肯定有一些复杂的Sidekiq设置here所以我倾向于“B”

对我来说有点奇怪的是你的Procfile中的这一行:

worker: bundle exec sidekiq -e production -C ./config/sidekiq.yml

如果您使用Foreman在本地运行项目,那么即使在开发环境中,您也明确将环境设置为“生产”(-e production)。如果您在本地运行并且sidekiq正在尝试连接到远程数据库,那么这可能会导致您无法连接到数据库。