Class: Resque::Master

Inherits:
Object
  • Object
show all
Defined in:
lib/resque/master.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(path) ⇒ Master

Returns a new instance of Master.



65
66
67
68
# File 'lib/resque/master.rb', line 65

def initialize(path)
  @config_path = path
  @options = Master.defaults
end

Instance Attribute Details

#optionsObject (readonly)

Returns the value of attribute options.



7
8
9
# File 'lib/resque/master.rb', line 7

def options
  @options
end

Class Method Details

.daemonize(options) ⇒ Object



121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
# File 'lib/resque/master.rb', line 121

def self.daemonize(options)
  $pidfile = options[:pidfile]
  rd, wr = IO.pipe

  stdout = options[:stdout_path] || "/dev/null"
  stderr = options[:stderr_path] || "/dev/null"

  # wait for child process to start running before exiting parent process
  fork do
    rd.close
    Process.setsid
    fork do
      begin
        wr.write "Prepare to fork: #{options.inspect}, stdout: #{stdout.inspect}, stderr: #{stderr.inspect}, stdin: /dev/null"
        Process.setsid
        Dir.chdir(options[:runpath])
        File.umask 0000
        STDIN.reopen "/dev/null"
        STDOUT.reopen stdout, "a"
        STDERR.reopen stderr, "a"

        wr.flush
        wr.close # signal to our parent we're up and running, this lets the parent exit

        Resque.fork! # fork to startup the workers
      rescue => e
        if wr.closed?
          STDERR.puts "#{e.message} #{e.backtrace.join("\n")}"
        else
          wr.write e.message
          wr.write e.backtrace.join("\n")
          wr.write "\n"
          wr.write "ERROR!"
          wr.flush
          wr.close
        end
      end
    end
    wr.close
  end

  wr.close
  output = rd.read
  puts output
  rd.close

end

.defaultsObject



217
218
219
220
221
222
223
224
225
226
227
228
229
230
# File 'lib/resque/master.rb', line 217

def self.defaults
  {
    :config => nil,
    :logfile => nil,
    :stderr_path => "/dev/null",
    :stdout_path => "/dev/null",
    :pidfile => nil,
    :daemon => false,
    :preload_app => true,
    :worker_queues => ['*'],
    :worker_processes => 1,
    :work_interval => 5
  }
end

.optparse!Object



169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
# File 'lib/resque/master.rb', line 169

def self.optparse!
  options = {}

  optparse = OptionParser.new do|opts|
   # Set a banner, displayed at the top
   # of the help screen.
   opts.banner = "Usage: optparse1.rb [options] file1 file2 ..."

   opts.on( '-d', '--daemon', 'Run the master process as daemon' ) do
     options[:daemon] = true
   end

   opts.on( '-p', '--pid FILE', 'Write pid file to FILE' ) do|file|
     options[:pidfile] = file
   end

   opts.on( '-r', '--run PATH', 'Where to run the forked daemon' ) do|file|
     options[:runpath] = file
   end

   opts.on( '-e', '--stderr PATH', 'Where to send stderr output' ) do|file|
     options[:stderr_path] = file
   end

   opts.on( '-o', '--stdout PATH', 'Where to send stdout output' ) do|file|
     options[:stdout_path] = file
   end

   opts.on( '-c', '--config PATH', 'Path to a configuration file to load' ) do|file|
     options[:config] = file
   end

   opts.on( '-l', '--logfile FILE', 'Write log to FILE' ) do|file|
     options[:logfile] = file
   end

   # This displays the help screen, all programs are
   # assumed to have this option.
   opts.on( '-h', '--help', 'Display this screen' ) do
     puts opts
     exit
   end
  end

  optparse.parse!
  options
end

.process(config_path) ⇒ Object



70
71
72
73
74
# File 'lib/resque/master.rb', line 70

def self.process(config_path)
  loader = Resque::Master.new(config_path)
  loader.run
  loader.options
end

.setup(options) ⇒ Object



82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
# File 'lib/resque/master.rb', line 82

def self.setup(options)
  options[:runpath] = Dir.pwd if options[:runpath].nil?

  Resque.setup do |forker|
    if options[:preload_app]
      begin
        $:.unshift options[:runpath] # Makes 1.9.2 happy
        require options[:runpath] + "/config/environment"
        puts "Loaded Rails: #{Rails.env}"
        forker.logger   = Rails.logger
      rescue => e
        STDERR.puts e.message
        STDERR.puts e.backtrace.join("\n")
        raise e
      end
    end

    File.open(options[:pidfile],"wb") {|f| f << Process.pid } if options[:pidfile]
    forker.workload = options[:worker_queues] * options[:worker_processes].to_i if options[:worker_queues] && options[:worker_processes]
    forker.options.interval = options[:work_interval].to_i if options.key?(:work_interval)
  end

  Resque.teardown do|forker|
    File.unlink(options[:pidfile]) if options[:pidfile] && File.exist?(options[:pidfile]) 
  end

  Resque.before_first_fork do
    options[:before_first_fork].call if options[:before_first_fork].is_a?(Proc)
  end

  Resque.before_fork do
    options[:before_fork].call if options[:before_fork].is_a?(Proc)
  end

  Resque.after_fork do
    options[:after_fork].call if options[:after_fork].is_a?(Proc)
  end
end

Instance Method Details

#after_fork(&block) ⇒ Object



53
54
55
# File 'lib/resque/master.rb', line 53

def after_fork(&block)
  @options[:after_fork] = block
end

#before_fork(&block) ⇒ Object



49
50
51
# File 'lib/resque/master.rb', line 49

def before_fork(&block)
  @options[:before_fork] = block
end

#daemonize(yes_no) ⇒ Object



61
62
63
# File 'lib/resque/master.rb', line 61

def daemonize(yes_no)
  @options[:daemon] = yes_no
end

#pid(pidfile) ⇒ Object



29
30
31
# File 'lib/resque/master.rb', line 29

def pid(pidfile)
  @options[:pidfile] = pidfile
end

#preload_app(yes_no) ⇒ Object



57
58
59
# File 'lib/resque/master.rb', line 57

def preload_app(yes_no)
  @options[:preload_app] = yes_no
end

#runObject



76
77
78
79
80
# File 'lib/resque/master.rb', line 76

def run
  self.instance_eval(File.read(@config_path), @config_path, 0)
rescue => e
  raise "Config error: '#{e.message}' at #{e.backtrace[0].gsub(/:in `run'/,'')}"
end

#setup(&block) ⇒ Object



41
42
43
# File 'lib/resque/master.rb', line 41

def setup(&block)
  @options[:setup] = block
end

#stderr_path(path) ⇒ Object



33
34
35
# File 'lib/resque/master.rb', line 33

def stderr_path(path)
  @options[:stderr_path] = path
end

#stdout_path(path) ⇒ Object



37
38
39
# File 'lib/resque/master.rb', line 37

def stdout_path(path)
  @options[:stdout_path] = path
end

#teardown(&block) ⇒ Object



45
46
47
# File 'lib/resque/master.rb', line 45

def teardown(&block)
  @options[:teardown] = block
end

#work_interval(interval) ⇒ Object



21
22
23
# File 'lib/resque/master.rb', line 21

def work_interval(interval)
  @options[:work_interval] = interval
end

#worker_processes(count) ⇒ Object



9
10
11
# File 'lib/resque/master.rb', line 9

def worker_processes(count)
  @options[:worker_processes] = count
end

#worker_queues(queues) ⇒ Object



13
14
15
# File 'lib/resque/master.rb', line 13

def worker_queues(queues)
  @options[:worker_queues] = queues
end

#worker_timeout(timeout) ⇒ Object



17
18
19
# File 'lib/resque/master.rb', line 17

def worker_timeout(timeout)
  @options[:worker_timeout] = timeout
end

#working_directory(path) ⇒ Object



25
26
27
# File 'lib/resque/master.rb', line 25

def working_directory(path)
  @options[:runpath] = path
end