Class: Speednode::Runtime::VM

Inherits:
Object
  • Object
show all
Defined in:
lib/speednode/runtime/vm.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options) ⇒ VM

Returns a new instance of VM.



69
70
71
72
73
74
75
# File 'lib/speednode/runtime/vm.rb', line 69

def initialize(options)
  @mutex = ::Thread::Mutex.new
  @socket_path = nil
  @options = options
  @started = false
  @socket = nil
end

Instance Attribute Details

#responderObject (readonly)

Returns the value of attribute responder.



67
68
69
# File 'lib/speednode/runtime/vm.rb', line 67

def responder
  @responder
end

Class Method Details

.exit_node(socket, socket_dir, socket_path, pid) ⇒ Object



50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
# File 'lib/speednode/runtime/vm.rb', line 50

def self.exit_node(socket, socket_dir, socket_path, pid)
  VMCommand.new(socket, "exit", 0).execute rescue nil
  socket.close
  File.unlink(socket_path) if File.exist?(socket_path)
  Dir.rmdir(socket_dir) if socket_dir && Dir.exist?(socket_dir)
  if Gem.win_platform?
    # SIGINT or SIGKILL are unreliable on Windows, try native taskkill first
    unless system("taskkill /f /t /pid #{pid} >NUL 2>NUL")
      Process.kill('KILL', pid) rescue nil
    end
  else
    Process.kill('KILL', pid) rescue nil
  end
rescue
  nil
end

.finalize(socket, socket_dir, socket_path, pid) ⇒ Object



43
44
45
46
47
48
# File 'lib/speednode/runtime/vm.rb', line 43

def self.finalize(socket, socket_dir, socket_path, pid)
  proc do
    ::Speednode::Runtime.responders[socket_path].kill if ::Speednode::Runtime.responders[socket_path]
    exit_node(socket, socket_dir, socket_path, pid)
  end
end

Instance Method Details

#attach(context, func) ⇒ Object



113
114
115
116
# File 'lib/speednode/runtime/vm.rb', line 113

def attach(context, func)
  create_responder(context) unless responder
  command('attach', {'context' => context, 'func' => func })
end

#bench(context, source) ⇒ Object



97
98
99
# File 'lib/speednode/runtime/vm.rb', line 97

def bench(context, source)
  command('bench', {'context' => context, 'source' => source})
end

#create(context, source, options) ⇒ Object



101
102
103
# File 'lib/speednode/runtime/vm.rb', line 101

def create(context, source, options)
  command('create', {'context' => context, 'source' => source, 'options' => options})
end

#created(context, source, options) ⇒ Object



105
106
107
# File 'lib/speednode/runtime/vm.rb', line 105

def created(context, source, options)
  command('created', {'context' => context, 'source' => source, 'options' => options})
end

#createp(context, source, options) ⇒ Object



109
110
111
# File 'lib/speednode/runtime/vm.rb', line 109

def createp(context, source, options)
  command('createp', {'context' => context, 'source' => source, 'options' => options})
end

#delete_context(context) ⇒ Object



118
119
120
121
122
123
124
# File 'lib/speednode/runtime/vm.rb', line 118

def delete_context(context)
  @mutex.synchronize do
    VMCommand.new(@socket, "deleteContext", context).execute rescue nil
  end
rescue ThreadError
  nil
end

#eval(context, source) ⇒ Object



89
90
91
# File 'lib/speednode/runtime/vm.rb', line 89

def eval(context, source)
  command('eval', {'context' => context, 'source' => source})
end

#evsc(context, key) ⇒ Object



81
82
83
# File 'lib/speednode/runtime/vm.rb', line 81

def evsc(context, key)
  command('evsc', { 'context' => context, 'key' => key })
end

#exec(context, source) ⇒ Object



93
94
95
# File 'lib/speednode/runtime/vm.rb', line 93

def exec(context, source)
  command('exec', {'context' => context, 'source' => source})
end

#scsc(context, key, source) ⇒ Object



85
86
87
# File 'lib/speednode/runtime/vm.rb', line 85

def scsc(context, key, source)
  command('scsc', { 'context' => context, 'key' => key, 'source' => source })
end

#startObject

def context_options(context)

command('ctxo', {'context' => context })

end



130
131
132
133
134
# File 'lib/speednode/runtime/vm.rb', line 130

def start
  @mutex.synchronize do
    start_without_synchronization
  end
end

#started?Boolean

Returns:

  • (Boolean)


77
78
79
# File 'lib/speednode/runtime/vm.rb', line 77

def started?
  @started
end

#stopObject



136
137
138
139
140
141
142
143
144
# File 'lib/speednode/runtime/vm.rb', line 136

def stop
  return unless @started
  @mutex.synchronize do
    self.class.exit_node(@socket, @socket_dir, @socket_path, @pid)
    @socket_path = nil
    @started = false
    @socket = nil
  end
end