Top Level Namespace

Defined Under Namespace

Modules: Capistrano, Rubber Classes: VulcanizeGenerator

Instance Method Summary collapse

Instance Method Details

#daemonize(log_file, pid_file = nil) ⇒ Object



89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
# File 'lib/generators/vulcanize/templates/resque/script/resque_worker_management.rb', line 89

def daemonize(log_file, pid_file=nil)
  return if fork
  Process::setsid
  exit!(0) if fork
  Dir::chdir(Rubber.root)
  File.umask 0000
  FileUtils.touch log_file
  STDIN.reopen    log_file
  STDOUT.reopen   log_file, "a"
  STDERR.reopen   log_file, "a"
  STDOUT.sync = true
  STDERR.sync = true

  File.open(pid_file, 'w') {|f| f.write("#{Process.pid}") } if pid_file

  yield if block_given?
  exit(0)
end

#load_env(rubber_only = true) ⇒ Object



6
7
8
9
10
11
12
13
14
15
16
17
18
# File 'lib/generators/vulcanize/templates/resque/script/resque_worker_management.rb', line 6

def load_env(rubber_only=true)
  env = ENV["RUBBER_ENV"] ||= "development"
  root = File.expand_path('../..', __FILE__)
  rails_env_file = File.join(root, 'config', 'environment.rb')

  if ! rubber_only && File.exists?(rails_env_file)
    require(rails_env_file)
  else
    require "bundler/setup" if File.exist?(File.join(root, "Gemfile"))
    require "rubber"
    Rubber::initialize(root, env)
  end
end

#log_file(index) ⇒ Object



112
113
114
# File 'lib/generators/vulcanize/templates/resque/script/resque_worker_management.rb', line 112

def log_file(index)
  File.expand_path "#{Rubber.root}/log/resque_worker_#{index}.log"
end

#pid_file(index) ⇒ Object



108
109
110
# File 'lib/generators/vulcanize/templates/resque/script/resque_worker_management.rb', line 108

def pid_file(index)
  File.expand_path "#{Rubber.root}/tmp/pids/resque_worker_#{index}.pid"
end

#start(worker, index) ⇒ Object



35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
# File 'lib/generators/vulcanize/templates/resque/script/resque_worker_management.rb', line 35

def start(worker, index)
  puts "Starting worker #{index}/#{worker.queues}"

  log_file = log_file(index)
  pid_file = pid_file(index)

  queues = worker.queues.to_s.split(',')

  daemonize(log_file, pid_file) do
    # load env for each worker, if starting multiple, this will be a no-op due
    # to start_all preloading the env
    load_env(false)
    resque_worker = Resque::Worker.new(*queues)
    resque_worker.verbose = ENV['LOGGING'] || ENV['VERBOSE']
    resque_worker.very_verbose = ENV['VVERBOSE']

    puts "*** Starting worker #{resque_worker}"
    resque_worker.work(worker.poll_interval.to_i || 5) # interval, will block
  end

end

#start_all(workers) ⇒ Object



21
22
23
24
25
26
27
28
29
30
31
32
33
# File 'lib/generators/vulcanize/templates/resque/script/resque_worker_management.rb', line 21

def start_all(workers)
  puts "Starting all workers"
  # fork then load rails env so script runs quick, yet we don't overload
  # machine by loading env for each worker
  daemonize(log_file('all')) do
    load_env(false)
    puts "Preloaded environment for all workers"

    workers.each_with_index do |worker, i|
      start(worker, i)
    end
  end
end

#stop(index, signal) ⇒ Object

Resque workers respond to a few different signals:

QUIT - Wait for child to finish processing then exit TERM / INT - Immediately kill child then exit USR1 - Immediately kill child but don’t exit USR2 - Don’t start to process any new jobs CONT - Start to process new jobs again after a USR2



71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
# File 'lib/generators/vulcanize/templates/resque/script/resque_worker_management.rb', line 71

def stop(index, signal)
  puts "Stopping worker #{index}"

  pid_file = pid_file(index)
  pid = File.read(pid_file).to_i rescue nil
  if pid
    puts "Killing worker #{index}: pid #{pid} - #{signal}"
    begin
      Process.kill(signal, pid)
    rescue Exception => e
      puts e
    end
    File.delete(pid_file) if File.exist?(pid_file)
  else
    puts "No pid file for worker #{index}: #{pid_file}"
  end
end

#stop_all(workers, signal) ⇒ Object



57
58
59
60
61
62
# File 'lib/generators/vulcanize/templates/resque/script/resque_worker_management.rb', line 57

def stop_all(workers, signal)
  puts "Stopping all workers"
  workers.size.times do |i|
    stop(i, signal)
  end
end