Class: PrefetchRspec::Server

Inherits:
Base
  • Object
show all
Defined in:
lib/prefetch_rspec.rb

Constant Summary collapse

@@force_exit =
false

Instance Attribute Summary

Attributes inherited from Base

#options

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Base

#color, #cwarn, #drb_uri, #initialize

Constructor Details

This class inherits a constructor from PrefetchRspec::Base

Class Method Details

.force_exit!Object



132
133
134
135
# File 'lib/prefetch_rspec.rb', line 132

def self.force_exit!
  @@force_exit = true
  exit 1
end

.listen(args, script) ⇒ Object



86
87
88
89
90
91
92
93
94
95
96
97
# File 'lib/prefetch_rspec.rb', line 86

def self.listen(args, script)
  begin
    server = self.new(args)
    register_handlers(server, args, script)
    server.listen
  rescue SystemExit
    # silent
  rescue Exception => e
    warn "ServerError: #{e}"
    warn e.backtrace.map {|l| "  " + l}.join("\n")
  end
end

.register_handlers(server, args, script) ⇒ Object



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
# File 'lib/prefetch_rspec.rb', line 100

def self.register_handlers(server, args, script)
  [:TERM, :KILL].each do |signal|
    Signal.trap(signal) {
      force_exit!
    }
  end
  
  sigint_first_called = false
  Signal.trap(:INT) {
    if sigint_first_called
      force_exit!
    else
      sigint_first_called = true
      warn " reloding... [Ctrl-C quick press shoudown]"
      Thread.new { 
        sleep 1.5
        exit
      }
    end
  }

  at_exit {
    if @@force_exit
      server.cwarn("shutdown: ...")
    else
      server.cwarn("")
      server.cwarn("self restart: " + [script, args.to_a].flatten.join(' '))
      exec(script, *args.to_a)
    end
  }
end

Instance Method Details

#after_run(&block) ⇒ Object



177
178
179
# File 'lib/prefetch_rspec.rb', line 177

def after_run(&block)
  @after_run = block
end


257
258
259
# File 'lib/prefetch_rspec.rb', line 257

def banner
  cwarn "usage: #{File.basename($0)} [--rails] [config_file(default .prspecd)]"
end

#before_run(&block) ⇒ Object



173
174
175
# File 'lib/prefetch_rspec.rb', line 173

def before_run(&block)
  @before_run = block
end

#call_after_run(err, out) ⇒ Object



230
231
232
233
234
235
236
# File 'lib/prefetch_rspec.rb', line 230

def call_after_run(err, out)
  if @after_run
    timewatch('after_run') do
      replace_io_execute(err, out, 'after_run') { @after_run.call }
    end
  end
end

#call_before_run(err, out) ⇒ Object



222
223
224
225
226
227
228
# File 'lib/prefetch_rspec.rb', line 222

def call_before_run(err, out)
  if @before_run
    timewatch('before_run') do
      replace_io_execute(err, out, 'before_run') { @before_run.call }
    end
  end
end

#call_prefetchObject



188
189
190
191
192
193
194
195
196
197
198
199
200
# File 'lib/prefetch_rspec.rb', line 188

def call_prefetch
  if @prefetch
    timewatch('prefetch') do
      @prefetch_err = StringIO.new
      @prefetch_out = StringIO.new
        replace_io_execute(@prefetch_err, @prefetch_out, 'prefetch') {
          @prefetch.call
        }
      @prefetch_err.rewind
    end
  end
  @prefetched = true
end

#detect_load_configObject



261
262
263
264
265
266
267
268
269
270
# File 'lib/prefetch_rspec.rb', line 261

def detect_load_config
  require 'pathname'
  if options[:rails]
    load_config Pathname.new(File.expand_path(__FILE__)).parent.parent.join('examples/rails.prspecd')
  elsif options[:args].first
    load_config Pathname.new(Dir.pwd).join(options[:args].first)
  else
    load_config(Pathname.new(Dir.pwd).join('.prspecd'))
  end
end

#listenObject



272
273
274
275
276
277
278
279
280
281
282
283
284
285
# File 'lib/prefetch_rspec.rb', line 272

def listen
  ENV['PRSPEC'] = 'true'
  detect_load_config

  begin
    @drb_service = DRb.start_service(drb_uri, self)
  rescue DRb::DRbConnError => e
    cwarn("client connection abort", 31)
    @drb_service.stop_service
    exit 1
  end
  call_prefetch
  @drb_service.thread.join
end

#load_config(path) ⇒ Object



246
247
248
249
250
251
252
253
254
255
# File 'lib/prefetch_rspec.rb', line 246

def load_config(path)
  if path.exist?
    self.instance_eval path.read, path.to_s
    cwarn("config loaded: #{path}", 37)
  else
    cwarn("config not found:#{path}", 31)
    banner
    self.class.force_exit!
  end
end

#load_config_prspecdObject



238
239
240
241
242
243
244
# File 'lib/prefetch_rspec.rb', line 238

def load_config_prspecd
  dot_prspecd = Pathname.new(Dir.pwd).join('.prspecd')
  if dot_prspecd.exist?
    self.instance_eval dot_prspecd.read, dot_prspecd.to_s
  else
  end
end

#prefetch(&block) ⇒ Object



169
170
171
# File 'lib/prefetch_rspec.rb', line 169

def prefetch(&block)
  @prefetch = block
end

#replace_io_execute(err, out, catch_fail) ⇒ Object



202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
# File 'lib/prefetch_rspec.rb', line 202

def replace_io_execute(err, out, catch_fail)
  orig_out = $stdout
  orig_err = $stderr
  result = nil
  begin
    $stdout = out
    $stderr = err
    result = yield
  rescue Exception => exception
    if catch_fail
      err.puts color("hook #{catch_fail} raise error: #{exception}", 31)
      err.puts color(exception.backtrace.map {|l| "  " + l}.join("\n"), 37)
    end
  ensure
    $stdout = orig_out
    $stderr = orig_err
  end
  result
end

#run(options, err, out) ⇒ Object



137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
# File 'lib/prefetch_rspec.rb', line 137

def run(options, err, out)
  while !@prefetched
    sleep 0.1
  end
  if @prefetch_out
    @prefetch_out.rewind
    out.print @prefetch_out.read
  end
  if @prefetch_err
    @prefetch_err.rewind
    err.print @prefetch_err.read
  end

  call_before_run(err, out)
  RSpec::Core::Runner.disable_autorun!
  result = replace_io_execute(err, out, nil) { RSpec::Core::Runner.run(options, err, out) }
  call_after_run(err, out)
  Thread.new { 
    sleep 0.01
    stop_service!
  }
  result
end

#stop_service!Object



161
162
163
164
165
166
167
# File 'lib/prefetch_rspec.rb', line 161

def stop_service!
  if @drb_service
    @drb_service.stop_service
  else
    false
  end
end

#timewatch(name) ⇒ Object



181
182
183
184
185
186
# File 'lib/prefetch_rspec.rb', line 181

def timewatch(name)
  now = Time.now.to_f
  cwarn("#{name}: start", 35)
  yield
  cwarn("#{name}: finished (%.3f sec)" % (Time.now.to_f - now), 35)
end