Class: CheckTaskr::JobsConfiguration
- Inherits:
-
Object
- Object
- CheckTaskr::JobsConfiguration
- 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
-
#actions ⇒ Object
readonly
Returns the value of attribute actions.
-
#listen_port ⇒ Object
Returns the value of attribute listen_port.
-
#load_paths ⇒ Object
readonly
Returns the value of attribute load_paths.
-
#locked ⇒ Object
Returns the value of attribute locked.
-
#results ⇒ Object
Returns the value of attribute results.
-
#sleep_time ⇒ Object
Returns the value of attribute sleep_time.
Class Method Summary collapse
Instance Method Summary collapse
- #add_item(item) ⇒ Object
- #check_http_result(name, options = {}) ⇒ Object
- #execute_all ⇒ Object
- #http_json(name, options = {}) ⇒ Object
- #http_returncode(name, options = {}) ⇒ Object
-
#initialize ⇒ JobsConfiguration
constructor
A new instance of JobsConfiguration.
- #load_from_file(file, name = nil) ⇒ Object
- #lock ⇒ Object
-
#log_level(level) ⇒ Object
set log level.
-
#process_hosts(options) ⇒ Object
process hosts from options.
- #run_actions(actions, results) ⇒ Object
- #setup_http_json(options) ⇒ Object
- #setup_http_result(options) ⇒ Object
- #setup_http_returncode(options) ⇒ Object
- #setup_tcp_port(options) ⇒ Object
- #setup_xmpp_chat(options) ⇒ Object
- #tcp_port(name, options = {}) ⇒ Object
- #unlock ⇒ Object
- #xmpp_chat(name, options = {}) ⇒ Object
Constructor Details
#initialize ⇒ JobsConfiguration
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
#actions ⇒ Object (readonly)
Returns the value of attribute actions.
23 24 25 |
# File 'lib/check-taskr/base.rb', line 23 def actions @actions end |
#listen_port ⇒ Object
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_paths ⇒ Object (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 |
#locked ⇒ Object
Returns the value of attribute locked.
22 23 24 |
# File 'lib/check-taskr/base.rb', line 22 def locked @locked end |
#results ⇒ Object
Returns the value of attribute results.
22 23 24 |
# File 'lib/check-taskr/base.rb', line 22 def results @results end |
#sleep_time ⇒ Object
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( = {}) _instance = self.instance _instance.sleep_time = [:sleep_time] || 8 _instance.listen_port = [: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, = {}) process_hosts() do |host| action = HttpResultAction.new({:name => "#{name}-#{host}", :ip => host}.merge()) @actions << action end end |
#execute_all ⇒ Object
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, = {}) process_hosts() do |host| action = HttpJsonAction.new({:name => "#{name}-#{host}", :ip => host}.merge()) @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, = {}) process_hosts() do |host| action = HttpReturnCodeAction.new({:name => "#{name}-#{host}", :ip => host}.merge()) @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 |
#lock ⇒ Object
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() log = Log4r::Logger['default'] hosts = .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() HttpJsonAction.setup() end |
#setup_http_result(options) ⇒ Object
8 9 10 |
# File 'lib/check-taskr/task/http_result.rb', line 8 def setup_http_result() HttpResultAction.setup() end |
#setup_http_returncode(options) ⇒ Object
8 9 10 |
# File 'lib/check-taskr/task/http_code.rb', line 8 def setup_http_returncode() HttpReturnCodeAction.setup() end |
#setup_tcp_port(options) ⇒ Object
9 10 11 |
# File 'lib/check-taskr/task/sockets.rb', line 9 def setup_tcp_port() SocketAction.setup() end |
#setup_xmpp_chat(options) ⇒ Object
10 11 12 |
# File 'lib/check-taskr/task/xmpp_chat.rb', line 10 def setup_xmpp_chat() HttpReturnCodeAction.setup() 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, = {}) process_hosts() do |host| action = SocketAction.new(:name => "#{name}-#{host}", :ip => host, :port => .fetch(:port)) action.error_code ||= [:error_code] action.error_msg ||= [:error_msg] @actions << action end end |
#unlock ⇒ Object
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, = {}) process_hosts() do |host| action = XmppChatAction.new({:name => "#{name}-#{host}", :ip => host}.merge()) @actions << action end end |