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
71bcfce4
 		attr_accessor :config_path
def91ab1
 
 		def initialize
71bcfce4
 			@config 		= {repeat: 60}
 			@running_mods	= []
 			@config_path	= "#{LIB_PATH}/rubysermon/config.json"
 			@enabled_mods	= []
 		end
fb5f2bce
 
71bcfce4
 		def run
 			read_config()
 			enable_and_start_mods()
 
 			if (msg = cannot_start_fetch_process_sleep_cycle?)
 				abort(msg)
 			end
fb5f2bce
 
 			start_fetch_process_sleep_cycle()
def91ab1
 		end
 
 		private
c61af953
 
71bcfce4
 		def read_config
c61af953
 			configurator = Configurator.new(@config_path)
e9310bd3
 			config = configurator.get_settings()
c196a26b
 			@config.merge!(config)
c61af953
 		end
 
71bcfce4
 		def enable_and_start_mods
 			@enabled_mods = @config[:modules].to_a
fb5f2bce
 			@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
71bcfce4
 				$stderr.puts e.message
def91ab1
 			end
 		end
fb5f2bce
 
71bcfce4
 		def cannot_start_fetch_process_sleep_cycle?
 			if @config[:repeat].to_i < 1
 				return "Repeat cycle is too short"
 			elsif @running_mods.empty?
 				return "There are no enabled modules"
fb5f2bce
 			end
 
71bcfce4
 			false
 		end
 
 		def start_fetch_process_sleep_cycle
fb5f2bce
 			while true
 				current_time = DateTime.now()
 				@running_mods.each do |mod|
 					result = mod.results
 					notify = mod.notify?
 				end
 				sleep @config[:repeat]
 			end
 		end
def91ab1
 	end
271b1a48
 end