run_rabbit_run 0.0.5 → 0.0.6
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/README.md +1 -3
- data/lib/run_rabbit_run/config.rb +10 -1
- data/lib/run_rabbit_run/config/worker.rb +3 -3
- data/lib/run_rabbit_run/master.rb +18 -17
- data/lib/run_rabbit_run/processes/base.rb +0 -1
- data/lib/run_rabbit_run/processes/master.rb +5 -2
- data/lib/run_rabbit_run/processes/worker.rb +35 -12
- data/lib/run_rabbit_run/rabbitmq.rb +24 -2
- data/lib/run_rabbit_run/rabbitmq/system_messages.rb +7 -14
- data/lib/run_rabbit_run/version.rb +1 -1
- data/run_rabbit_run.gemspec +1 -0
- metadata +18 -2
data/README.md
CHANGED
@@ -96,7 +96,7 @@ subscribe(test_queue) do | header, data |
|
|
96
96
|
end
|
97
97
|
```
|
98
98
|
|
99
|
-
Sometimes we need to have workers which does something and shuts down. For example send some messages and end the process. It is possible if you set `processes` to **
|
99
|
+
Sometimes we need to have workers which does something and shuts down. For example send some messages and end the process. It is possible if you set `processes` to **0** for the worker in config file. If the `processes` count is set to number bigger than **0** then master will run the process again after it finishes.
|
100
100
|
|
101
101
|
__config/rrr.rb__
|
102
102
|
|
@@ -111,8 +111,6 @@ __workers/worker_name_1.rb__
|
|
111
111
|
test_queue = channel.queue('test_queue', auto_delete: false)
|
112
112
|
|
113
113
|
publish(test_queue, {some: 'data'})
|
114
|
-
|
115
|
-
stop
|
116
114
|
```
|
117
115
|
|
118
116
|
## Contributing
|
@@ -17,6 +17,8 @@ module RunRabbitRun
|
|
17
17
|
load_from_file "#{application_path}/config/rrr.rb"
|
18
18
|
load_from_file "#{application_path}/config/rrr/#{@options[:environment]}.rb"
|
19
19
|
|
20
|
+
check_run_statement
|
21
|
+
|
20
22
|
options
|
21
23
|
end
|
22
24
|
|
@@ -41,7 +43,7 @@ module RunRabbitRun
|
|
41
43
|
end
|
42
44
|
|
43
45
|
def run *workers
|
44
|
-
options[:run] = workers
|
46
|
+
options[:run] = workers
|
45
47
|
end
|
46
48
|
|
47
49
|
private
|
@@ -50,5 +52,12 @@ module RunRabbitRun
|
|
50
52
|
instance_eval File.read(path), path
|
51
53
|
end
|
52
54
|
|
55
|
+
def check_run_statement
|
56
|
+
diff = options[:run] - options[:workers].keys
|
57
|
+
if diff.size > 0
|
58
|
+
raise "Configuration error, run statement contains not definded worker name [#{diff.join(',')}]"
|
59
|
+
end
|
60
|
+
end
|
61
|
+
|
53
62
|
end
|
54
63
|
end
|
@@ -2,11 +2,11 @@ module RunRabbitRun
|
|
2
2
|
module Config
|
3
3
|
class Worker
|
4
4
|
attr_accessor :options
|
5
|
-
|
5
|
+
|
6
6
|
def initialize(path, options = {})
|
7
7
|
@options = {
|
8
8
|
path: File.expand_path(path),
|
9
|
-
log_to_master:
|
9
|
+
log_to_master: false,
|
10
10
|
processes: 1,
|
11
11
|
}.merge(options)
|
12
12
|
|
@@ -18,7 +18,7 @@ module RunRabbitRun
|
|
18
18
|
def options
|
19
19
|
@options ||= {}
|
20
20
|
end
|
21
|
-
|
21
|
+
|
22
22
|
def name value
|
23
23
|
options[:name] = value
|
24
24
|
end
|
@@ -20,23 +20,6 @@ module RunRabbitRun
|
|
20
20
|
def start
|
21
21
|
master_process.start do
|
22
22
|
workers = RunRabbitRun::Workers.new
|
23
|
-
workers.start
|
24
|
-
|
25
|
-
add_periodic_timer 5 do
|
26
|
-
begin
|
27
|
-
workers.check unless exiting? || starting?
|
28
|
-
rescue => e
|
29
|
-
RunRabbitRun.logger.error e.message
|
30
|
-
end
|
31
|
-
end
|
32
|
-
|
33
|
-
before_exit do
|
34
|
-
workers.stop
|
35
|
-
end
|
36
|
-
|
37
|
-
before_reload do
|
38
|
-
workers.reload
|
39
|
-
end
|
40
23
|
|
41
24
|
on_system_message_received do | from, message, data |
|
42
25
|
begin
|
@@ -55,6 +38,24 @@ module RunRabbitRun
|
|
55
38
|
end
|
56
39
|
end
|
57
40
|
|
41
|
+
workers.start
|
42
|
+
|
43
|
+
add_periodic_timer 5 do
|
44
|
+
begin
|
45
|
+
workers.check unless exiting? || starting?
|
46
|
+
rescue => e
|
47
|
+
RunRabbitRun.logger.error e.message
|
48
|
+
end
|
49
|
+
end
|
50
|
+
|
51
|
+
before_exit do
|
52
|
+
workers.stop
|
53
|
+
end
|
54
|
+
|
55
|
+
before_reload do
|
56
|
+
workers.reload
|
57
|
+
end
|
58
|
+
|
58
59
|
end
|
59
60
|
|
60
61
|
Pid.save(master_process.pid)
|
@@ -13,8 +13,6 @@ module RunRabbitRun
|
|
13
13
|
|
14
14
|
def start &block
|
15
15
|
super do
|
16
|
-
instance_exec &block
|
17
|
-
|
18
16
|
rabbitmq = RunRabbitRun::Rabbitmq::Base.new
|
19
17
|
system_messages = RunRabbitRun::Rabbitmq::SystemMessages.new(rabbitmq)
|
20
18
|
|
@@ -25,6 +23,11 @@ module RunRabbitRun
|
|
25
23
|
before_exit do
|
26
24
|
rabbitmq.stop
|
27
25
|
end
|
26
|
+
|
27
|
+
add_timer 1 do
|
28
|
+
instance_exec &block
|
29
|
+
end
|
30
|
+
|
28
31
|
end
|
29
32
|
end
|
30
33
|
end
|
@@ -19,20 +19,43 @@ module RunRabbitRun
|
|
19
19
|
|
20
20
|
system_messages.publish(@name, :master, :process_started, { pid: Process.pid } )
|
21
21
|
|
22
|
-
|
23
|
-
|
24
|
-
|
25
|
-
rabbitmq.on_message_processed do | queue |
|
26
|
-
system_messages.publish(@name, :master, :message_processed, { queue: queue.name } ) if @options[:log_to_master]
|
27
|
-
end
|
22
|
+
if @options[:processes] == 0
|
23
|
+
add_timer 1 do
|
24
|
+
rabbitmq.instance_eval File.read(@options[:path]), @options[:path]
|
28
25
|
|
29
|
-
|
30
|
-
|
31
|
-
|
26
|
+
system_messages.publish(@name, :master, :process_quit)
|
27
|
+
rabbitmq.stop
|
28
|
+
end
|
29
|
+
else
|
30
|
+
|
31
|
+
rabbitmq.on_message_received do | queue |
|
32
|
+
system_messages.publish(@name, :master, :message_received, { queue: queue.name } ) if @options[:log_to_master]
|
33
|
+
end
|
34
|
+
rabbitmq.on_message_processed do | queue |
|
35
|
+
system_messages.publish(@name, :master, :message_processed, { queue: queue.name } ) if @options[:log_to_master]
|
36
|
+
end
|
37
|
+
rabbitmq.on_error do | queue, e |
|
38
|
+
RunRabbitRun.logger.error "[#{@name}] #{e.message} \n#{e.backtrace.join("\n")}"
|
39
|
+
#system_messages.publish( @name, :master, :error,
|
40
|
+
# {
|
41
|
+
# queue: queue.name,
|
42
|
+
# exception: { message: e.message, backtrace: e.backtrace }
|
43
|
+
# }
|
44
|
+
#) if @options[:log_to_master]
|
45
|
+
end
|
46
|
+
|
47
|
+
add_timer 1 do
|
48
|
+
begin
|
49
|
+
rabbitmq.instance_eval File.read(@options[:path]), @options[:path]
|
50
|
+
rescue => e
|
51
|
+
RunRabbitRun.logger.error "[#{@name}] #{e.message} \n#{e.backtrace.join("\n")}"
|
52
|
+
end
|
53
|
+
end
|
32
54
|
|
33
|
-
|
34
|
-
|
35
|
-
|
55
|
+
before_exit do
|
56
|
+
system_messages.publish(@name, :master, :process_quit)
|
57
|
+
rabbitmq.stop
|
58
|
+
end
|
36
59
|
end
|
37
60
|
end
|
38
61
|
end
|
@@ -7,6 +7,7 @@ module RunRabbitRun
|
|
7
7
|
|
8
8
|
define_callback :on_message_received
|
9
9
|
define_callback :on_message_processed
|
10
|
+
define_callback :on_error
|
10
11
|
|
11
12
|
def subscribe(queue, options = {}, &block)
|
12
13
|
opts = options.dup
|
@@ -16,7 +17,11 @@ module RunRabbitRun
|
|
16
17
|
RunRabbitRun.logger.info "[#{queue.name}] [#{Time.now.to_f}] started" if time_logging
|
17
18
|
call_callback :on_message_received, queue
|
18
19
|
|
19
|
-
|
20
|
+
begin
|
21
|
+
instance_exec(header, JSON.parse(payload), &block)
|
22
|
+
rescue => e
|
23
|
+
call_callback :on_error, queue, e
|
24
|
+
end
|
20
25
|
|
21
26
|
call_callback :on_message_processed, queue
|
22
27
|
RunRabbitRun.logger.info "[#{queue.name}] [#{Time.now.to_f}] finished" if time_logging
|
@@ -24,7 +29,22 @@ module RunRabbitRun
|
|
24
29
|
end
|
25
30
|
|
26
31
|
def publish(queue, data)
|
27
|
-
exchange.publish JSON.generate(data), :
|
32
|
+
exchange.publish JSON.generate(data), routing_key: queue.name
|
33
|
+
end
|
34
|
+
|
35
|
+
def publish_sync(queue, data)
|
36
|
+
bunny.exchange('').publish(JSON.generate(data), key: queue.name)
|
37
|
+
end
|
38
|
+
|
39
|
+
def bunny
|
40
|
+
@bunny ||= begin
|
41
|
+
require 'bunny'
|
42
|
+
|
43
|
+
bunny = Bunny.new
|
44
|
+
bunny.start
|
45
|
+
|
46
|
+
bunny
|
47
|
+
end
|
28
48
|
end
|
29
49
|
|
30
50
|
def connection
|
@@ -40,6 +60,8 @@ module RunRabbitRun
|
|
40
60
|
end
|
41
61
|
|
42
62
|
def stop
|
63
|
+
bunny.stop if @bunny
|
64
|
+
|
43
65
|
connection.close {
|
44
66
|
EventMachine.stop { exit }
|
45
67
|
}
|
@@ -2,18 +2,21 @@ module RunRabbitRun
|
|
2
2
|
module Rabbitmq
|
3
3
|
class SystemMessages
|
4
4
|
def initialize(rabbitmq)
|
5
|
-
@rabbitmq
|
5
|
+
@rabbitmq = rabbitmq
|
6
|
+
@system_queue = @rabbitmq.channel.queue("profile.#{RunRabbitRun.config[:environment]}", durable: true, auto_delete: false)
|
7
|
+
@system_exchange = @rabbitmq.channel.topic("runrabbitrun.system", durable: true)
|
6
8
|
end
|
7
9
|
|
8
10
|
def subscribe routing_key, &block
|
9
|
-
system_queue.
|
10
|
-
|
11
|
+
@system_queue.
|
12
|
+
bind(@system_exchange, routing_key: "profile.#{RunRabbitRun.config[:environment]}.#{routing_key}").
|
13
|
+
subscribe do | headers, payload |
|
11
14
|
block.call(headers, JSON.parse(payload))
|
12
15
|
end
|
13
16
|
end
|
14
17
|
|
15
18
|
def publish from, target, message, data = {}
|
16
|
-
system_exchange.publish(
|
19
|
+
@system_exchange.publish(
|
17
20
|
JSON.generate({
|
18
21
|
from: from,
|
19
22
|
message: message,
|
@@ -22,16 +25,6 @@ module RunRabbitRun
|
|
22
25
|
routing_key: "profile.#{RunRabbitRun.config[:environment]}.#{target}")
|
23
26
|
end
|
24
27
|
|
25
|
-
private
|
26
|
-
|
27
|
-
def system_queue
|
28
|
-
@system_queue ||= @rabbitmq.channel.queue("profile.#{RunRabbitRun.config[:environment]}", auto_delete: false)
|
29
|
-
end
|
30
|
-
|
31
|
-
def system_exchange
|
32
|
-
@system_exchange ||= @rabbitmq.channel.topic("runrabbitrun.system", durable: true)
|
33
|
-
end
|
34
|
-
|
35
28
|
end
|
36
29
|
end
|
37
30
|
end
|
data/run_rabbit_run.gemspec
CHANGED
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: run_rabbit_run
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0.
|
4
|
+
version: 0.0.6
|
5
5
|
prerelease:
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
@@ -9,7 +9,7 @@ authors:
|
|
9
9
|
autorequire:
|
10
10
|
bindir: bin
|
11
11
|
cert_chain: []
|
12
|
-
date: 2013-
|
12
|
+
date: 2013-04-04 00:00:00.000000000 Z
|
13
13
|
dependencies:
|
14
14
|
- !ruby/object:Gem::Dependency
|
15
15
|
name: rake
|
@@ -59,6 +59,22 @@ dependencies:
|
|
59
59
|
- - ! '>='
|
60
60
|
- !ruby/object:Gem::Version
|
61
61
|
version: '0'
|
62
|
+
- !ruby/object:Gem::Dependency
|
63
|
+
name: bunny
|
64
|
+
requirement: !ruby/object:Gem::Requirement
|
65
|
+
none: false
|
66
|
+
requirements:
|
67
|
+
- - ! '>='
|
68
|
+
- !ruby/object:Gem::Version
|
69
|
+
version: '0'
|
70
|
+
type: :runtime
|
71
|
+
prerelease: false
|
72
|
+
version_requirements: !ruby/object:Gem::Requirement
|
73
|
+
none: false
|
74
|
+
requirements:
|
75
|
+
- - ! '>='
|
76
|
+
- !ruby/object:Gem::Version
|
77
|
+
version: '0'
|
62
78
|
- !ruby/object:Gem::Dependency
|
63
79
|
name: pry
|
64
80
|
requirement: !ruby/object:Gem::Requirement
|