Last active
January 4, 2017 16:47
-
-
Save Frank004/48276206a96e45102e9004f2c19f38d0 to your computer and use it in GitHub Desktop.
Error with connection with rails, apartment, pg and sidekiq/redis
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# You can have Apartment route to the appropriate Tenant by adding some Rack middleware. | |
# Apartment can support many different "Elevators" that can take care of this routing to your data. | |
# Require whichever Elevator you're using below or none if you have a custom one. | |
# | |
# require 'apartment/elevators/generic' | |
# require 'apartment/elevators/domain' | |
require 'apartment/elevators/subdomain' | |
# Apartment Configuration | |
# | |
Apartment.configure do |config| | |
# Add any models that you do not want to be multi-tenanted, but remain in the global (public) namespace. | |
# A typical example would be a Customer or Tenant model that stores each Tenant's information. | |
# | |
config.excluded_models = ["User","ServiceUser","ClientUser","Company","CompanyConfig","Admin"] | |
# In order to migrate all of your Tenants you need to provide a list of Tenant names to Apartment. | |
# You can make this dynamic by providing a Proc object to be called on migrations. | |
# This object should yield an array of strings representing each Tenant name. | |
# | |
# config.tenant_names = lambda{ Customer.pluck(:tenant_name) } | |
# config.tenant_names = ['tenant1', 'tenant2'] | |
# | |
config.tenant_names = lambda { Company.pluck :subdomain } | |
# | |
# ==> PostgreSQL only options | |
# Specifies whether to use PostgreSQL schemas or create a new database per Tenant. | |
# The default behaviour is true. | |
# | |
# config.use_schemas = true | |
# Apartment can be forced to use raw SQL dumps instead of schema.rb for creating new schemas. | |
# Use this when you are using some extra features in PostgreSQL that can't be respresented in | |
# schema.rb, like materialized views etc. (only applies with use_schemas set to true). | |
# (Note: this option doesn't use db/structure.sql, it creates SQL dump by executing pg_dump) | |
# | |
# config.use_sql = false | |
# There are cases where you might want some schemas to always be in your search_path | |
# e.g when using a PostgreSQL extension like hstore. | |
# Any schemas added here will be available along with your selected Tenant. | |
# | |
# config.persistent_schemas = %w{ hstore } | |
# <== PostgreSQL only options | |
# | |
# By default, and only when not using PostgreSQL schemas, Apartment will prepend the environment | |
# to the tenant name to ensure there is no conflict between your environments. | |
# This is mainly for the benefit of your development and test environments. | |
# Uncomment the line below if you want to disable this behaviour in production. | |
# | |
# config.prepend_environment = !Rails.env.production? | |
end | |
# Setup a custom Tenant switching middleware. The Proc should return the name of the Tenant that | |
# you want to switch to. | |
# Rails.application.config.middleware.use 'Apartment::Elevators::Generic', lambda { |request| | |
# request.host.split('.').first | |
# } | |
Apartment::Elevators::Subdomain.excluded_subdomains = ['www',"support","blog","billing","help","api","admin","info"] | |
Rails.application.config.middleware.use 'Apartment::Elevators::Subdomain' | |
Rails.application.config.middleware.insert_before 'Warden::Manager', 'Apartment::Elevators::Subdomain' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
module Apartment::Sidekiq::Middleware | |
class Server | |
def call(worker_class, item, queue) | |
puts "----------------------------" | |
puts "The item param for apartment: #{item["apartment"]}" | |
puts "----------------------------" | |
puts "Worker Class: #{worker_class}" | |
puts "----------------------------" | |
puts "The queue: #{queue}" | |
puts "----------------------------" | |
puts "Tenant: #{Apartment::Tenant.switch(item['apartment'])}" | |
puts "----------------------------" | |
#this just so the job run it will fail im just looking for the top part output | |
Apartment::Tenant.current do | |
yield | |
end | |
end | |
end | |
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
--------------- | |
gem 'apartment', '~> 1.0', '>= 1.0.2' | |
gem 'sidekiq', '~> 4.0', '>= 4.1' | |
gem 'apartment-sidekiq', '~> 1.1.0' | |
group :production do | |
gem 'autoscaler', '~> 0.11.0' | |
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
--------------- | |
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
rails c | |
MyWorker.perform_in(1.seconds, Apartment::Tenant.current) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
--------------- | |
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} | |
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} | |
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
--------------- | |
:concurrency: 10 | |
# you can override concurrency based on environment | |
production: | |
:concurrency: 25 | |
# Set timeout to 8 on Heroku, longer if you manage your own systems. | |
:timeout: 8 | |
:queues: | |
- critical | |
- default | |
- paperclip | |
- low |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
11:20:24 worker.1 | 2016-06-09T15:20:24.581Z 30781 TID-ow4pahmoc MyWorker JID-a5a0ca08afbaaf0b10f37774 INFO: start | |
11:20:24 worker.1 | ---------------------------- | |
11:20:24 worker.1 | The item param for apartment: public | |
11:20:24 worker.1 | ---------------------------- | |
11:20:24 worker.1 | Worker Class: #<MyWorker:0x007fbcdc0004b0> | |
11:20:24 worker.1 | ---------------------------- | |
11:20:24 worker.1 | The queue: default | |
11:20:24 worker.1 | ---------------------------- | |
11:20:24 worker.1 | Tenant: "public" | |
11:20:24 worker.1 | ---------------------------- | |
11:20:24 worker.1 | ------------------------ | |
11:20:24 worker.1 | ------------------------ | |
11:20:24 worker.1 | Current Tenant public | |
11:20:24 worker.1 | ------------------------ | |
11:20:24 worker.1 | ------------------------ | |
11:20:24 worker.1 | 2016-06-09T15:20:24.584Z 30781 TID-ow4pahmoc MyWorker JID-a5a0ca08afbaaf0b10f37774 INFO: done: 0.003 sec | |
11:20:54 worker.1 | 2016-06-09T15:20:54.436Z 30781 TID-ow4pashdc MyWorker JID-d27f2334ac2452d15e6bf9ba INFO: start | |
11:20:54 worker.1 | ---------------------------- | |
11:20:54 worker.1 | The item param for apartment: companydemo | |
11:20:54 worker.1 | ---------------------------- | |
11:20:54 worker.1 | Worker Class: #<MyWorker:0x007fbcd836c968> | |
11:20:54 worker.1 | ---------------------------- | |
11:20:54 worker.1 | The queue: default | |
11:20:54 worker.1 | ---------------------------- | |
11:20:54 worker.1 | 2016-06-09T15:20:54.439Z 30781 TID-ow4pashdc MyWorker JID-d27f2334ac2452d15e6bf9ba INFO: fail: 0.003 sec | |
11:20:54 worker.1 | 2016-06-09T15:20:54.439Z 30781 TID-ow4pashdc WARN: {"class":"MyWorker","args":["companydemo"],"retry":true,"queue":"default","jid":"d27f2334ac2452d15e6bf9ba","created_at":1465485640.888371,"apartment":"companydemo","enqueued_at":1465485654.435481,"error_message":"One of the following schema(s) is invalid: \"companydemo\" \"public\"","error_class":"Apartment::TenantNotFound","failed_at":1465485654.439004,"retry_count":0} | |
11:20:54 worker.1 | 2016-06-09T15:20:54.439Z 30781 TID-ow4pashdc WARN: Apartment::TenantNotFound: One of the following schema(s) is invalid: "companydemo" "public" | |
11:20:54 worker.1 | 2016-06-09T15:20:54.439Z 30781 TID-ow4pashdc WARN: /Users/IFrank/.rbenv/versions/2.3.0/lib/ruby/gems/2.3.0/gems/apartment-1.1.0/lib/apartment/adapters/postgresql_adapter.rb:72:in `rescue in connect_to_new' |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment