Class: CheckTaskr::JobsConfiguration

Inherits:
Object
  • Object
show all
Includes:
Log4r, Singleton
Defined in:
lib/check-taskr/base.rb,
lib/check-taskr/task/sockets.rb,
lib/check-taskr/task/http_code.rb,
lib/check-taskr/task/http_json.rb,
lib/check-taskr/task/xmpp_chat.rb,
lib/check-taskr/task/http_result.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeJobsConfiguration

Returns a new instance of JobsConfiguration.



25
26
27
28
29
30
31
# File 'lib/check-taskr/base.rb', line 25

def initialize
  @actions = []
  @sleep_time = 8
  @results = Hash.new
  @listen_port = 4567
  @locked = false
end

Instance Attribute Details

#actionsObject (readonly)

Returns the value of attribute actions.



23
24
25
# File 'lib/check-taskr/base.rb', line 23

def actions
  @actions
end

#listen_portObject

Returns the value of attribute listen_port.



22
23
24
# File 'lib/check-taskr/base.rb', line 22

def listen_port
  @listen_port
end

#load_pathsObject (readonly)

Returns the value of attribute load_paths.



23
24
25
# File 'lib/check-taskr/base.rb', line 23

def load_paths
  @load_paths
end

#lockedObject

Returns the value of attribute locked.



22
23
24
# File 'lib/check-taskr/base.rb', line 22

def locked
  @locked
end

#resultsObject

Returns the value of attribute results.



22
23
24
# File 'lib/check-taskr/base.rb', line 22

def results
  @results
end

#sleep_timeObject

Returns the value of attribute sleep_time.



22
23
24
# File 'lib/check-taskr/base.rb', line 22

def sleep_time
  @sleep_time
end

Class Method Details

.init(options = {}) ⇒ Object



48
49
50
51
52
53
54
55
# File 'lib/check-taskr/base.rb', line 48

def self.init(options = {})
  _instance = self.instance
  _instance.sleep_time = options[:sleep_time] || 8
  _instance.listen_port = options[:port] || 4567
  if block_given?
    yield _instance
  end
end

Instance Method Details

#add_item(item) ⇒ Object



33
34
35
36
37
38
# File 'lib/check-taskr/base.rb', line 33

def add_item(item)
  if @items.nil?
    @items = Array.new
  end
  @items.add(item)
end

#check_http_result(name, options = {}) ⇒ Object



12
13
14
15
16
17
# File 'lib/check-taskr/task/http_result.rb', line 12

def check_http_result(name, options = {})
  process_hosts(options) do |host|
    action = HttpResultAction.new({:name => "#{name}-#{host}", :ip => host}.merge(options))
    @actions << action
  end
end

#execute_allObject



57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
# File 'lib/check-taskr/base.rb', line 57

def execute_all
  log = Log4r::Logger['default']
  return if @locked
  results = Hash.new
  had_error = false

  fail_actions = run_actions(@actions, results)

  # 如果有失败,过0.1秒后重试失败的
  if fail_actions.size > 0
    sleep(0.1)
    fail_actions2 = run_actions(fail_actions, results)
    if fail_actions2.size > 0
      # 还失败的话,过1秒后重新试一次
      sleep(1)
      run_actions(fail_actions2, results)
    end
  end
  @results.clear
  @results = results
end

#http_json(name, options = {}) ⇒ Object



13
14
15
16
17
18
# File 'lib/check-taskr/task/http_json.rb', line 13

def http_json(name, options = {})
  process_hosts(options) do |host|
    action = HttpJsonAction.new({:name => "#{name}-#{host}", :ip => host}.merge(options))
    @actions << action
  end
end

#http_returncode(name, options = {}) ⇒ Object



12
13
14
15
16
17
# File 'lib/check-taskr/task/http_code.rb', line 12

def http_returncode(name, options = {})
  process_hosts(options) do |host|
    action = HttpReturnCodeAction.new({:name => "#{name}-#{host}", :ip => host}.merge(options))
    @actions << action
  end
end

#load_from_file(file, name = nil) ⇒ Object



104
105
106
107
108
# File 'lib/check-taskr/base.rb', line 104

def load_from_file(file, name=nil)
  file = find_file_in_load_path(file) unless File.file?(file)
  string = File.read(file)
  instance_eval(string, name || "<eval>")
end

#lockObject



40
41
42
# File 'lib/check-taskr/base.rb', line 40

def lock
  @locked = true
end

#log_level(level) ⇒ Object

set log level



112
113
114
115
# File 'lib/check-taskr/base.rb', line 112

def log_level(level)
  log = Log4r::Logger['default']
  log.level = level
end

#process_hosts(options) ⇒ Object

process hosts from options



118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
# File 'lib/check-taskr/base.rb', line 118

def process_hosts(options)
  log = Log4r::Logger['default']
  hosts = options.delete(:hosts)
  if block_given?
    if hosts.nil?
      throw Exception.new("Must include :hosts option")
    end
    if hosts.class.eql?(String)
      yield hosts
    else
      hosts.each do |host|
        yield host
      end
    end
  end
end

#run_actions(actions, results) ⇒ Object



79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
# File 'lib/check-taskr/base.rb', line 79

def run_actions(actions, results)
  fail_actions = []
  log = Log4r::Logger['default']
  actions.each do |action|
    hash = action.execute
    unless hash.nil?
      if hash["ip"].nil? && hash[:ip].nil?
        hash.each do |k, v|
          unless (v["ip"].nil? && v[:ip].nil?)
            results["#{action.name}_#{k}"] = v
          end
        end
      else
        results[action.name] = hash
      end
      state_code = hash[:stat] || hash['stat']
      if !"0".eql?(state_code) && !0.eql?(state_code)
        log.error "#{Time.now}:#{hash.to_json}"
        fail_actions << action
      end
    end
  end
  fail_actions
end

#setup_http_json(options) ⇒ Object



9
10
11
# File 'lib/check-taskr/task/http_json.rb', line 9

def setup_http_json(options)
  HttpJsonAction.setup(options)
end

#setup_http_result(options) ⇒ Object



8
9
10
# File 'lib/check-taskr/task/http_result.rb', line 8

def setup_http_result(options)
  HttpResultAction.setup(options)
end

#setup_http_returncode(options) ⇒ Object



8
9
10
# File 'lib/check-taskr/task/http_code.rb', line 8

def setup_http_returncode(options)
  HttpReturnCodeAction.setup(options)
end

#setup_tcp_port(options) ⇒ Object



9
10
11
# File 'lib/check-taskr/task/sockets.rb', line 9

def setup_tcp_port(options)
  SocketAction.setup(options)
end

#setup_xmpp_chat(options) ⇒ Object



10
11
12
# File 'lib/check-taskr/task/xmpp_chat.rb', line 10

def setup_xmpp_chat(options)
  HttpReturnCodeAction.setup(options)
end

#tcp_port(name, options = {}) ⇒ Object



13
14
15
16
17
18
19
20
21
# File 'lib/check-taskr/task/sockets.rb', line 13

def tcp_port(name, options = {})
  process_hosts(options) do |host|
    action = SocketAction.new(:name => "#{name}-#{host}", :ip => host, :port => options.fetch(:port))
    action.error_code ||= options[:error_code]
    action.error_msg ||= options[:error_msg]

    @actions << action
  end
end

#unlockObject



44
45
46
# File 'lib/check-taskr/base.rb', line 44

def unlock
  @locked = false
end

#xmpp_chat(name, options = {}) ⇒ Object



14
15
16
17
18
19
# File 'lib/check-taskr/task/xmpp_chat.rb', line 14

def xmpp_chat(name, options = {})
  process_hosts(options) do |host|
    action = XmppChatAction.new({:name => "#{name}-#{host}", :ip => host}.merge(options))
    @actions << action
  end
end