run_rabbit_run 0.0.4 → 0.0.5

Sign up to get free protection for your applications and to get access to all the features.
data/README.md CHANGED
@@ -18,7 +18,102 @@ Or install it yourself as:
18
18
 
19
19
  ## Usage
20
20
 
21
- TODO: Write usage instructions here
21
+
22
+ ### Including the rake tasks
23
+
24
+ Require the rake tasks in your **Rakefile**
25
+
26
+ require "run_rabbit_run/tasks"
27
+
28
+ #### Rake tasks
29
+
30
+ ```console
31
+ RAKE_ENV=production bundle exec rake rrr:start
32
+ ```
33
+
34
+ Starts the master process and it starts all worker processes for **production** environment (default is **development**).
35
+
36
+ ```console
37
+ bundle exec rake rrr:stop
38
+ ```
39
+
40
+ Stops the master process. Master process sends **QUIT** signal to workers and waits for 5 seconds. If processes are still running the master kills them.
41
+
42
+ ```console
43
+ bundle exec rake rrr:reload
44
+ ```
45
+
46
+ Master stops all workers and then starts the profile again.
47
+
48
+ ```console
49
+ bundle exec rake rrr:worker:add[worker_name]
50
+ ```
51
+
52
+ Master runs new process for given worker.
53
+
54
+ ```console
55
+ bundle exec rake rrr:worker:remove[worker_name]
56
+ ```
57
+
58
+ Master stops one worker process.
59
+
60
+ ### Configuration
61
+
62
+ you need to create __config/rrr.rb__ file to set your config variables
63
+
64
+ ```ruby
65
+ log "log/run_rabbit_run.log"
66
+ pid "tmp/pids/run_rabbit_run.pid"
67
+
68
+ worker :worker1, 'workers/worker_name_1.rb', processes: 0
69
+ worker :worker2, 'workers/worker_name_2.rb', processes: 2
70
+ worker :worker3, 'workers/worker_name_3.rb', processes: 1
71
+ ```
72
+
73
+ * **log** sets path to log file
74
+ * **pid** sets path to pid file
75
+ * **worker** sets settings for worker. The first argument is the worker name. The second argument is the path to the worker ruby file. And you can pass options as a third parameter. The **processes** option sets the process count for worker, default is 1. If process count is 0 then no processes will be run, you can run the process by `bundle exec rake rrr:worker:add[worker_name]`.
76
+
77
+ Another config file you need to create is configuration file for environment eg. **config/rrr/development.rb** or **config/rrr/production.rb**
78
+
79
+ ```ruby
80
+ run :worker1, :worker2
81
+
82
+ ```
83
+ **run** points the workers which will be run for the environment. In this case it will run only `worker1` and `worker2` but exlude the `worker3`.
84
+
85
+ ### Creating worker
86
+
87
+ This is the "Hello world" worker. It creates **test_queue**, sends simple message to this queue, subscribes to the same queue and prints it into log file.
88
+
89
+ ```ruby
90
+ test_queue = channel.queue('test_queue', auto_delete: false)
91
+
92
+ publish(test_queue, {some: 'data'})
93
+
94
+ subscribe(test_queue) do | header, data |
95
+ RunRabbitRun.logger.info data.inspect
96
+ end
97
+ ```
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 **zero** for the worker in config file and run `stop` command at the end of the worker script file. If the `processes` count is set to number bigger than **0** then master will run the process again after it finishes.
100
+
101
+ __config/rrr.rb__
102
+
103
+ ```ruby
104
+ # ... some code
105
+ worker :worker1, 'workers/worker_name_1.rb', processes: 0
106
+ # ... some code
107
+ ```
108
+ __workers/worker_name_1.rb__
109
+
110
+ ```ruby
111
+ test_queue = channel.queue('test_queue', auto_delete: false)
112
+
113
+ publish(test_queue, {some: 'data'})
114
+
115
+ stop
116
+ ```
22
117
 
23
118
  ## Contributing
24
119
 
@@ -12,19 +12,19 @@ module RunRabbitRun
12
12
  @master_process ||= begin
13
13
  master = RunRabbitRun::Processes::Master.new
14
14
  master.pid = Pid.pid
15
-
15
+
16
16
  master
17
17
  end
18
18
  end
19
-
19
+
20
20
  def start
21
21
  master_process.start do
22
22
  workers = RunRabbitRun::Workers.new
23
23
  workers.start
24
24
 
25
- add_periodic_timer 2 do
25
+ add_periodic_timer 5 do
26
26
  begin
27
- workers.check unless exiting?
27
+ workers.check unless exiting? || starting?
28
28
  rescue => e
29
29
  RunRabbitRun.logger.error e.message
30
30
  end
@@ -49,6 +49,8 @@ module RunRabbitRun
49
49
  end
50
50
  end
51
51
  end
52
+
53
+ @starting = false
52
54
  end
53
55
 
54
56
  RunRabbitRun.logger.info "[#{@name}] process finished"
@@ -58,6 +60,10 @@ module RunRabbitRun
58
60
 
59
61
  end
60
62
 
63
+ def starting?
64
+ @starting ||= true
65
+ end
66
+
61
67
  def exiting?
62
68
  @exiting ||= false
63
69
  end
@@ -4,7 +4,7 @@ module RunRabbitRun
4
4
  module Rabbitmq
5
5
  class Base
6
6
  include RunRabbitRun::Callbacks
7
-
7
+
8
8
  define_callback :on_message_received
9
9
  define_callback :on_message_processed
10
10
 
@@ -12,7 +12,7 @@ module RunRabbitRun
12
12
  opts = options.dup
13
13
  time_logging = opts.delete(:time_logging) || false
14
14
 
15
- queue.subscribe do | header, payload |
15
+ queue.subscribe(options) do | header, payload |
16
16
  RunRabbitRun.logger.info "[#{queue.name}] [#{Time.now.to_f}] started" if time_logging
17
17
  call_callback :on_message_received, queue
18
18
 
@@ -1,3 +1,3 @@
1
1
  module RunRabbitRun
2
- VERSION = "0.0.4"
2
+ VERSION = "0.0.5"
3
3
  end
@@ -19,15 +19,15 @@ module RunRabbitRun
19
19
  def check
20
20
  @workers.each do | name, workers |
21
21
  processes = RunRabbitRun.config[:workers][name][:processes]
22
-
22
+
23
23
  workers.each do | guid, worker |
24
- unless worker.running?
24
+ if worker.pid && worker.pid > 0 && !worker.running?
25
25
  if processes == 0
26
26
  @workers[name].delete(guid)
27
27
  else
28
28
  worker.run
29
29
  end
30
- end
30
+ end
31
31
  end
32
32
  diff = processes - workers.size
33
33
  diff.times{ run_new_worker name } if diff > 0
@@ -73,7 +73,7 @@ module RunRabbitRun
73
73
  def stop
74
74
  # try to stop gracefully
75
75
  @workers.each { | name, workers | workers.each { | guid, worker | worker.stop } }
76
-
76
+
77
77
  sleep 1
78
78
 
79
79
  @workers.each do | name, workers |
@@ -1,5 +1,3 @@
1
- require 'run_rabbit_run'
2
-
3
1
  #TODO working_directory '/path/to/working/directory'
4
2
 
5
3
  log "log/run_rabbit_run.log"
@@ -1,5 +1,5 @@
1
- output = channel.queue('output', auto_delete: false)
2
- input = channel.queue('input', auto_delete: false)
1
+ output = channel.queue('output', durable: true, auto_delete: false)
2
+ input = channel.queue('input', durable: true, auto_delete: false)
3
3
 
4
4
  publish(output, {some: 'data'})
5
5
 
@@ -8,6 +8,7 @@ subscribe(output, time_logging: true) do | header, data |
8
8
  publish(input, {received: 'data'})
9
9
  end
10
10
 
11
- subscribe(input) do | header, data |
11
+ subscribe(input, ack: true) do | header, data |
12
12
  RunRabbitRun.logger.info data.inspect
13
+ header.ack
13
14
  end
@@ -1,5 +1,7 @@
1
- RunRabbitRun.logger.info "run worker 3 for 20 seconds"
1
+ input = channel.queue('input', durable: true, auto_delete: false)
2
2
 
3
- sleep 20
3
+ 10.times do | index |
4
+ publish(input, { some: 'zero data' })
5
+ end
4
6
 
5
7
  stop
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
4
+ version: 0.0.5
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-03-21 00:00:00.000000000 Z
12
+ date: 2013-03-26 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: rake