Class: Rerun::Runner
Class Method Summary collapse
Instance Method Summary collapse
- #clear? ⇒ Boolean
- #clear_screen ⇒ Object
- #die ⇒ Object
- #dir ⇒ Object
- #exit? ⇒ Boolean
- #git_head_changed? ⇒ Boolean
-
#initialize(run_command, options = {}) ⇒ Runner
constructor
A new instance of Runner.
- #join ⇒ Object
-
#key_pressed ⇒ Object
non-blocking stdin reader.
- #notify(title, body) ⇒ Object
- #pattern ⇒ Object
- #read_git_head ⇒ Object
- #restart ⇒ Object
- #running? ⇒ Boolean
- #say(msg) ⇒ Object
- #signal(signal) ⇒ Object
- #start ⇒ Object
- #start_keypress_thread ⇒ Object
-
#stop ⇒ Object
todo: test escalation.
- #stop_keypress_thread ⇒ Object
Methods included from System
#app_name, #growl, #growl?, #growlcmd, #icon, #linux?, #mac?, #windows?
Constructor Details
#initialize(run_command, options = {}) ⇒ Runner
Returns a new instance of Runner.
15 16 17 18 |
# File 'lib/rerun/runner.rb', line 15 def initialize(run_command, = {}) @run_command, @options = run_command, @run_command = "ruby #{@run_command}" if @run_command.split(' ').first =~ /\.rb$/ end |
Class Method Details
.keep_running(cmd, options) ⇒ Object
7 8 9 10 11 |
# File 'lib/rerun/runner.rb', line 7 def self.keep_running(cmd, ) runner = new(cmd, ) runner.start runner.join end |
Instance Method Details
#clear? ⇒ Boolean
69 70 71 |
# File 'lib/rerun/runner.rb', line 69 def clear? @options[:clear] end |
#clear_screen ⇒ Object
261 262 263 264 |
# File 'lib/rerun/runner.rb', line 261 def clear_screen # see http://ascii-table.com/ansi-escape-sequences-vt-100.php $stdout.print "\033[H\033[2J" end |
#die ⇒ Object
160 161 162 163 164 |
# File 'lib/rerun/runner.rb', line 160 def die #stop_keypress_thread # don't do this since we're probably *in* the keypress thread stop # stop the child process if it exists exit 0 # todo: status code param end |
#dir ⇒ Object
61 62 63 |
# File 'lib/rerun/runner.rb', line 61 def dir @options[:dir] || "." end |
#exit? ⇒ Boolean
73 74 75 |
# File 'lib/rerun/runner.rb', line 73 def exit? @options[:exit] end |
#git_head_changed? ⇒ Boolean
207 208 209 210 211 |
# File 'lib/rerun/runner.rb', line 207 def git_head_changed? old_git_head = @git_head read_git_head @git_head and old_git_head and @git_head != old_git_head end |
#join ⇒ Object
166 167 168 |
# File 'lib/rerun/runner.rb', line 166 def join @watcher.join end |
#key_pressed ⇒ Object
non-blocking stdin reader. returns a 1-char string if a key was pressed; otherwise nil
231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 |
# File 'lib/rerun/runner.rb', line 231 def key_pressed begin # this "raw input" nonsense is because unix likes waiting for linefeeds before sending stdin # 'raw' means turn raw input on # restore proper output newline handling -- see stty.rb and "man stty" and /usr/include/sys/termios.h # looks like "raw" flips off the OPOST bit 0x00000001 /* enable following output processing */ # which disables #define ONLCR 0x00000002 /* map NL to CR-NL (ala CRMOD) */ # so this sets it back on again since all we care about is raw input, not raw output system("stty raw opost") c = nil if $stdin.ready? c = $stdin.getc end c.chr if c ensure system "stty -raw" # turn raw input off end # note: according to 'man tty' the proper way restore the settings is # tty_state=`stty -g` # ensure # system 'stty "#{tty_state}' # end # but this way seems fine and less confusing end |
#notify(title, body) ⇒ Object
218 219 220 221 222 |
# File 'lib/rerun/runner.rb', line 218 def notify(title, body) growl title, body puts say "#{app_name} #{title}" end |
#pattern ⇒ Object
65 66 67 |
# File 'lib/rerun/runner.rb', line 65 def pattern @options[:pattern] || DEFAULT_PATTERN end |
#read_git_head ⇒ Object
213 214 215 216 |
# File 'lib/rerun/runner.rb', line 213 def read_git_head git_head_file = File.join(dir, '.git', 'HEAD') @git_head = File.exists?(git_head_file) && File.read(git_head_file) end |
#restart ⇒ Object
54 55 56 57 58 59 |
# File 'lib/rerun/runner.rb', line 54 def restart @restarting = true stop start @restarting = false end |
#running? ⇒ Boolean
170 171 172 |
# File 'lib/rerun/runner.rb', line 170 def running? signal(0) end |
#say(msg) ⇒ Object
224 225 226 |
# File 'lib/rerun/runner.rb', line 224 def say msg puts "#{Time.now.strftime("%T")} [rerun] #{msg}" end |
#signal(signal) ⇒ Object
174 175 176 177 178 179 180 |
# File 'lib/rerun/runner.rb', line 174 def signal(signal) say "Sending signal #{signal} to #{@pid}" unless signal == 0 Process.kill(signal, @pid) true rescue false end |
#start ⇒ Object
77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 |
# File 'lib/rerun/runner.rb', line 77 def start if windows? raise "Sorry, Rerun does not work on Windows." end if (!@already_running) taglines = [ "To infinity... and beyond!", "Charge!", ] notify "launched", taglines[rand(taglines.size)] @already_running = true else taglines = [ "Here we go again!", "Keep on trucking.", "Once more unto the breach, dear friends, once more!", "The road goes ever on and on, down from the door where it began.", ] notify "restarted", taglines[rand(taglines.size)] end clear_screen if clear? start_keypress_thread unless @keypress_thread @pid = Kernel.fork do begin exec(@run_command) rescue => e puts "#{e.class}: #{e.}" exit end end status_thread = Process.detach(@pid) # so if the child exits, it dies Signal.trap("INT") do # INT = control-C -- allows user to stop the top-level rerun process die end Signal.trap("TERM") do # TERM is the polite way of terminating a process die end begin sleep 2 rescue Interrupt => e # in case someone hits control-C immediately ("oops!") die end if exit? status = status_thread.value if status.success? notify "succeeded", "" else notify "failed", "Exit status #{status.exitstatus}" end else if !running? notify "Launch Failed", "See console for error output" @already_running = false end end unless @watcher watcher = Watcher.new(:directory => dir, :pattern => pattern) do |changes| = [:modified, :added, :removed].map do |change| count = changes[change].size if count and count > 0 "#{count} #{change}" end end.compact.join(", ") say "Change detected: #{}" restart unless @restarting end watcher.start @watcher = watcher say "Watching #{dir}/#{pattern} using #{watcher.adapter.class.name.split('::').last} adapter" end end |
#start_keypress_thread ⇒ Object
20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 |
# File 'lib/rerun/runner.rb', line 20 def start_keypress_thread @keypress_thread = Thread.new do while true if c = key_pressed case c.downcase when 'c' say "Clearing screen" clear_screen when 'r' say "Restarting" restart when 'x', 'q' die break # the break will stop this thread, in case the 'die' doesn't else puts "\n#{c.inspect} pressed inside rerun" puts [["c", "clear screen"], ["r", "restart"], ["x or q", "stop and exit"] ].map{|key, description| " #{key} -- #{description}"}.join("\n") puts end end sleep 1 # todo: use select instead of polling somehow? end end @keypress_thread.run end |
#stop ⇒ Object
todo: test escalation
183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 |
# File 'lib/rerun/runner.rb', line 183 def stop if @pid && (@pid != 0) notify "stopping", "All good things must come to an end." unless @restarting begin timeout(4) do # todo: escalation timeout setting # start with a polite SIGTERM signal("TERM") && Process.wait(@pid) end rescue Timeout::Error begin timeout(2) do # escalate to SIGINT aka control-C since some foolish process may be ignoring SIGTERM signal("INT") && Process.wait(@pid) end rescue Timeout::Error # escalate to SIGKILL aka "kill -9" which cannot be ignored signal("KILL") && Process.wait(@pid) end end end rescue => e false end |
#stop_keypress_thread ⇒ Object
49 50 51 52 |
# File 'lib/rerun/runner.rb', line 49 def stop_keypress_thread @keypress_thread.kill if @keypress_thread @keypress_thread = nil end |