Class: Job::HttpMulti

Inherits:
Base
  • Object
show all
Defined in:
app/models/jobs/http_multi.rb

Constant Summary

Hydra =
Typhoeus::Hydra
Request =
Typhoeus::Request
MAX_RETRIES =
3
OPTS =
{:max_redirects => 3, :timeout => 5000, :method => :post}

Class Method Summary (collapse)

Methods inherited from Base

perform

Methods included from ResqueJobLogging

#application_trace, #around_perform_log_job

Class Method Details

+ (Object) perform_delegate(user_id, enc_object_xml, person_ids, retry_count = 0)



16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
# File 'app/models/jobs/http_multi.rb', line 16

def self.perform_delegate(user_id, enc_object_xml, person_ids, retry_count=0)
  user = User.find(user_id)
  people = Person.where(:id => person_ids)

  salmon = Salmon::SalmonSlap.create(user, Base64.decode64(enc_object_xml))

  failed_request_people = []

  hydra = Hydra.new
  people.each do |person|

    url = person.receive_url
    xml = salmon.xml_for(person)

    request = Request.new(url, OPTS.merge(:params => {:xml => CGI::escape(xml)}))

    request.on_complete do |response|
      if response.code >= 300 && response.code < 400
        if response.headers_hash['Location'] == response.request.url.sub('http://', 'https://')
          location = URI.parse(response.headers_hash['Location'])
          newuri = "#{location.scheme}://#{location.host}"
          newuri += ":#{location.port}" unless ["80", "443"].include?(location.port.to_s)
          newuri += "/"
          person.url = newuri
          person.save
        end
      end
      unless response.success?
        Rails.logger.info("event=http_multi_fail sender_id=#{user_id} recipient_id=#{person.id} url=#{response.effective_url} response_code='#{response.code}' xml='#{Base64.decode64(enc_object_xml)}'")
        failed_request_people << person.id
      end
    end

    hydra.queue request
  end
  hydra.run

  unless failed_request_people.empty?
    if retry_count < MAX_RETRIES
      Resque.enqueue(Job::HttpMulti, user_id, enc_object_xml, failed_request_people, retry_count + 1 )
    else
      Rails.logger.info("event=http_multi_abandon sender_id=#{user_id} failed_recipient_ids='[#{person_ids.join(', ')}] xml='#{Base64.decode64(enc_object_xml)}'")
    end
  end
end