lib/rubysermon.rb
271b1a48
 require_relative "rubysermon/version"
def91ab1
 require_relative "rubysermon/mod_loader"
 require_relative "rubysermon/mod_template"
c61af953
 require_relative "rubysermon/configurator"
271b1a48
 
fb5f2bce
 require "date"
 
271b1a48
 module Rubysermon
8cbb7fe5
 	LIB_PATH = File.expand_path(File.dirname(__FILE__))
 	APP_PATH = "#{LIB_PATH}/.."
f0d38088
 	MOD_PATH = "#{LIB_PATH}/rubysermon/mod"
8cbb7fe5
 
def91ab1
 	class App
 
fb5f2bce
 		#todo: refactor
def91ab1
 		def initialize
fb5f2bce
 			@config = {repeat: 60}
 			@running_mods = []
7cc51fc7
 			@config_path = "#{LIB_PATH}/rubysermon/config.json"
fb5f2bce
 
 			config()
 			@enabled_mods = @config[:modules].to_a
 
 			start_mods()
 			start_fetch_process_sleep_cycle()
def91ab1
 		end
 
 		private
c61af953
 
 		def config
 			configurator = Configurator.new(@config_path)
e9310bd3
 			config = configurator.get_settings()
c196a26b
 			@config.merge!(config)
c61af953
 		end
 
fb5f2bce
 		def start_mods
 			@enabled_mods.each do |mod| load_mod(mod) end
 		end
 
def91ab1
 		def load_mod(mod_name)
 			begin
fb5f2bce
 				mod = ModLoader.load(mod_name)
 				@running_mods.push(mod)
def91ab1
 			rescue ModLoaderException => e
 				$stderr = e.message
 			end
 		end
fb5f2bce
 
 		def start_fetch_process_sleep_cycle
ab928c8d
 			if (msg = cannot_start_fetch_process_sleep_cycle?)
 				abort(msg)
fb5f2bce
 			end
 
 			while true
 				current_time = DateTime.now()
 				@running_mods.each do |mod|
 					result = mod.results
 					notify = mod.notify?
 				end
 				sleep @config[:repeat]
 			end
 		end
 
ab928c8d
 		def cannot_start_fetch_process_sleep_cycle?
 			if @config[:repeat].to_i < 1
 				return "Repeat cycle too short"
 			end
 
 			false
fb5f2bce
 		end
def91ab1
 	end
271b1a48
 end