Top Level Namespace

Defined Under Namespace

Modules: Capistrano, Rubber Classes: VulcanizeGenerator

Instance Method Summary collapse

Instance Method Details

#daemonize(log_file, pid_file) ⇒ Object


66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
# File 'lib/generators/vulcanize/templates/resque/script/resque_worker_management.rb', line 66

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

  File.open(pid_file, 'w') {|f| f.write("#{Process.pid}") }
  
  yield if block_given?
  exit(0)
end

#log_file(index) ⇒ Object


87
88
89
# File 'lib/generators/vulcanize/templates/resque/script/resque_worker_management.rb', line 87

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

#pid_file(index) ⇒ Object


83
84
85
# File 'lib/generators/vulcanize/templates/resque/script/resque_worker_management.rb', line 83

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

#start(worker, index) ⇒ Object


23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
# File 'lib/generators/vulcanize/templates/resque/script/resque_worker_management.rb', line 23

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
    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


16
17
18
19
20
21
# File 'lib/generators/vulcanize/templates/resque/script/resque_worker_management.rb', line 16

def start_all(workers)
  puts "Starting all workers"
  workers.each_with_index do |worker, i|
    start(worker, i)
  end
end

#stop(index) ⇒ Object


48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
# File 'lib/generators/vulcanize/templates/resque/script/resque_worker_management.rb', line 48

def stop(index)
  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}"
    begin
      Process.kill("QUIT", 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) ⇒ Object


41
42
43
44
45
46
# File 'lib/generators/vulcanize/templates/resque/script/resque_worker_management.rb', line 41

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