Module: PrerenderRailsEmbedded

Defined in:
lib/prerender_rails_embedded.rb

Class Method Summary collapse

Class Method Details

.flatten_js_to_html(url) ⇒ Object



5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/prerender_rails_embedded.rb', line 5

def self.flatten_js_to_html(url)
  out = ''
  thread = nil
  pid = -1
  begin
  Timeout::timeout(30) do
    thread = Thread.new do
      IO.popen(phantomjs_invocation(url)) do |io|
        pid = io.pid
        Rails.logger.debug("Spawing new phantomjs processo with pid: #{pid}")
        io.read
      end
    end
    out += thread.value
  end
  rescue Timeout::Error
    Rails.logger.debug('triggering timed out')

    if pid_exists?(pid)
      Rails.logger.warn("phantomjs with pid##{pid} timed out")

      Process.kill 9, pid
      out += '<html><head><meta name="robots" content="noindex, noarchive " /></head><body></body></html>'
    end
    if thread && thread.alive?
      Thread.kill(thread)
    end
  end
  out
end

.local_rendererObject



51
52
53
54
55
# File 'lib/prerender_rails_embedded.rb', line 51

def self.local_renderer
  Proc.new do |env|
    flatten_js_to_html(Rack::Request.new(env).url)
  end
end

.phantomjs_invocation(url) ⇒ Object



47
48
49
# File 'lib/prerender_rails_embedded.rb', line 47

def self.phantomjs_invocation(url)
  [Phantomjs.path , '--load-images=false', '--ignore-ssl-errors=true', '--ssl-protocol=TLSv1', '--disk-cache=true', '--max-disk-cache-size=524228', "#{File.dirname(__FILE__)}/prerender_rails_embedded.js", url]
end

.pid_exists?(pid) ⇒ Boolean

Returns:

  • (Boolean)


37
38
39
40
41
42
43
44
45
# File 'lib/prerender_rails_embedded.rb', line 37

def self.pid_exists?(pid)
  begin
    Process.kill 0, pid
    return true
  rescue Errno::ESRCH
    return false
  end

end