123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125 |
- class CucumberExternalResqueWorker
- DEFAULT_STARTUP_TIMEOUT = 1.minute
- COUNTER_KEY = "cucumber:counter"
- class << self
- attr_accessor :pid, :startup_timeout
- def start
-
- return unless Rails.env.test?
- if self.pid = fork
- start_parent
- wait_for_worker_to_start
- else
- start_child
- end
- end
- def install_hooks_on_startup
-
- return unless Rails.env.test?
-
- Resque::Worker.all.each { |worker| worker.prune_dead_workers }
- install_pause_on_start_hook
- install_worker_base_counter_patch
- end
- def process_all
-
- unpause
- sleep 1 until done?
- pause
- end
- def incr
- Resque.redis.incr(COUNTER_KEY)
- end
- def decr
- Resque.redis.decr(COUNTER_KEY)
- end
- def reset_counter
- Resque.redis.set(COUNTER_KEY, 0)
- end
- private
- def done?
- Resque.redis.get(CucumberExternalResqueWorker::COUNTER_KEY).to_i.zero?
- end
- def pause(pid = self.pid)
- return unless Rails.env.test?
- Process.kill("USR2", pid)
- end
- def unpause
- return unless Rails.env.test?
- Process.kill("CONT", pid)
- end
- def start_parent
- at_exit do
-
- Process.kill("KILL", pid) if pid
- end
- end
- def start_child
-
-
-
- exec('rake', 'resque:work', "QUEUE=*", "RAILS_ENV=test", "VVERBOSE=1")
- end
- def wait_for_worker_to_start
- self.startup_timeout ||= DEFAULT_STARTUP_TIMEOUT
- start = Time.now.to_i
- while (Time.now.to_i - start) < startup_timeout
- return if worker_started?
- sleep 1
- end
- raise "Timeout while waiting for the worker to start. Waited #{startup_timeout} seconds."
- end
- def worker_started?
- Resque.info[:workers].to_i > 0
- end
- def install_pause_on_start_hook
- Resque.before_first_fork do
-
- pause(Process.pid)
- end
- end
- def install_worker_base_counter_patch
- Resque.class_eval do
- class << self
- def enqueue_with_counters(*args, &block)
- CucumberExternalResqueWorker.incr
- enqueue_without_counters(*args, &block)
- end
- alias_method_chain :enqueue, :counters
- end
- end
- Resque::Job.class_eval do
- def perform_with_counters(*args, &block)
- perform_without_counters(*args, &block)
- ensure
- CucumberExternalResqueWorker.decr
- end
- alias_method_chain :perform, :counters
- end
- end
- end
- end
|