module Gotta::Run::Ruby::Runtime

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.

Constants

Event

Public Class Methods

included(base) click to toggle source
# File lib/gotta/run/ruby/runtime/response.rb, line 6
def self.included(base)
  base.extend(self)
end

Public Instance Methods

build_stats(time, cpu_time) click to toggle source
# 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) click to toggle source
# 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_info() click to toggle source
# 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_time() click to toggle source
# 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_status() click to toggle source
# 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) click to toggle source
# 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) click to toggle source
# 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