Module: Gotta::Run::Ruby::Runtime

Defined in:
lib/gotta/run/ruby/runtime/event.rb,
lib/gotta/run/ruby/runtime.rb,
lib/gotta/run/ruby/runtime/errors.rb,
lib/gotta/run/ruby/runtime/response.rb,
lib/gotta/run/ruby/runtime/template.rb

Overview

Copyright from 2019 Paulo Arruda (parrudaj at gmail dot com)

Licensed under the Commons Clause + Apache License, Version 2.0 (the “License”); you may not use this file except in compliance with the License. You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an “AS IS” BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.

Defined Under Namespace

Modules: Component Classes: DoubleRenderError, Event, Response, Template

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.included(base) ⇒ Object



6
7
8
# File 'lib/gotta/run/ruby/runtime/response.rb', line 6

def self.included(base)
  base.extend(self)
end

Instance Method Details

#build_stats(time, cpu_time) ⇒ Object



41
42
43
44
45
46
47
# File 'lib/gotta/run/ruby/runtime.rb', line 41

def build_stats(time, cpu_time)
  {
    'memory' => get_mem_info,
    'cpu_time' => cpu_time || 0.0,
    'time' => time || 0.0
  }
end

#calculate_times(start_time, end_time) ⇒ Object



49
50
51
# File 'lib/gotta/run/ruby/runtime.rb', line 49

def calculate_times(start_time, end_time)
  ((end_time - start_time) * 1000).round(2)
end

#get_mem_infoObject



33
34
35
36
37
38
39
# File 'lib/gotta/run/ruby/runtime.rb', line 33

def get_mem_info
  return 0 unless p_status = proc_status
  return Regexp.last_match(1).to_f / 1024.0 if
    p_status.match?(/RSS:\s*(\d+) kB/i)

  0.0
end

#get_timeObject



22
23
24
25
# File 'lib/gotta/run/ruby/runtime.rb', line 22

def get_time
  [Process.clock_gettime(Process::CLOCK_MONOTONIC),
   Process.clock_gettime(Process::CLOCK_PROCESS_CPUTIME_ID)]
end

#proc_statusObject



27
28
29
30
31
# File 'lib/gotta/run/ruby/runtime.rb', line 27

def proc_status
  File.open('/proc/self/status', 'r') { |f| f.read_nonblock(4096).strip }
rescue StandardError
  nil
end

#run_component(event) ⇒ Object



61
62
63
64
65
66
67
68
69
70
71
72
73
# File 'lib/gotta/run/ruby/runtime.rb', line 61

def run_component(event)
  Timeout.timeout(MAX_EXECUTION_TIME) do
    Loader::Component.load_handler
    lambda = ->(event) { Runtime::Component.handler(event) }
    return Loader::Executor.run(lambda, event) || Response.empty
  end
rescue Timeout::Error => e
  Response.timeout
rescue Exception => e
  puts '[RUBY] Component threw an exception'
  puts e.full_message
  Response.error(e)
end

#set_path(path) ⇒ Object



53
54
55
56
57
58
59
# File 'lib/gotta/run/ruby/runtime.rb', line 53

def set_path(path)
  return false unless File.directory?(path)

  Dir.chdir(path)
  $LOAD_PATH << path
  true
end