class Fanforce::PluginWorker LOADED_AT = Time.now @@iron = {} @@redis = {} @@log = Logger.new($stdout) @@log.datetime_format = '%H:%M:%S' @@log.formatter = proc do |severity, datetime, prog_name, msg| "#{datetime}: #{msg}\n" end def self.iron_token @@iron[:token] || ENV['IRON_TOKEN'] end def self.iron_project_id @@iron[:project_id] || ENV['IRON_PROJECT_ID'] end def self.iron_mq require 'iron_mq' @@iron[:mq] ||= IronMQ::Client.new(:token => @@iron[:token], :project_id => @@iron[:project_id]) end def self.redis_url_errorlog @@redis[:url_errorlog] || ENV['REDIS_URL_ERRORLOG'] || (raise 'No REDIS_URL_ERRORLOG found in ENV') end def self.set_config(obj) @@iron[:token] = obj[:iron_token] if obj[:iron_token] @@iron[:project_id] = obj[:iron_project_id] if obj[:iron_project_id] @@redis[:url_errorlog] = obj[:redis_url_errorlog] if obj[:redis_url_errorlog] end def self.log @@log end ########################################################################################## def self.enqueue(queue_id, params, options={}) raise 'Params being sent to the queue must be a Hash' if !params.is_a?(Hash) if ENV['FANFORCE_WORKER_SHOULD_BYPASS_ENQUEUE'] bypass_enqueue(queue_id, params) else queue_id = Utils.iron_queue_id(queue_id) retries = (options[:retries].present?) ? options.delete(:retries) : 0 iron_mq.queue(queue_id).post({params: params, retries: retries}.to_json, options) end end def self.bypass_enqueue(queue_id, params) config_file = "#{Plugin.base_dir}/workers/#{queue_id}.worker" exec_file = "#{Plugin.base_dir}/workers/#{queue_id}.rb" raise "Config file is missing: #{config_file}" if !File.exists?(config_file) raise "Ruby file is missing: #{exec_file}" if !File.exists?(exec_file) raise "RACK_ENV must be development: #{ENV['RACK_ENV']}" if ENV['RACK_ENV'] != 'development' params = {queue_id: queue_id, params: params} binding.eval(File.open(exec_file).read, exec_file) end def self.run(worker_data, min_execution_time=300, &code_block) if ENV['FANFORCE_WORKER_SHOULD_BYPASS_ENQUEUE'] RunnerLocal.new(worker_data, min_execution_time, &code_block) else Runner.new(worker_data, min_execution_time, &code_block) end end end