Changeset 1480
- Timestamp:
- 04/08/08 12:20:21 (5 months ago)
- Files:
-
- hive/trunk/worker_bee/lib/rest_client.rb (deleted)
- hive/trunk/worker_bee/lib/worker_bee.rb (modified) (3 diffs)
Legend:
- Unmodified
- Added
- Removed
- Modified
- Copied
- Moved
hive/trunk/worker_bee/lib/worker_bee.rb
r1383 r1480 1 1 module Hive 2 2 class WorkerBee 3 attr_reader :hive, :config , :client3 attr_reader :hive, :config 4 4 5 def initialize(config) 6 @config = config 5 def initialize 6 begin 7 f = "#{RAILS_ROOT}/config/workerbee.yml" 8 @config = YAML.load_file(f) 9 rescue SystemCallError 10 raise "Couldn't find or read the conf/worker.yml file. Tried: #{f}" 11 end 12 13 if @config["access_key"] == "nil" 14 raise "Access Key is not set." 15 end 16 17 hive = config["hive_url"] 18 hive << "/" unless hive[/\/$/] 19 @config["ping_url"] = "#{hive}bee/ping" 20 @config["client"] = "#{hive}bee/client" 21 @config["fingerprint"] = "#{hive}bee/fingerprint" 22 @config["history_urls"] = "#{hive}bee/history" 7 23 end 8 24 9 25 def setup_header 10 26 headers = {} 11 headers['Authorization'] = @config[ 'access_key']27 headers['Authorization'] = @config["access_key"] 12 28 headers['User-Agent'] = "Hive-Worker-Bee" 13 29 headers['Date'] = Time.now.httpdate 14 30 end 15 31 32 def get(url) 33 call_hive("get",url) 34 end 35 36 def post(url,body) 37 call_hive("post",path,url) 38 end 39 16 40 def ping? 17 41 begin 18 RestClient.get(@config['ping_url'], setup_header) 42 #RestClient.get(@config['ping_url'], setup_header) 43 get(@config['ping_url']) 19 44 return true 20 rescue Exception 45 rescue Exception => e 46 STDERR.print "Can't reach Global hive @ #{@config["ping_url"]} exception: #{e}\n" 21 47 return false 22 48 end … … 24 50 25 51 # Need to test this 26 def s end_client_data52 def ship_data 27 53 # Grab data from the local DB, serialize 28 54 # and send to the Hive 29 55 job = BeeJob.get_job 56 30 57 if job 31 STDOUT.print "Job to process!" 32 jt = job.job_type 33 jd = job.job_data 34 jd["job_type"] = jt 35 encoded_data = ActiveSupport::JSON.encode(jd) 58 #STDOUT.print "Job to process!" 36 59 60 encoded_data = ActiveSupport::JSON.encode(job[1]) 61 #form_data = "data=#{encoded_data}" 37 62 begin 38 RestClient.post( @config["data_url"], encoded_data, setup_header) 63 # Set the url based on the type 64 #RestClient.post( @config[job_type], form_data, setup_header) 65 post(@config[job[0]],encoded_data) 39 66 job.destroy # Remove the job from the database 40 67 rescue Exception => e … … 49 76 loop do 50 77 if ping? 51 s end_client_data78 ship_data 52 79 end 53 sleep(5*60) # 5 minutes 80 81 sleep(2) # 5 minutes 54 82 end 83 end 84 85 private 86 87 def call_hive(verb,u,body=nil) 88 url = parse_url(u) 89 request = request_method(verb).new(url.path,{}) 90 setup_headers!(request) 91 if body 92 request.set_form_data({'data'=>body}) 93 end 94 Net::HTTP.new(url.host,url.port).start do |http| 95 process_response http.request(request) 96 end 97 end 98 99 def process_response(res) 100 if %w(200 201).include? res.code 101 res.body 102 elsif res.code == "403" 103 raise "Unauthorized" 104 else 105 raise "HTTP Error. Status: #{res.code} #{res.body}" 106 end 107 end 108 109 def parse_url(url) 110 url = "http://#{url}" unless url.match(/^http/) 111 URI.parse(url) 112 end 113 114 def request_method(verb) 115 Net::HTTP.const_get(verb.to_s.capitalize) 116 end 117 118 def setup_headers!(req) 119 req['Authorization'] = @key 120 req['User-Agent'] = "Hive-Worker-Bee" 121 req['Date'] = Time.now.httpdate 55 122 end 56 123
