Class: KSConnect

Inherits:
Object
  • Object
show all
Includes:
Logs
Defined in:
lib/ksconnect.rb,
lib/ksconnect/api.rb,
lib/ksconnect/helpers.rb,
lib/ksconnect/api/plugin.rb,
lib/ksconnect/api/plugin/data.rb,
lib/ksconnect/api/plugin/config.rb,
lib/ksconnect/api/plugin/domain.rb

Defined Under Namespace

Modules: Helpers Classes: API

Constant Summary collapse

MIN_THREADS =
(ENV['MIN_THREADS'] || 5).to_i
MAX_THREADS =
(ENV['MAX_THREADS'] || 25).to_i

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Logs

included

Constructor Details

#initialize(*args) ⇒ KSConnect

Returns a new instance of KSConnect.



20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/ksconnect.rb', line 20

def initialize(*args)
  Redis.current ||= Redis.new(driver: :hiredis)
  $redis ||= ConnectionPool.new(size: MAX_THREADS, timeout: 8) { Redis.current }

  plugins = args

  additional_options = args.last.is_a?(Hash) ? args.last : nil
  if additional_options
    plugins.pop
  else
    additional_options = {}
  end

  @api = KSConnect::API.new(enabled_plugins: plugins, use_helpers: additional_options[:use_helpers])
end

Instance Attribute Details

#apiObject (readonly)

Returns the value of attribute api.



14
15
16
# File 'lib/ksconnect.rb', line 14

def api
  @api
end

#pluginObject (readonly)

Returns the value of attribute plugin.



15
16
17
# File 'lib/ksconnect.rb', line 15

def plugin
  @plugin
end

Class Method Details

.channel(name) ⇒ Object



41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
# File 'lib/ksconnect.rb', line 41

def self.channel(name)
  Thread.start do
    begin
      Redis.new(driver: :hiredis).subscribe(name) do |on|
        logger.info "Subscribing to redis channel: #{name}"
        on.message do |_channel, message|
          thread_pool.process do
            begin
              yield message
            rescue => error
              logger.error "#{error.message} on redis channel #{name}:"
              logger.error error
            end
          end
        end
      end
    rescue => error
      logger.error "Fatal error on redis channel #{name}, restarting in 0.5s"
      logger.error error.backtrace.join("\n")
      sleep 0.5
      retry
    end
  end
end

.thread_poolObject



36
37
38
39
# File 'lib/ksconnect.rb', line 36

def self.thread_pool
  Thread::Pool.abort_on_exception = true
  @@thread_pool ||= Thread.pool(MIN_THREADS, MAX_THREADS)
end