Sha256: c6f53b33c619d539325544fed7f72731c640ee9342d2aa2984ba079999cf73f3

Contents?: true

Size: 1.88 KB

Versions: 1

Compression:

Stored size: 1.88 KB

Contents

module Perkins
  class Application
    attr_accessor :host , :port, :working_dir, :redis, :database, :sse_endpoint
    attr_reader   :server
    attr_accessor :github_client_id, :github_client_secret

    class << self
      attr_accessor :instance
    end

    def initialize(opts={})
      host = opts[:host] unless opts[:host].blank?
      post = opts[:port] unless opts[:port].blank?
      github_client_id = opts[:github_client_id] unless opts[:github_client_id].blank?
      github_client_server = opts[:github_client_secret] unless opts[:github_client_secret].blank?
      working_dir = opts[:working_dir] unless opts[:working_dir].blank?
      self.class.instance = self
      self
    end

    def database=(file)
      @dbconfig = YAML::load(File.open(file))
      @db = ActiveRecord::Base.establish_connection(@dbconfig[ENV['RACK_ENV']])
      setup_sidekiq
    end

    def database
      @db
    end

    def redis=(opts={})
      namespace = opts.delete(:namespace) || :perkins
      redis_connection = Redis.new(opts)
      $redis = Redis::Namespace.new(namespace, :redis => redis_connection)
    end

    def as_json(options = {})
      data = {}
      fields = [:host, :port, :github_client_id,
                :github_client_secret, :working_dir]

      fields.each { |k| data[k] = send(k) }
      data
    end

    def setup_sidekiq
      # Sidekiq server is multi-threaded so our Redis connection pool size defaults to concurrency (-c)
      Sidekiq.configure_server do |config|
        config.redis = { :namespace => 'perkins-worker' }
        if defined?(ActiveRecord::Base)
          #ActiveRecord::Base.establish_connection
          #Perkins::Application.instance.database
          ActiveRecord::Base.establish_connection(@dbconfig[ENV['RACK_ENV']])
        end
      end

      Sidekiq.configure_client do |config|
        config.redis = { :namespace => 'perkins-worker' }
      end

    end

  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
perkins-0.0.2 lib/perkins/application.rb