queue_map 0.1 → 0.2

Sign up to get free protection for your applications and to get access to all the features.
@@ -1,39 +1,79 @@
1
1
  #!/usr/bin/env ruby
2
2
 
3
3
  require 'optparse'
4
- options = {}
4
+ @options = {}
5
5
  opt_p = OptionParser.new do |opts|
6
- opts.banner = "Usage: script/export_sales.rb [options] path/to/consumer.rb"
6
+ opts.banner = "Usage: queue_map_consumer [options] [ start | stop | restart ] path/to/consumer.rb"
7
7
  opts.separator "Options:"
8
8
  opts.on('-f', '--foreground', "Run appraiser in the foreground") do
9
- options[:foreground] = true
9
+ @options[:foreground] = true
10
10
  end
11
11
  opts.on_tail("-h", "--help", "Show this message")
12
12
  end
13
13
  opt_p.parse!
14
- consumer_path = ARGV[0]
14
+ action, consumer_path = ARGV
15
15
  if consumer_path.nil?
16
16
  puts opt_p
17
17
  exit 1
18
18
  end
19
19
 
20
20
  require "rubygems"
21
+ require "background_process"
21
22
  require File.dirname(__FILE__) + "/../lib/queue_map"
22
23
  require File.dirname(__FILE__) + "/../lib/queue_map/consumer"
23
24
 
24
- consumer = QueueMap::Consumer.from_file(consumer_path, :strategy => :fork)
25
-
26
- unless options[:foreground]
27
- # daemonize
28
- puts "Daemonizing"
29
- pid = Kernel.fork
30
- if pid
31
- Process.detach(pid)
32
- puts "Daemonized as #{pid}"
33
- exit 0
34
- else
35
- Signal.trap('HUP', 'IGNORE') # Don't die upon logout
25
+ @consumer = QueueMap::Consumer.from_file(consumer_path, :strategy => :fork)
26
+ @pid_file = @consumer.pid_file
27
+
28
+ def pid
29
+ return nil unless File.exist?(@pid_file)
30
+ pid = File.read(@pid_file).to_i
31
+ begin
32
+ Process.getpgid(pid)
33
+ pid
34
+ rescue Errno::ESRCH
35
+ nil
36
+ end
37
+ end
38
+
39
+ def stop
40
+ (puts "Not running"; exit 1) unless pid
41
+ Process.kill("INT", pid)
42
+ puts "Sending INT to #{pid}"
43
+ end
44
+
45
+ def start
46
+ (puts "Already running as #{pid}"; exit 1) if pid
47
+ puts "Starting consumers..."
48
+ unless @options[:foreground]
49
+ # daemonize
50
+ puts "Daemonizing"
51
+ pid = Kernel.fork
52
+ if pid
53
+ Process.detach(pid)
54
+ puts "Daemonized as #{pid}"
55
+ File.open(@consumer.pid_file, "w") { |f| f << pid.to_s }
56
+ exit 0
57
+ else
58
+ Signal.trap('HUP', 'IGNORE') # Don't die upon logout
59
+ end
36
60
  end
61
+ @consumer.start
62
+ end
63
+
64
+ def restart
65
+ old_pid = pid
66
+ @consumer.before_fork_procs << (lambda do
67
+ Process.kill("INT", old_pid) rescue Errno::ESRCH
68
+ end)
69
+ FileUtils.rm_f(@pid_file)
70
+ start
71
+ end
72
+
73
+ case action
74
+ when "start" then start
75
+ when "stop" then stop
76
+ when "restart" then restart
77
+ else
78
+ puts "Unknown action: #{action}"
37
79
  end
38
- puts "Starting consumers..."
39
- consumer.start
@@ -1,6 +1,7 @@
1
1
  class QueueMap::Consumer
2
2
  attr_accessor :count_workers, :worker_proc, :on_exception_proc
3
- attr_reader :name
3
+ attr_reader :name, :master_pid
4
+ attr_writer :pid_file
4
5
  class Configurator
5
6
  def initialize(base)
6
7
  @base = base
@@ -12,6 +13,7 @@ class QueueMap::Consumer
12
13
  def worker(&block); @base.worker_proc = block; end
13
14
  def on_exception(&block); @base.on_exception_proc = block; end
14
15
  def count_workers(value); @base.count_workers = value; end
16
+ def pid_file(value); @base.pid_file = value; end
15
17
  end
16
18
 
17
19
 
@@ -53,6 +55,10 @@ class QueueMap::Consumer
53
55
  def between_responses_procs
54
56
  @between_responses_procs ||= []
55
57
  end
58
+
59
+ def pid_file
60
+ "#{name}_consumer.pid"
61
+ end
56
62
 
57
63
  def start
58
64
  raise RuntimeError, "Called start on Consumer without strategy"
@@ -134,6 +140,7 @@ class QueueMap::Consumer
134
140
  end
135
141
 
136
142
  def stop(graceful = true)
143
+ $0 = $0 + " [shutting down]"
137
144
  @child_pids.each do |pid|
138
145
  begin
139
146
  Process.kill(graceful ? "INT" : "TERM", pid)
@@ -1,3 +1,3 @@
1
1
  module QueueMap
2
- VERSION=0.1
2
+ VERSION="0.2"
3
3
  end
metadata CHANGED
@@ -1,12 +1,12 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: queue_map
3
3
  version: !ruby/object:Gem::Version
4
- hash: 9
4
+ hash: 15
5
5
  prerelease: false
6
6
  segments:
7
7
  - 0
8
- - 1
9
- version: "0.1"
8
+ - 2
9
+ version: "0.2"
10
10
  platform: ruby
11
11
  authors:
12
12
  - Tim Harper
@@ -14,7 +14,7 @@ autorequire:
14
14
  bindir: bin
15
15
  cert_chain: []
16
16
 
17
- date: 2010-10-11 00:00:00 -06:00
17
+ date: 2010-10-12 00:00:00 -06:00
18
18
  default_executable:
19
19
  dependencies:
20
20
  - !ruby/object:Gem::Dependency