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.



72
73
74
75
76
77
78
# File 'lib/speednode/runtime/vm.rb', line 72

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.



70
71
72
# File 'lib/speednode/runtime/vm.rb', line 70

def responder
  @responder
end

Class Method Details

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



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

def self.exit_node(socket, socket_dir, socket_path, pid, spawner_pid)
  return if spawner_pid != Process.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, spawner_pid) ⇒ Object



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

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

Instance Method Details

#attach(context, func) ⇒ Object



116
117
118
119
# File 'lib/speednode/runtime/vm.rb', line 116

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

#bench(context, source) ⇒ Object



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

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

#create(context, source, options) ⇒ Object



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

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

#created(context, source, options) ⇒ Object



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

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

#createp(context, source, options) ⇒ Object



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

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

#delete_context(context) ⇒ Object



121
122
123
124
125
126
127
# File 'lib/speednode/runtime/vm.rb', line 121

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

#eval(context, source) ⇒ Object



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

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

#evsc(context, key) ⇒ Object



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

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

#exec(context, source) ⇒ Object



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

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

#scsc(context, key, source) ⇒ Object



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

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

#startObject

def context_options(context)

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

end



133
134
135
136
137
# File 'lib/speednode/runtime/vm.rb', line 133

def start
  @mutex.synchronize do
    start_without_synchronization
  end
end

#started?Boolean

Returns:

  • (Boolean)


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

def started?
  @started
end

#stopObject



139
140
141
142
143
144
145
146
147
148
# File 'lib/speednode/runtime/vm.rb', line 139

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