Module: PostageApp::FailedRequest

Defined in:
lib/postageapp/failed_request.rb

Class Method Summary collapse

Class Method Details

.force_delete!(path) ⇒ Object



21
22
23
24
25
26
# File 'lib/postageapp/failed_request.rb', line 21

def self.force_delete!(path)
  File.delete(path)

rescue
  nil
end

.initialize_request(uid) ⇒ Object

Initializing PostageApp::Request object from the file



70
71
72
73
74
75
76
77
78
79
80
# File 'lib/postageapp/failed_request.rb', line 70

def self.initialize_request(uid)
  return false unless (self.store_path)
  return false unless (File.exist?(file_path(uid)))

  Marshal.load(File.read(file_path(uid))) 

rescue
  force_delete!(file_path(uid))

  false
end

.resend_allObject

Attempting to resend failed requests



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
61
62
63
64
65
66
67
# File 'lib/postageapp/failed_request.rb', line 29

def self.resend_all
  return false unless (self.store_path)
  
  Dir.foreach(store_path) do |filename|
    next unless (filename.match(/^\w{40}$/))
    
    request = initialize_request(filename)
    
    receipt_response = PostageApp::Request.new(
      :get_message_receipt,
      uid: filename
    ).send(true)

    if (receipt_response.fail?)
      return
    elsif (receipt_response.ok?)
      PostageApp.logger.info("Skipping failed request (already sent) [#{filename}]")

      force_delete!(file_path(filename))
    elsif (receipt_response.not_found?)
      PostageApp.logger.info("Retrying failed request [#{filename}]")

      response = request.send(true)
      
      # Not a fail, so we can remove this file, if it was then
      # there will be another attempt to resend

      unless (response.fail?)
        force_delete!(file_path(filename))
      end
    else
      PostageApp.logger.info("Skipping failed request (non-replayable request type) [#{filename}]")

      force_delete!(file_path(filename))
    end
  end

  return
end

.store(request) ⇒ Object

Stores request object into a file for future re-send returns true if stored, false if not (due to undefined project path)



6
7
8
9
10
11
12
13
14
15
16
17
18
19
# File 'lib/postageapp/failed_request.rb', line 6

def self.store(request)
  return false unless (self.store_path) 
  return false unless (PostageApp.configuration.requests_to_resend.member?(request.method.to_s))
  
  unless (File.exist?(file_path(request.uid)))
    open(file_path(request.uid), 'wb') do |f|
      f.write(Marshal.dump(request))
    end
  end
  
  PostageApp.logger.info("STORING FAILED REQUEST [#{request.uid}]")
  
  true
end