Module: ConfigmonkeyCli::Application::Core

Included in:
ConfigmonkeyCli::Application
Defined in:
lib/configmonkey_cli/application/core.rb

Instance Method Summary collapse

Instance Method Details

#fire(which, *args) ⇒ Object



54
55
56
57
58
# File 'lib/configmonkey_cli/application/core.rb', line 54

def fire which, *args
  return if @disable_event_firing
  sync { debug "[Event] Firing #{which} (#{@hooks[which].try(:length) || 0} handlers) #{args.map(&:class)}", 99 }
  @hooks[which] && @hooks[which].each{|h| h.call(*args) }
end

#haltpoint(thr = Thread.current) ⇒ Object



27
28
29
# File 'lib/configmonkey_cli/application/core.rb', line 27

def haltpoint thr = Thread.current
  thr.raise Interrupt if $cm_runtime_exiting
end

#hook(*which, &hook_block) ⇒ Object

Events =



47
48
49
50
51
52
# File 'lib/configmonkey_cli/application/core.rb', line 47

def hook *which, &hook_block
  which.each do |w|
    @hooks[w.to_sym] ||= []
    @hooks[w.to_sym] << hook_block
  end
end

#interruptable(&block) ⇒ Object



31
32
33
34
35
36
37
38
39
40
41
# File 'lib/configmonkey_cli/application/core.rb', line 31

def interruptable &block
  Thread.new do
    begin
      thr = Thread.current
      $interruptable_threads << Thread.current
      thr[:return_value] = block.call(thr)
    ensure
      $interruptable_threads.delete(Thread.current)
    end
  end.join[:return_value]
end

#loggerObject



68
69
70
71
72
73
74
75
# File 'lib/configmonkey_cli/application/core.rb', line 68

def logger
  sync do
    @logger ||= begin
      FileUtils.mkdir_p(File.dirname(@opts[:logfile]))
      Logger.new(@opts[:logfile], 10, 1024000)
    end
  end
end

#logger_filenameObject

Logger =



64
65
66
# File 'lib/configmonkey_cli/application/core.rb', line 64

def logger_filename
  "#{cm_cfg_path}/logs/configmonkey.log"
end

#release_signalsObject



21
22
23
24
25
# File 'lib/configmonkey_cli/application/core.rb', line 21

def release_signals
  debug "Releasing INT signal..."
  Signal.trap("INT", "DEFAULT")
  # Signal.trap("TERM", "DEFAULT")
end

#trap_signalsObject

Signal trapping =



7
8
9
10
11
12
13
14
15
16
17
18
19
# File 'lib/configmonkey_cli/application/core.rb', line 7

def trap_signals
  debug "Trapping INT signal..."
  $interruptable_threads = []
  Signal.trap("INT") do
    $cm_runtime_exiting = true
    $interruptable_threads.each{|thr| thr.raise(Interrupt) if thr.alive? }
    Kernel.puts "Interrupting..."
  end
  # Signal.trap("TERM") do
  #   $cm_runtime_exiting = true
  #   Kernel.puts "Terminating..."
  # end
end