Class: SpreadBeaver::Runner

Inherits:
Object
  • Object
show all
Defined in:
lib/spread_beaver/runner.rb

Constant Summary collapse

QUEUE_KEY =
:_spread_beaver_queue

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.exec(module_name, props) ⇒ Object



13
14
15
16
17
# File 'lib/spread_beaver/runner.rb', line 13

def self.exec module_name, props
  @@pool.with do |r|
    r.exec module_name, props
  end
end

.queue(module_name, props) ⇒ Object



19
20
21
22
23
24
# File 'lib/spread_beaver/runner.rb', line 19

def self.queue module_name, props
  if RequestStore.store[QUEUE_KEY].nil?
    RequestStore.store[QUEUE_KEY] = []
  end
  RequestStore.store[QUEUE_KEY].push module_name: module_name, props: props
end

.setup!(bundle, opts = {size: 10, timeout: 20}) ⇒ Object



7
8
9
10
11
# File 'lib/spread_beaver/runner.rb', line 7

def self.setup! bundle, opts={size: 10, timeout: 20}
  @@bundle = bundle
  @@pool.shutdown{} if @@pool
  @@pool = ConnectionPool.new(opts) { self.new }
end

Instance Method Details

#exec(module_name, props) ⇒ Object



26
27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/spread_beaver/runner.rb', line 26

def exec module_name, props
  js_code = "(function() {"
  js_code += initialize_script if !initialize_script.nil?
  if !RequestStore.store[self.class::QUEUE_KEY].nil?
    RequestStore.store[self.class::QUEUE_KEY].each do |store|
      str = exec_queued_module store[:module_name], store[:props]
      js_code += str if !str.nil?
    end
  end
  js_code += "return #{exec_module module_name, props}"
  js_code += "})()"
  context.eval(js_code).html_safe
end