Class: PumaAutoTune::Hook

Inherits:
Object
  • Object
show all
Defined in:
lib/puma_auto_tune/hook.rb

Instance Method Summary collapse

Constructor Details

#initialize(resource) ⇒ Hook

Returns a new instance of Hook.



4
5
6
7
8
9
# File 'lib/puma_auto_tune/hook.rb', line 4

def initialize(resource)
  @resource = resource
  @started  = Time.now
  @hooks    = {}
  @wraps    = {}
end

Instance Method Details

#argsObject



52
53
54
55
# File 'lib/puma_auto_tune/hook.rb', line 52

def args
  @resource.reset
  [@resource.amount, @resource.master, @resource.workers]
end

#auto_cycleObject



33
34
35
36
37
38
39
40
# File 'lib/puma_auto_tune/hook.rb', line 33

def auto_cycle
  Thread.new do
    loop do
      sleep PumaAutoTune.frequency
      call(:cycle) if @resource.master.running?
    end
  end
end

#call(name) ⇒ Object



17
18
19
20
# File 'lib/puma_auto_tune/hook.rb', line 17

def call(name)
  hook = @hooks[name] or raise "No such hook #{name.inspect}. Available: #{@hooks.keys.inspect}"
  hook.call(self.args)
end

#define_hook(name, &block) ⇒ Object Also known as: set



11
12
13
14
# File 'lib/puma_auto_tune/hook.rb', line 11

def define_hook(name, &block)
  wrap_hook(name)
  @hooks[name] = block
end

#log(msg, options = {}) ⇒ Object



42
43
44
45
46
47
48
49
50
# File 'lib/puma_auto_tune/hook.rb', line 42

def log(msg, options = {})
  elapsed = (Time.now - @started).ceil
  msg     = ["PumaAutoTune (#{elapsed}s): #{msg}"]

  options[@resource.name] = @resource.amount
  options["current_cluster_size"] = @resource.workers.size
  options.each { |k, v| msg << "puma.#{k.to_s.downcase}=#{v}" }
  puts msg.join(" ")
end

#wrap_hook(name, &block) ⇒ Object Also known as: wrap



22
23
24
25
26
27
28
29
30
# File 'lib/puma_auto_tune/hook.rb', line 22

def wrap_hook(name, &block)
  if block
    @wraps[name] = block
  else
    if wrap = @wraps[name]
      @hooks[name] = wrap.call(@hooks[name])
    end
  end
end