module QM

Constants

VERSION

Public Instance Methods

create_app(options = {}) click to toggle source
# File lib/qm.rb, line 13
def create_app(options = {})
  # This function creates and configures a fresh app. This code is separate
  # from the `launch_service` method's code to allow direct use of a
  # `QMachineService` instance from within a Rackup file ("config.ru").
    require 'qm/service'
    app = Sinatra.new(QMachineService) do
        configure do
            convert = lambda do |x|
              # This converts all keys in a hash to symbols recursively.
                if x.is_a?(Hash) then
                    x = x.inject({}) do |y, (key, val)|
                        y[key.to_sym] = convert.call(val); y
                    end
                end
                return x
            end
            convert.call(options).each_pair do |key, val|
              # This provides feedback for user-specified options.
                if settings.qm_options.include?(key) then
                    set(key, val)
                else
                    STDERR.puts "Unknown option: #{key}"
                end
            end
            settings.qm_lazy.each do |key|
              # Eagerly evaluate the lambdas in `QMachineService` in the
              # correct scope and store their outputs. This strategy avoids
              # re-evaluating them for every HTTP request later, of course,
              # but the main motivation is to avoid endlessly opening new
              # connections without closing old ones.
                set(key, settings.send(key))
            end
        end
    end
    return app
end
launch_client(options = {mothership: 'https://api.qmachine.org'}) click to toggle source
# File lib/qm.rb, line 50
def launch_client(options = {mothership: 'https://api.qmachine.org'})
  # This function needs documentation.
    require 'qm/client'
    return QMachineClient.new(options)
end
launch_service(options = {}) click to toggle source
# File lib/qm.rb, line 56
def launch_service(options = {})
  # This function launches a new app using Unicorn :-)
    require 'unicorn'
    app = create_app(options)
    Unicorn::HttpServer.new(app, {
        before_fork: lambda {|server, worker|
          # This needs documentation.
            settings = server.app.settings
            settings.api_db.close if not settings.api_db.nil?
            settings.log_db.close if not settings.log_db.nil?
        },
        listeners: [
            app.settings.hostname.to_s + ':' + app.settings.port.to_s
        ],
        preload_app: true,
        timeout: 30,
        worker_processes: app.settings.worker_procs.to_s.to_i(10)
    }).start.join
    return
end
version() click to toggle source
# File lib/qm.rb, line 77
def version()
  # This function exists because it exists in the Node.js version.
    return VERSION
end