Browse code

Implement fetch - process - sleep lifecycle

Cinan Rakosnik authored on 16/12/2012 at 00:48:59
Showing 1 changed files
... ...
@@ -3,6 +3,8 @@ require_relative "rubysermon/mod_loader"
3 3
 require_relative "rubysermon/mod_template"
4 4
 require_relative "rubysermon/configurator"
5 5
 
6
+require "date"
7
+
6 8
 module Rubysermon
7 9
 	LIB_PATH = File.expand_path(File.dirname(__FILE__))
8 10
 	APP_PATH = "#{LIB_PATH}/.."
... ...
@@ -10,10 +12,17 @@ module Rubysermon
10 10
 
11 11
 	class App
12 12
 
13
+		#todo: refactor
13 14
 		def initialize
14
-			@config = ""
15
-			@loader = nil
15
+			@config = {repeat: 60}
16
+			@running_mods = []
16 17
 			@config_path = "#{APP_PATH}/config.json"
18
+
19
+			config()
20
+			@enabled_mods = @config[:modules].to_a
21
+
22
+			start_mods()
23
+			start_fetch_process_sleep_cycle()
17 24
 		end
18 25
 
19 26
 		private
... ...
@@ -23,12 +32,36 @@ module Rubysermon
23 23
 			@config = configurator.get_settings()
24 24
 		end
25 25
 
26
+		def start_mods
27
+			@enabled_mods.each do |mod| load_mod(mod) end
28
+		end
29
+
26 30
 		def load_mod(mod_name)
27 31
 			begin
28
-				@loader = ModLoader.load(mod_name)
32
+				mod = ModLoader.load(mod_name)
33
+				@running_mods.push(mod)
29 34
 			rescue ModLoaderException => e
30 35
 				$stderr = e.message
31 36
 			end
32 37
 		end
38
+
39
+		def start_fetch_process_sleep_cycle
40
+			if not can_start_fetch_process_sleep_cycle?
41
+				abort()
42
+			end
43
+
44
+			while true
45
+				current_time = DateTime.now()
46
+				@running_mods.each do |mod|
47
+					result = mod.results
48
+					notify = mod.notify?
49
+				end
50
+				sleep @config[:repeat]
51
+			end
52
+		end
53
+
54
+		def can_start_fetch_process_sleep_cycle?
55
+			@config[:repeat].to_i < 1
56
+		end
33 57
 	end
34 58
 end