Guest User

Untitled

a guest
Oct 20th, 2018
81
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.98 KB | None | 0 0
  1. #!/usr/bin/env ruby
  2. # encoding: utf-8
  3.  
  4. ##
  5. # setup
  6. #
  7. quiet = ARGV.delete('-q') || ARGV.delete('--quiet')
  8. mode = ARGV.shift || 'run'
  9. pidfile = './tmp/resque/pid'
  10. pid = Integer(IO.read(pidfile).strip) rescue nil
  11. running = begin; Process.kill(0, pid); true; rescue Object; false; end
  12.  
  13. ## go
  14. #
  15. #
  16. case mode
  17. when 'pid'
  18. puts(pid) if running
  19.  
  20. when 'run'
  21. ENV['PIDFILE'] = pidfile
  22. ENV['QUEUE'] = 'jobs'
  23. ENV['VVERBOSE'] = '1'
  24. exec "rake environment resque:work"
  25.  
  26. when 'stop'
  27. if running
  28. begin
  29. Process.kill('-QUIT', pid)
  30. rescue Errno::ESRCH
  31. nil
  32. end
  33. end
  34.  
  35. when 'start'
  36. unless running
  37. FileUtils.rm_f(pidfile)
  38.  
  39. pid = nil
  40. a, b = IO.pipe
  41. if fork
  42. b.close
  43. pid = Integer(a.read.strip)
  44. a.close
  45. puts(pid) unless quiet
  46. exit
  47. end
  48. exit!(0) if fork
  49. a.close
  50. b.puts(Process.pid)
  51. b.close
  52.  
  53. {
  54. 'stdin' => STDIN,
  55. 'stdout' => STDOUT,
  56. 'stderr' => STDERR,
  57. }.each do |basename, io|
  58. path = File.join("tmp/resque/#{ basename }")
  59. begin
  60. open(path, 'a+'){|fd| io.reopen(fd)}
  61. rescue
  62. open(path, 'w+'){|fd| io.reopen(fd)}
  63. end
  64. end
  65.  
  66. Process.setsid rescue nil
  67. File.umask(0) rescue nil
  68.  
  69. ENV['PIDFILE'] = pidfile
  70. ENV['QUEUE'] = 'jobs'
  71. ENV['VVERBOSE'] = '1' if ENV['RAILS_ENV'] != 'production'
  72. exec "rake environment resque:work"
  73. end
  74.  
  75. when 'shutdown'
  76. if running
  77. alive = true
  78.  
  79. %w( QUIT TERM ).each do |signal|
  80. begin
  81. Process.kill(signal, pid)
  82. rescue Errno::ESRCH
  83. nil
  84. end
  85.  
  86. 42.times do
  87. begin
  88. alive = Process.kill(0, pid)
  89. sleep(rand) if alive
  90. rescue Errno::ESRCH
  91. alive = false
  92. break
  93. end
  94. end
  95.  
  96. break unless alive
  97. end
  98.  
  99. if alive
  100. begin
  101. Process.kill(-9, pid)
  102. sleep(rand)
  103. rescue Errno::ESRCH
  104. nil
  105. end
  106. end
  107. end
  108.  
  109. when 'restart'
  110. exit! if fork
  111. exit! if fork
  112. script = File.expand_path(__FILE__)
  113. system "#{ script } shutdown"
  114. exec "#{ script } start"
  115.  
  116. when 'tail'
  117. exec "tail -F ./tmp/resque/*"
  118.  
  119. when 'pstree'
  120. exec "pstree #{ pid }" if running
  121. end
  122.  
  123.  
  124. BEGIN {
  125. require 'fileutils'
  126.  
  127. script_file = File.expand_path(__FILE__)
  128. script_dir = File.dirname(script_file)
  129. rails_root = File.dirname(script_dir)
  130.  
  131. require File.join(rails_root, 'config', 'env.rb')
  132.  
  133. Dir.chdir(rails_root)
  134. FileUtils.mkdir_p(File.join(rails_root, 'tmp', 'resque'))
  135. FileUtils.touch(File.join(rails_root, 'tmp', 'resque', 'pid'))
  136.  
  137. %w(
  138. BACKGROUND
  139. PIDFILE
  140. QUEUE
  141. VVERBOSE
  142. INTERVAL
  143. COUNT
  144. ).each do |key|
  145. ENV.delete(key)
  146. end
  147. }
Add Comment
Please, Sign In to add comment