2016-12-19 57 views
1

我正在使用帶有sidekiq的rails multitenant應用程序。 我將它添加到apartment-sidekiq中,以便它使用redis在後臺運行作業。 我的問題是,當我在鐵軌上運行MyWorker I C得到這個錯誤:爲什麼apartment-sidekiq找不到租客?

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' 

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我發現公寓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 

更新soluction 使用與-e生產procfile sidekiq了。

回答

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 

如果在本地與工頭運行項目,你明確地設置你的環境,以「生產」(-e production)即使它在開發中。如果你在本地運行它並且sidekiq試圖連接到遠程數據庫,那麼這可能會導致你無法連接到數據庫。