Class: RubyAwaitNodejs::Runtime

Inherits:
Object
  • Object
show all
Defined in:
lib/ruby-await-nodejs/runtime.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(opts = {}) ⇒ Runtime

Returns a new instance of Runtime.



9
10
11
12
13
14
15
16
# File 'lib/ruby-await-nodejs/runtime.rb', line 9

def initialize(opts = {})
  srand

  @debug = opts[:debug]
  @nodejs_cmd = "node"
  @pid = nil
  @semaphore = Mutex.new
end

Instance Attribute Details

#debugObject

Returns the value of attribute debug.



7
8
9
# File 'lib/ruby-await-nodejs/runtime.rb', line 7

def debug
  @debug
end

#nodejs_cmdObject

Returns the value of attribute nodejs_cmd.



7
8
9
# File 'lib/ruby-await-nodejs/runtime.rb', line 7

def nodejs_cmd
  @nodejs_cmd
end

#pidObject

Returns the value of attribute pid.



7
8
9
# File 'lib/ruby-await-nodejs/runtime.rb', line 7

def pid
  @pid
end

#portObject

Returns the value of attribute port.



7
8
9
# File 'lib/ruby-await-nodejs/runtime.rb', line 7

def port
  @port
end

#semaphoreObject

Returns the value of attribute semaphore.



7
8
9
# File 'lib/ruby-await-nodejs/runtime.rb', line 7

def semaphore
  @semaphore
end

Instance Method Details

#available?Boolean

Returns:

  • (Boolean)


22
23
24
25
26
27
28
29
30
31
# File 'lib/ruby-await-nodejs/runtime.rb', line 22

def available?
  ENV["PATH"].split(":").detect do |path|
    %w{
      nodejs
      node
    }.detect do |node|
      File.exist?(File.join(path, node)) || File.symlink?(File.join(path, node))
    end
  end
end

#context_classObject



37
38
39
# File 'lib/ruby-await-nodejs/runtime.rb', line 37

def context_class
  RubyAwaitNodejs::Context
end

#deprecated?Boolean

Returns:

  • (Boolean)


33
34
35
# File 'lib/ruby-await-nodejs/runtime.rb', line 33

def deprecated?
  false
end

#nameObject



18
19
20
# File 'lib/ruby-await-nodejs/runtime.rb', line 18

def name
  "RubyAwaitNodejs"
end

#provision_socketObject

NOTE: this should be thread-safe



42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
# File 'lib/ruby-await-nodejs/runtime.rb', line 42

def provision_socket
  ensure_startup unless @pid

  wait_socket = nil
  checks = 0
  max_retries = 12

  while checks < max_retries
    begin
      checks += 1
      wait_socket = UNIXSocket.new(@port)
      break
    rescue Errno::ENOENT, Errno::ECONNREFUSED, Errno::ENOTDIR
      wait_socket = nil
      sleep 0.5
    end
  end

  if checks >= max_retries
    ensure_shutdown
    raise RuntimeError, "unable to connect to ruby-await-nodejs.js server"
  end

  wait_socket
end