Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/usr/bin/env ruby
- # encoding: utf-8
- ##
- # setup
- #
- quiet = ARGV.delete('-q') || ARGV.delete('--quiet')
- mode = ARGV.shift || 'run'
- pidfile = './tmp/resque/pid'
- pid = Integer(IO.read(pidfile).strip) rescue nil
- running = begin; Process.kill(0, pid); true; rescue Object; false; end
- ## go
- #
- #
- case mode
- when 'pid'
- puts(pid) if running
- when 'run'
- ENV['PIDFILE'] = pidfile
- ENV['QUEUE'] = 'jobs'
- ENV['VVERBOSE'] = '1'
- exec "rake environment resque:work"
- when 'stop'
- if running
- begin
- Process.kill('-QUIT', pid)
- rescue Errno::ESRCH
- nil
- end
- end
- when 'start'
- unless running
- FileUtils.rm_f(pidfile)
- pid = nil
- a, b = IO.pipe
- if fork
- b.close
- pid = Integer(a.read.strip)
- a.close
- puts(pid) unless quiet
- exit
- end
- exit!(0) if fork
- a.close
- b.puts(Process.pid)
- b.close
- {
- 'stdin' => STDIN,
- 'stdout' => STDOUT,
- 'stderr' => STDERR,
- }.each do |basename, io|
- path = File.join("tmp/resque/#{ basename }")
- begin
- open(path, 'a+'){|fd| io.reopen(fd)}
- rescue
- open(path, 'w+'){|fd| io.reopen(fd)}
- end
- end
- Process.setsid rescue nil
- File.umask(0) rescue nil
- ENV['PIDFILE'] = pidfile
- ENV['QUEUE'] = 'jobs'
- ENV['VVERBOSE'] = '1' if ENV['RAILS_ENV'] != 'production'
- exec "rake environment resque:work"
- end
- when 'shutdown'
- if running
- alive = true
- %w( QUIT TERM ).each do |signal|
- begin
- Process.kill(signal, pid)
- rescue Errno::ESRCH
- nil
- end
- 42.times do
- begin
- alive = Process.kill(0, pid)
- sleep(rand) if alive
- rescue Errno::ESRCH
- alive = false
- break
- end
- end
- break unless alive
- end
- if alive
- begin
- Process.kill(-9, pid)
- sleep(rand)
- rescue Errno::ESRCH
- nil
- end
- end
- end
- when 'restart'
- exit! if fork
- exit! if fork
- script = File.expand_path(__FILE__)
- system "#{ script } shutdown"
- exec "#{ script } start"
- when 'tail'
- exec "tail -F ./tmp/resque/*"
- when 'pstree'
- exec "pstree #{ pid }" if running
- end
- BEGIN {
- require 'fileutils'
- script_file = File.expand_path(__FILE__)
- script_dir = File.dirname(script_file)
- rails_root = File.dirname(script_dir)
- require File.join(rails_root, 'config', 'env.rb')
- Dir.chdir(rails_root)
- FileUtils.mkdir_p(File.join(rails_root, 'tmp', 'resque'))
- FileUtils.touch(File.join(rails_root, 'tmp', 'resque', 'pid'))
- %w(
- BACKGROUND
- PIDFILE
- QUEUE
- VVERBOSE
- INTERVAL
- COUNT
- ).each do |key|
- ENV.delete(key)
- end
- }
Add Comment
Please, Sign In to add comment