Class: Bullring::RhinoServer

Inherits:
Object
  • Object
show all
Defined in:
lib/bullring/workers/rhino_server.rb

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(host, registry_port) ⇒ RhinoServer

Returns a new instance of RhinoServer.



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
48
49
50
51
52
53
# File 'lib/bullring/workers/rhino_server.rb', line 21

def initialize(host, registry_port)
  @library_cache = {}
  
  @default_options = { :run_is_sealed => false,
                       :run_is_restrictable => true,
                       :run_timeout_secs => 0.5 }

  # Connect to the server registry
  @server_registry = ServerRegistry.new(host,registry_port, nil)

  # Start up as a DRb server (get the port from the registry)
  port = @server_registry.next_server_port
  uri = "druby://#{host}:#{port}"
  DRb.start_service uri, self

  # Put ourselves on the registry
  @server_registry.register_server(uri)

  # Keep an eye on the registry, if it dies, we should die
  Thread.new do
    while (true) do
      sleep(5)
      begin 
        @server_registry.test!
      rescue
        DRb.stop_service
        Thread.main.exit
      end
    end
  end
  
  DRb.thread.join
end

Class Method Details

.start(host, registry_port) ⇒ Object



108
109
110
# File 'lib/bullring/workers/rhino_server.rb', line 108

def self.start(host, registry_port)
  RhinoServer.new(host, registry_port)
end

Instance Method Details

#alive?Boolean

Returns:

  • (Boolean)


99
100
101
# File 'lib/bullring/workers/rhino_server.rb', line 99

def alive?
  true
end

#check(script, options) ⇒ Object



69
70
71
72
73
74
75
76
77
78
79
# File 'lib/bullring/workers/rhino_server.rb', line 69

def check(script, options)
  Rhino::Context.open do |context|
    context_wrapper {context.load(File.expand_path("../../js/jslint.min.js", __FILE__))}
    
    call = Bullring::Helper::jslint_call(script)
    
    duration, result = context_wrapper {context.eval(call)}
    
    result = result.collect{|obj| obj.respond_to?(:to_h) ? obj.to_h : obj}
  end      
end

#get_library(name) ⇒ Object



63
64
65
66
67
# File 'lib/bullring/workers/rhino_server.rb', line 63

def get_library(name)
  # If try to use the cache, tests that change libraries fail
  #   @library_cache[name] ||= fetch_library(name)
  fetch_library(name)
end

#killObject



103
104
105
106
# File 'lib/bullring/workers/rhino_server.rb', line 103

def kill
  DRb.stop_service
  exit
end

#loggerObject



59
60
61
# File 'lib/bullring/workers/rhino_server.rb', line 59

def logger
  @logger ||= Bullring::DummyLogger.new
end

#logger=(logger) ⇒ Object



55
56
57
# File 'lib/bullring/workers/rhino_server.rb', line 55

def logger=(logger)
  @logger = logger
end

#run(script, options) ⇒ Object



81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
# File 'lib/bullring/workers/rhino_server.rb', line 81

def run(script, options)
  # Don't do a merge b/c jruby and ruby don't play nicely for some reason
  @default_options.each{|k,v| options[k] = v}
  
  Rhino::Context.open(:sealed => options[:run_is_sealed], :restrictable => options[:run_is_restrictable]) do |context|

    (options['library_names'] || []).each do |library_name|
      library_script = get_library(library_name)
      context_wrapper {context.eval(library_script)}      
    end
      
    context.timeout_limit = options[:run_timeout_secs]
    
    duration, result = context_wrapper {context.eval(script)}      
    result.respond_to?(:to_h) ? result.to_h : result      
  end      
end