lib/qm.rb in qm-1.2.2 vs lib/qm.rb in qm-1.2.3

- old
+ new

@@ -1,26 +1,20 @@ #- Ruby source code #- qm.rb ~~ # ~~ (c) SRW, 12 Apr 2013 -# ~~ last updated 07 Dec 2014 +# ~~ last updated 23 Jan 2015 module QM - def self::launch_client(options = {mothership: 'https://api.qmachine.org'}) - # This function needs documentation. - require 'client' - return QMachineClient.new(options) - end - - def self::launch_service(options = {}) - # This function creates, configures, and launches a fresh Sinatra app - # that inherits from the original "teaching version". - require 'defs-mongo' - require 'service' + def self::create_app(options = {}) + # This function creates and configures a fresh Sinatra app that inherits + # from the original "teaching version". This code is separated from the + # `launch_service` method's code to allow a `QMachineService` instance to + # be used from the "config.ru" file of a Rack app. + require 'qm/service' app = Sinatra.new(QMachineService) do - register Sinatra::MongoConnect 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 |memo, (k, v)| @@ -30,24 +24,60 @@ end return x end options = convert.call(options) set options - if settings.persistent_storage.has_key?(:mongo) then - helpers Sinatra::MongoAPIDefs - mongo_api_connect - end - if settings.trafficlog_storage.has_key?(:mongo) then - helpers Sinatra::MongoLogDefs - mongo_log_connect - after do - log_to_db unless response.status == 444 - end - end end end - return app.run! + return app end + + def self::launch_client(options = {mothership: 'https://api.qmachine.org'}) + # This function needs documentation. + require 'qm/client' + return QMachineClient.new(options) + end + + def self::launch_service(options = {}) + # This function launches a new app using Unicorn :-) + require 'unicorn' + app = self::create_app(options) + Unicorn::HttpServer.new(app, { + 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 + +=begin + def self::launch_service(options = {}) + # This function launches a new app using Puma. I would prefer to use Puma + # instead of Unicorn in the future, in order to support as many of the + # different Ruby platforms as possible, but that's not the main priority + # for this "teaching version" anyway. Puma will teach *me* a lot about + # concurrency in the meantime :-) + require 'puma' + app = self::create_app(options) + server = Puma::Server.new(app) + server.add_tcp_listener(app.settings.hostname, app.settings.port) + server.min_threads = 1 + server.max_threads = 16 + puts "QM up -> http://#{app.settings.hostname}:#{app.settings.port} ..." + server.run.join + return + end +=end + + def self::version() + # This function exists because it exists in the Node.js version. + return VERSION + end + + VERSION = '1.2.3' end #- vim:set syntax=ruby: