Changeset 1480

Show
Ignore:
Timestamp:
04/08/08 12:20:21 (5 months ago)
Author:
dbryson
Message:


Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • hive/trunk/worker_bee/lib/worker_bee.rb

    r1383 r1480  
    11module Hive 
    22  class WorkerBee 
    3     attr_reader :hive, :config, :client 
     3    attr_reader :hive, :config 
    44     
    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" 
    723    end 
    824     
    925    def setup_header 
    1026      headers = {} 
    11       headers['Authorization'] = @config['access_key'
     27      headers['Authorization'] = @config["access_key"
    1228      headers['User-Agent'] = "Hive-Worker-Bee" 
    1329      headers['Date'] = Time.now.httpdate 
    1430    end 
    1531     
     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     
    1640    def ping? 
    1741      begin 
    18         RestClient.get(@config['ping_url'], setup_header) 
     42        #RestClient.get(@config['ping_url'], setup_header) 
     43        get(@config['ping_url']) 
    1944        return true 
    20       rescue Exception 
     45      rescue Exception => e 
     46        STDERR.print "Can't reach Global hive @ #{@config["ping_url"]} exception: #{e}\n" 
    2147        return false 
    2248      end 
     
    2450     
    2551    # Need to test this 
    26     def send_client_data 
     52    def ship_data 
    2753      # Grab data from the local DB, serialize 
    2854      # and send to the Hive 
    2955      job = BeeJob.get_job 
     56       
    3057      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!" 
    3659         
     60        encoded_data = ActiveSupport::JSON.encode(job[1]) 
     61        #form_data = "data=#{encoded_data}" 
    3762        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) 
    3966          job.destroy # Remove the job from the database 
    4067        rescue Exception => e 
     
    4976      loop do 
    5077        if ping? 
    51           send_client_data 
     78          ship_data 
    5279        end 
    53         sleep(5*60) # 5 minutes 
     80         
     81        sleep(2) # 5 minutes 
    5482      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 
    55122    end 
    56123