run_rabbit_run 0.0.8 → 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -10,6 +10,7 @@ module RunRabbitRun
10
10
  @options = {
11
11
  application_path: application_path,
12
12
  pid: "#{application_path}/tmp/pids/run_rabbit_run.pid",
13
+ guid: "#{application_path}/tmp/pids/run_rabbit_run.guid",
13
14
  environment: (ENV['RAKE_ENV'] || ENV['RAILS_ENV'] || 'development'),
14
15
  log: "log/run_rabbit_run.log",
15
16
  rubbitmq: {}
@@ -35,6 +36,10 @@ module RunRabbitRun
35
36
  options[:pid] = File.expand_path(value)
36
37
  end
37
38
 
39
+ def guid value
40
+ options[:guid] = File.expand_path(value)
41
+ end
42
+
38
43
  def worker name, path, options = {}, &block
39
44
  worker = RunRabbitRun::Config::Worker.new(path, options)
40
45
  worker.instance_exec &block if block_given?
@@ -0,0 +1,43 @@
1
+ module RunRabbitRun
2
+ module Guid
3
+ extend self
4
+
5
+ def remove
6
+ File.delete(path) if File.exists?(path)
7
+ end
8
+
9
+ def guid
10
+ @guid || _guid
11
+ end
12
+
13
+ private
14
+
15
+ def save(guid)
16
+ create_directory_if_not_exists(File.dirname(path))
17
+
18
+ File.open(path, "w") {|file| file.puts(guid) }
19
+ end
20
+
21
+ def _guid
22
+ @guid = File.open(path, 'r') { |file| file.read }.strip if File.exists?(path)
23
+ unless @guid
24
+ @guid = SecureRandom.uuid
25
+ save(@guid)
26
+ end
27
+
28
+ @guid
29
+ end
30
+
31
+ def path
32
+ raise "[ERROR] please specify the guid file path" unless RunRabbitRun.config[:guid]
33
+
34
+ RunRabbitRun.config[:guid]
35
+ end
36
+
37
+ def create_directory_if_not_exists(dir)
38
+ FileUtils.mkdir_p(dir) unless File.exists?(dir)
39
+ end
40
+
41
+ end
42
+ end
43
+
@@ -2,6 +2,7 @@ require 'run_rabbit_run/workers'
2
2
  require 'run_rabbit_run/rabbitmq'
3
3
  require 'run_rabbit_run/rabbitmq/system_messages'
4
4
  require 'run_rabbit_run/pid'
5
+ require 'run_rabbit_run/guid'
5
6
  require 'run_rabbit_run/processes/master'
6
7
 
7
8
  module RunRabbitRun
@@ -63,10 +64,10 @@ module RunRabbitRun
63
64
 
64
65
  def add_worker name
65
66
  EventMachine.run do
66
- rabbitmq = RunRabbitRun::Rabbitmq::Base.new
67
67
 
68
+ rabbitmq = RunRabbitRun::Rabbitmq::Base.new
68
69
  system_messages = RunRabbitRun::Rabbitmq::SystemMessages.new(rabbitmq)
69
- system_messages.publish(:system, :master, :add_worker, { worker: name })
70
+ system_messages.publish(:system, "master.#{RunRabbitRun::Guid.guid}", :add_worker, { worker: name })
70
71
 
71
72
  EventMachine.add_timer(2) do
72
73
  rabbitmq.stop
@@ -90,6 +91,7 @@ module RunRabbitRun
90
91
  def stop
91
92
  RunRabbitRun::Processes::Signals.stop_signal(:master, master_process.pid)
92
93
  Pid.remove
94
+ Guid.remove
93
95
  end
94
96
 
95
97
  def reload
@@ -1,4 +1,5 @@
1
1
  require 'run_rabbit_run/processes/base'
2
+ require 'securerandom'
2
3
 
3
4
  module RunRabbitRun
4
5
 
@@ -16,7 +17,7 @@ module RunRabbitRun
16
17
  rabbitmq = RunRabbitRun::Rabbitmq::Base.new
17
18
  system_messages = RunRabbitRun::Rabbitmq::SystemMessages.new(rabbitmq)
18
19
 
19
- system_messages.subscribe "master.#" do | headers, payload |
20
+ system_messages.subscribe "master.#{RunRabbitRun::Guid.guid}.#" do | headers, payload |
20
21
  call_callback :on_system_message_received, payload["from"].to_sym, payload["message"].to_sym, payload["data"]
21
22
  end
22
23
 
@@ -4,9 +4,9 @@ module RunRabbitRun
4
4
  module Processes
5
5
  class Worker < Base
6
6
 
7
- def initialize name, options
8
- @options = options
9
-
7
+ def initialize name, master_guid, options
8
+ @options = options
9
+ @master_guid = master_guid
10
10
  super name
11
11
  end
12
12
 
@@ -17,22 +17,22 @@ module RunRabbitRun
17
17
  rabbitmq = RunRabbitRun::Rabbitmq::Base.new
18
18
  system_messages = RunRabbitRun::Rabbitmq::SystemMessages.new(rabbitmq)
19
19
 
20
- system_messages.publish(@name, :master, :process_started, { pid: Process.pid } )
20
+ system_messages.publish(@name, "master.#{@master_guid}", :process_started, { pid: Process.pid } )
21
21
 
22
22
  if @options[:processes] == 0
23
23
  add_timer 1 do
24
24
  rabbitmq.instance_eval File.read(@options[:path]), @options[:path]
25
25
 
26
- system_messages.publish(@name, :master, :process_quit)
26
+ system_messages.publish(@name, "master.#{@master_guid}", :process_quit)
27
27
  rabbitmq.stop
28
28
  end
29
29
  else
30
30
 
31
31
  rabbitmq.on_message_received do | queue |
32
- system_messages.publish(@name, :master, :message_received, { queue: queue.name } ) if @options[:log_to_master]
32
+ system_messages.publish(@name, "master.#{@master_guid}", :message_received, { queue: queue.name } ) if @options[:log_to_master]
33
33
  end
34
34
  rabbitmq.on_message_processed do | queue |
35
- system_messages.publish(@name, :master, :message_processed, { queue: queue.name } ) if @options[:log_to_master]
35
+ system_messages.publish(@name, "master.#{@master_guid}", :message_processed, { queue: queue.name } ) if @options[:log_to_master]
36
36
  end
37
37
  rabbitmq.on_error do | queue, e |
38
38
  RunRabbitRun.logger.error "[#{@name}] #{e.message} \n#{e.backtrace.join("\n")}"
@@ -53,7 +53,7 @@ module RunRabbitRun
53
53
  end
54
54
 
55
55
  before_exit do
56
- system_messages.publish(@name, :master, :process_quit)
56
+ system_messages.publish(@name, "master.#{@master_guid}", :process_quit)
57
57
  rabbitmq.stop
58
58
  end
59
59
  end
@@ -43,15 +43,16 @@ namespace :rrr do
43
43
  RunRabbitRun::Master.remove_worker(args[:worker_name])
44
44
  end
45
45
 
46
- task :new, [ :worker_name, :worker_guid ] => [ :config ] do | t, args |
46
+ task :new, [ :worker_name, :worker_guid, :master_guid ] => [ :config ] do | t, args |
47
47
  raise 'Please specify worker name' unless args[:worker_name]
48
48
  raise 'Please specify worker guid' unless args[:worker_guid]
49
+ raise 'Please specify worker guid' unless args[:master_guid]
49
50
 
50
51
  require 'run_rabbit_run/processes/worker'
51
52
 
52
53
  options = RunRabbitRun.config[:workers][args[:worker_name].to_sym]
53
54
 
54
- RunRabbitRun::Processes::Worker.new(args[:worker_guid], options).start
55
+ RunRabbitRun::Processes::Worker.new(args[:worker_guid], args[:master_guid], options).start
55
56
  end
56
57
  end
57
58
 
@@ -1,3 +1,3 @@
1
1
  module RunRabbitRun
2
- VERSION = "0.0.8"
2
+ VERSION = "0.1.0"
3
3
  end
@@ -11,7 +11,7 @@ module RunRabbitRun
11
11
 
12
12
  def run
13
13
  #TODO get path for bundle
14
- success = system("RAKE_ENV=#{RunRabbitRun.config[:environment]} bundle exec rake rrr:worker:new[#{@name},#{@guid}]")
14
+ success = system("RAKE_ENV=#{RunRabbitRun.config[:environment]} bundle exec rake rrr:worker:new[#{@name},#{@guid},#{RunRabbitRun::Guid.guid}]")
15
15
  end
16
16
 
17
17
  def kill
@@ -2,6 +2,7 @@ module RunRabbitRun
2
2
  require 'run_rabbit_run/worker'
3
3
 
4
4
  class Workers
5
+
5
6
  def initialize
6
7
  @workers = {}
7
8
  end
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.8
4
+ version: 0.1.0
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-04-24 00:00:00.000000000 Z
12
+ date: 2013-04-30 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: rake
@@ -107,6 +107,7 @@ files:
107
107
  - lib/run_rabbit_run/callbacks.rb
108
108
  - lib/run_rabbit_run/config.rb
109
109
  - lib/run_rabbit_run/config/worker.rb
110
+ - lib/run_rabbit_run/guid.rb
110
111
  - lib/run_rabbit_run/master.rb
111
112
  - lib/run_rabbit_run/pid.rb
112
113
  - lib/run_rabbit_run/processes/base.rb