modern_times 0.2.1 → 0.2.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- data/VERSION +1 -1
- data/lib/modern_times/manager.rb +40 -2
- data/lib/modern_times/railsable.rb +2 -0
- metadata +2 -2
data/VERSION
CHANGED
@@ -1 +1 @@
|
|
1
|
-
0.2.
|
1
|
+
0.2.2
|
data/lib/modern_times/manager.rb
CHANGED
@@ -1,8 +1,9 @@
|
|
1
1
|
require 'yaml'
|
2
|
+
require 'socket'
|
2
3
|
|
3
4
|
module ModernTimes
|
4
5
|
class Manager
|
5
|
-
attr_accessor :allowed_workers
|
6
|
+
attr_accessor :allowed_workers, :dummy_host
|
6
7
|
attr_reader :supervisors
|
7
8
|
|
8
9
|
def initialize(config={})
|
@@ -14,6 +15,7 @@ module ModernTimes
|
|
14
15
|
bean = ManagerMBean.new(@domain, self)
|
15
16
|
@jmx_server.register_mbean(bean, ModernTimes.manager_mbean_object_name(@domain))
|
16
17
|
self.persist_file = config[:persist_file]
|
18
|
+
self.worker_file = config[:worker_file]
|
17
19
|
end
|
18
20
|
|
19
21
|
def add(worker_klass, num_workers, worker_options={})
|
@@ -29,6 +31,7 @@ module ModernTimes
|
|
29
31
|
raise ModernTimes::Exception, "Error: #{worker_klass.name} is not an allowed worker"
|
30
32
|
end
|
31
33
|
supervisor = worker_klass.create_supervisor(self, worker_options)
|
34
|
+
raise ModernTimes::Exception "A supervisor with name #{supervisor.name} already exists" if find_supervisor(supervisor.name)
|
32
35
|
mbean = supervisor.create_mbean(@domain)
|
33
36
|
@supervisors << supervisor
|
34
37
|
supervisor.worker_count = num_workers
|
@@ -70,7 +73,6 @@ module ModernTimes
|
|
70
73
|
end
|
71
74
|
|
72
75
|
def persist_file=(file)
|
73
|
-
@persist_file = file
|
74
76
|
return unless file
|
75
77
|
@persist_file = file
|
76
78
|
if File.exist?(file)
|
@@ -99,5 +101,41 @@ module ModernTimes
|
|
99
101
|
YAML.dump(hash, out )
|
100
102
|
end
|
101
103
|
end
|
104
|
+
|
105
|
+
def find_supervisor(name)
|
106
|
+
@supervisors.each do |supervisor|
|
107
|
+
return supervisor if supervisor.name == name
|
108
|
+
end
|
109
|
+
return nil
|
110
|
+
end
|
111
|
+
|
112
|
+
def worker_file=(file)
|
113
|
+
return unless file
|
114
|
+
@worker_file = file
|
115
|
+
if File.exist?(file)
|
116
|
+
hash = YAML.load_file(file)
|
117
|
+
host = @dummy_host || Socket.gethostname
|
118
|
+
host.sub!(/\..*/, '')
|
119
|
+
config = hash[host]
|
120
|
+
return unless config
|
121
|
+
# Don't save new states if the user never dynamically updates the workers
|
122
|
+
# Then they can solely define the workers via this file and updates to the counts won't be ignored.
|
123
|
+
save_persist_file = @persist_file
|
124
|
+
@persist_file = nil unless File.exist?(@persist_file)
|
125
|
+
begin
|
126
|
+
config.each do |worker_name, worker_hash|
|
127
|
+
# Don't add if persist_file already created this supervisor
|
128
|
+
next if find_supervisor(worker_name)
|
129
|
+
klass = worker_hash[:klass] || "#{worker_name}Worker"
|
130
|
+
count = worker_hash[:count]
|
131
|
+
options = worker_hash[:options] || {}
|
132
|
+
options[:name] = worker_name
|
133
|
+
add(klass, count, options)
|
134
|
+
end
|
135
|
+
ensure
|
136
|
+
@persist_file = save_persist_file
|
137
|
+
end
|
138
|
+
end
|
139
|
+
end
|
102
140
|
end
|
103
141
|
end
|
@@ -56,6 +56,8 @@ module ModernTimes
|
|
56
56
|
manager.stop_on_signal
|
57
57
|
manager.allowed_workers = rails_workers
|
58
58
|
manager.persist_file = @cfg[:persist_file] || File.join(Rails.root, "log", "modern_times.persist")
|
59
|
+
manager.dummy_host = 'development' if Rails.env == 'development'
|
60
|
+
manager.worker_file = @cfg[:worker_file] || File.join(Rails.root, "config", "workers.yml")
|
59
61
|
return manager
|
60
62
|
end
|
61
63
|
|