arsenicum 0.3.1.2 → 0.4
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/lib/arsenicum/configuration.rb +1 -1
- data/lib/arsenicum/core/broker.rb +28 -34
- data/lib/arsenicum/core/worker.rb +45 -32
- data/lib/arsenicum/logger.rb +2 -2
- data/lib/arsenicum/main.rb +1 -1
- data/lib/arsenicum/queue.rb +5 -1
- data/lib/arsenicum/version.rb +1 -1
- metadata +1 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 9c515f579e66429e8c6c45cbc3f9dcd215b26d39
|
4
|
+
data.tar.gz: 23f075c1c83004cf9272b7cd8949da16c18ba169
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 609a092cb19169ab3f09589812b7454bcd32f8ac379ce99d4fad2c8eab01dccabbfc65176ea38785212d7f14e7c4a8ecc52fc8a053cfcec2a4d77e424c7c686e
|
7
|
+
data.tar.gz: 9cb39dbf8341c7973ab53c2659c58c344025b7f8c338c290fc8cf68fc7914b5c6a751c504e60d9a5127944c51614dfc218728dc0ce31f19c9db09f3c308c6a85
|
@@ -118,7 +118,7 @@ module Arsenicum
|
|
118
118
|
end
|
119
119
|
|
120
120
|
def build
|
121
|
-
klass.new(name, init_parameters.merge(router_class: router_class)).tap do |queue|
|
121
|
+
klass.new(name, init_parameters.merge(worker_count: worker_count, router_class: router_class)).tap do |queue|
|
122
122
|
task_configurations.each do |task_config|
|
123
123
|
queue.register task_config.build
|
124
124
|
end
|
@@ -11,7 +11,6 @@ class Arsenicum::Core::Broker
|
|
11
11
|
|
12
12
|
def initialize(options = {})
|
13
13
|
@worker_count = (options.delete(:worker_count) || PROCESSOR_COUNT_DEFAULT).to_i
|
14
|
-
@mutex = Mutex.new
|
15
14
|
@tasks = {}
|
16
15
|
@router = options.delete :router
|
17
16
|
|
@@ -19,6 +18,8 @@ class Arsenicum::Core::Broker
|
|
19
18
|
formatter = options[:formatter] || Arsenicum::Formatter.new
|
20
19
|
@worker_options = options.delete(:worker_options) || {}
|
21
20
|
@worker_options.merge! serializer: serializer, formatter: formatter
|
21
|
+
@current_worker_index = -1 # because it is incremented whenever used. (primary index should be ZERO)
|
22
|
+
@mutex = Mutex.new
|
22
23
|
end
|
23
24
|
|
24
25
|
def [](task_id)
|
@@ -32,8 +33,8 @@ class Arsenicum::Core::Broker
|
|
32
33
|
alias_method :register, :[]=
|
33
34
|
|
34
35
|
def run
|
35
|
-
@workers =
|
36
|
-
@available_workers =
|
36
|
+
@workers = []
|
37
|
+
@available_workers = []
|
37
38
|
|
38
39
|
prepare_workers
|
39
40
|
end
|
@@ -56,63 +57,56 @@ class Arsenicum::Core::Broker
|
|
56
57
|
end
|
57
58
|
|
58
59
|
def stop
|
59
|
-
workers.
|
60
|
+
workers.each(&:stop)
|
60
61
|
end
|
61
62
|
|
62
63
|
def remove(worker)
|
63
|
-
|
64
|
-
|
65
|
-
available_workers.delete(worker.pid)
|
66
|
-
end
|
64
|
+
available_workers.delete(worker)
|
65
|
+
workers.delete(worker)
|
67
66
|
end
|
68
67
|
|
69
68
|
def reload
|
70
|
-
|
69
|
+
stop
|
71
70
|
|
72
|
-
workers.clear
|
73
71
|
available_workers.clear
|
72
|
+
workers.clear
|
74
73
|
|
75
74
|
prepare_workers
|
76
75
|
end
|
77
76
|
|
78
77
|
def get_back_worker(worker)
|
79
|
-
|
80
|
-
|
81
|
-
|
82
|
-
|
83
|
-
|
84
|
-
|
85
|
-
|
86
|
-
prepare_worker next_index
|
87
|
-
end
|
88
|
-
}
|
78
|
+
if worker.active?
|
79
|
+
available_workers << worker
|
80
|
+
else
|
81
|
+
remove worker
|
82
|
+
worker.stop
|
83
|
+
prepare_worker
|
84
|
+
end
|
89
85
|
end
|
90
86
|
|
91
87
|
private
|
92
88
|
def prepare_workers
|
93
|
-
@worker_count.times
|
94
|
-
prepare_worker i
|
95
|
-
end
|
89
|
+
@worker_count.times{prepare_worker}
|
96
90
|
end
|
97
91
|
|
98
|
-
def prepare_worker
|
99
|
-
worker = Arsenicum::Core::Worker.new(self,
|
92
|
+
def prepare_worker
|
93
|
+
worker = Arsenicum::Core::Worker.new(self, next_worker_index, worker_options)
|
100
94
|
stock(worker)
|
101
95
|
end
|
102
96
|
|
97
|
+
def next_worker_index
|
98
|
+
mutex.synchronize{
|
99
|
+
@current_worker_index += 1
|
100
|
+
}
|
101
|
+
end
|
102
|
+
|
103
103
|
def stock(worker)
|
104
|
-
|
105
|
-
|
106
|
-
workers[pid] = worker
|
107
|
-
available_workers[pid] = worker
|
108
|
-
end
|
104
|
+
workers << worker
|
105
|
+
available_workers << worker
|
109
106
|
end
|
110
107
|
|
111
108
|
def next_worker
|
112
|
-
mutex.synchronize
|
113
|
-
(_, worker) = available_workers.shift
|
114
|
-
worker
|
115
|
-
end
|
109
|
+
mutex.synchronize{available_workers.shift}
|
116
110
|
end
|
117
111
|
|
118
112
|
def serialize(value = {})
|
@@ -27,28 +27,14 @@ class Arsenicum::Core::Worker
|
|
27
27
|
@thread = InvokerThread.new(self)
|
28
28
|
@work_at = :parent
|
29
29
|
@state = :parent
|
30
|
-
end
|
31
|
-
|
32
|
-
def run
|
33
|
-
(@in_parent, @out_child) = open_binary_pipes
|
34
|
-
(@in_child, @out_parent) = open_binary_pipes
|
35
|
-
(@ctrl_in_parent, @ctrl_out_child) = open_binary_pipes
|
36
|
-
(@ctrl_in_child, @ctrl_out_parent) = open_binary_pipes
|
37
|
-
|
38
|
-
@pid = fork &method(:run_in_child)
|
39
|
-
return unless @pid
|
40
30
|
|
41
|
-
|
42
|
-
[in_child, out_child, ctrl_in_child, ctrl_out_child].each(&:close)
|
43
|
-
pid
|
31
|
+
run
|
44
32
|
end
|
45
33
|
|
46
|
-
def
|
47
|
-
|
48
|
-
|
49
|
-
|
50
|
-
pipes.last.sync = true
|
51
|
-
end
|
34
|
+
def ==(another)
|
35
|
+
return false unless another.is_a? ::Arsenicum::Core::Worker
|
36
|
+
|
37
|
+
return another.pid == self.pid
|
52
38
|
end
|
53
39
|
|
54
40
|
def ask(task_id, *parameters)
|
@@ -70,13 +56,51 @@ class Arsenicum::Core::Worker
|
|
70
56
|
|
71
57
|
def stop
|
72
58
|
thread.terminate
|
73
|
-
return
|
59
|
+
return unless child_process_alive?
|
74
60
|
|
75
61
|
write_message ctrl_out_parent, COMMAND_STOP
|
76
62
|
Process.waitpid pid
|
77
63
|
end
|
78
64
|
|
65
|
+
def active?
|
66
|
+
worker_thread_alive? && child_process_alive?
|
67
|
+
end
|
68
|
+
|
69
|
+
def return_to_broker
|
70
|
+
broker.get_back_worker self
|
71
|
+
end
|
72
|
+
|
79
73
|
private
|
74
|
+
def worker_thread_alive?
|
75
|
+
thread.alive?
|
76
|
+
end
|
77
|
+
|
78
|
+
def child_process_alive?
|
79
|
+
Process.waitpid pid, Process::WNOHANG
|
80
|
+
end
|
81
|
+
|
82
|
+
def run
|
83
|
+
(@in_parent, @out_child) = open_binary_pipes
|
84
|
+
(@in_child, @out_parent) = open_binary_pipes
|
85
|
+
(@ctrl_in_parent, @ctrl_out_child) = open_binary_pipes
|
86
|
+
(@ctrl_in_child, @ctrl_out_parent) = open_binary_pipes
|
87
|
+
|
88
|
+
@pid = fork &method(:run_in_child)
|
89
|
+
return unless @pid
|
90
|
+
|
91
|
+
@active = true
|
92
|
+
[in_child, out_child, ctrl_in_child, ctrl_out_child].each(&:close)
|
93
|
+
pid
|
94
|
+
end
|
95
|
+
|
96
|
+
def open_binary_pipes
|
97
|
+
IO.pipe.each do |io|
|
98
|
+
io.set_encoding 'BINARY'
|
99
|
+
end.tap do |pipes|
|
100
|
+
pipes.last.sync = true
|
101
|
+
end
|
102
|
+
end
|
103
|
+
|
80
104
|
def run_in_child
|
81
105
|
switch_state :waiting
|
82
106
|
[in_parent, out_parent, ctrl_in_parent, ctrl_out_parent].each(&:close)
|
@@ -96,13 +120,6 @@ class Arsenicum::Core::Worker
|
|
96
120
|
end
|
97
121
|
end
|
98
122
|
|
99
|
-
def active?
|
100
|
-
case state
|
101
|
-
when :waiting, :busy
|
102
|
-
true
|
103
|
-
end
|
104
|
-
end
|
105
|
-
|
106
123
|
def switch_state(state)
|
107
124
|
@state = state
|
108
125
|
$0 = process_name
|
@@ -180,10 +197,6 @@ class Arsenicum::Core::Worker
|
|
180
197
|
"arsenicum[Worker ##{index}] - #{state}"
|
181
198
|
end
|
182
199
|
|
183
|
-
def return_to_broker
|
184
|
-
broker.get_back_worker self
|
185
|
-
end
|
186
|
-
|
187
200
|
[:debug, :info, :warn, :error, :fatal].each do |level|
|
188
201
|
eval <<-SCRIPT, binding, __FILE__, __LINE__ + 1
|
189
202
|
def #{level}(message: nil, exception: nil)
|
@@ -238,7 +251,7 @@ class Arsenicum::Core::Worker
|
|
238
251
|
failure_handler.call e
|
239
252
|
ensure
|
240
253
|
self.task_request = nil
|
241
|
-
return_to_broker
|
254
|
+
worker.return_to_broker
|
242
255
|
end
|
243
256
|
end
|
244
257
|
end
|
data/lib/arsenicum/logger.rb
CHANGED
data/lib/arsenicum/main.rb
CHANGED
data/lib/arsenicum/queue.rb
CHANGED
@@ -25,8 +25,12 @@ class Arsenicum::Queue
|
|
25
25
|
next
|
26
26
|
end
|
27
27
|
|
28
|
-
|
28
|
+
unless message
|
29
|
+
sleep(0.5)
|
30
|
+
next
|
31
|
+
end
|
29
32
|
|
33
|
+
Arsenicum::Logger.info{"Queue picked. message: #{message.inspect}"}
|
30
34
|
broker.delegate message, -> { handle_success(original_message) }, -> e { handle_failure(e, original_message) }
|
31
35
|
end
|
32
36
|
end
|
data/lib/arsenicum/version.rb
CHANGED