A website template with lots of features, built with ruby on rails.

cucumber_external_resque_worker.rb 3.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. # This is adapted from this gist: https://gist.github.com/532100 by Square
  2. # The main difference is that it doesn't require Bluth for WorkerBase
  3. # It also calls prune_dead_workers on start so it doesn't hang on every other run
  4. # It does not do anything special to avoid connecting to your main redis instance; you should be
  5. # doing that elsewhere
  6. class CucumberExternalResqueWorker
  7. DEFAULT_STARTUP_TIMEOUT = 1.minute
  8. COUNTER_KEY = "cucumber:counter"
  9. class << self
  10. attr_accessor :pid, :startup_timeout
  11. def start
  12. # Call from a Cucumber support file so it is run on startup
  13. return unless Rails.env.test?
  14. if self.pid = fork
  15. start_parent
  16. wait_for_worker_to_start
  17. else
  18. start_child
  19. end
  20. end
  21. def install_hooks_on_startup
  22. # Call from a Rails initializer
  23. return unless Rails.env.test?
  24. # Because otherwise crashed workers cause a fork and we pause the actual worker forever
  25. Resque::Worker.all.each { |worker| worker.prune_dead_workers }
  26. install_pause_on_start_hook
  27. install_worker_base_counter_patch
  28. end
  29. def process_all
  30. # Call from a Cucumber step
  31. unpause
  32. sleep 1 until done?
  33. pause
  34. end
  35. def incr
  36. Resque.redis.incr(COUNTER_KEY)
  37. end
  38. def decr
  39. Resque.redis.decr(COUNTER_KEY)
  40. end
  41. def reset_counter
  42. Resque.redis.set(COUNTER_KEY, 0)
  43. end
  44. private
  45. def done?
  46. Resque.redis.get(CucumberExternalResqueWorker::COUNTER_KEY).to_i.zero?
  47. end
  48. def pause(pid = self.pid)
  49. return unless Rails.env.test?
  50. Process.kill("USR2", pid)
  51. end
  52. def unpause
  53. return unless Rails.env.test?
  54. Process.kill("CONT", pid)
  55. end
  56. def start_parent
  57. at_exit do
  58. #reset_counter
  59. Process.kill("KILL", pid) if pid
  60. end
  61. end
  62. def start_child
  63. # Array form of exec() is required here, otherwise the worker is not a direct child process of cucumber.
  64. # If it's not the direct child process then the PID returned from fork() is wrong, which means we can't
  65. # communicate with the worker.
  66. exec('rake', 'resque:work', "QUEUE=*", "RAILS_ENV=test", "VVERBOSE=1")
  67. end
  68. def wait_for_worker_to_start
  69. self.startup_timeout ||= DEFAULT_STARTUP_TIMEOUT
  70. start = Time.now.to_i
  71. while (Time.now.to_i - start) < startup_timeout
  72. return if worker_started?
  73. sleep 1
  74. end
  75. raise "Timeout while waiting for the worker to start. Waited #{startup_timeout} seconds."
  76. end
  77. def worker_started?
  78. Resque.info[:workers].to_i > 0
  79. end
  80. def install_pause_on_start_hook
  81. Resque.before_first_fork do
  82. #reset_counter
  83. pause(Process.pid)
  84. end
  85. end
  86. def install_worker_base_counter_patch
  87. Resque.class_eval do
  88. class << self
  89. def enqueue_with_counters(*args, &block)
  90. CucumberExternalResqueWorker.incr
  91. enqueue_without_counters(*args, &block)
  92. end
  93. alias_method_chain :enqueue, :counters
  94. end
  95. end
  96. Resque::Job.class_eval do
  97. def perform_with_counters(*args, &block)
  98. perform_without_counters(*args, &block)
  99. ensure
  100. CucumberExternalResqueWorker.decr
  101. end
  102. alias_method_chain :perform, :counters
  103. end
  104. end
  105. end
  106. end